0% found this document useful (0 votes)
3 views2 pages

Filtering

The document provides code snippets for various image filtering techniques using OpenCV, including average, Gaussian, bilateral, and median filtering, as well as image sharpening. Each technique is demonstrated with the loading of an image, applying the filter, and displaying the original and filtered images. Additionally, a function is defined to apply the chosen filter type based on user input.

Uploaded by

aimlbietdvg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Filtering

The document provides code snippets for various image filtering techniques using OpenCV, including average, Gaussian, bilateral, and median filtering, as well as image sharpening. Each technique is demonstrated with the loading of an image, applying the filter, and displaying the original and filtered images. Additionally, a function is defined to apply the chosen filter type based on user input.

Uploaded by

aimlbietdvg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1/11/2021 filtering

In [ ]: #average filtering
import cv2
img = cv2.imread('dog.jpg',1)
blur=cv2.blur(img,(5,5))
cv2.imshow('original image',img)
cv2.imshow('average filtering',blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

#gaussian filtering
import cv2
img = cv2.imread('dog.jpg',1)
gaussian =cv2.GaussianBlur(img,(5,5),10,15)
cv2.imshow('original image',img)
cv2.imshow('gaussian filtering',gaussian)
cv2.waitKey(0)
cv2.destroyAllWindows()

#bilateral filtering
import cv2
img = cv2.imread('dog.jpg',1)
bilateral =cv2.bilateralFilter(img,15, 75, 75)
cv2.imshow('bilateral filtering',bilateral)
cv2.imshow('original image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

#median filtering
import cv2
img = cv2.imread('dog.jpg',1)
median =cv2.medianBlur(img,(5,5),10,15)
cv2.imshow('median filtering',median)
cv2.imshow('original image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

#sharpening image

import cv2
import numpy as np
image = cv2.imread("flower1.jpg")
#define sharpening kernel
sharpeningKernel = np.array(([0, -1, 0],[-1, 5, -1],[0, -1, 0]), dtype="int")
sharp = cv2.filter2D(image, -1, sharpeningKernel)
#display images
cv2.imshow("original image", image)
cv2.imshow("output", sharp)
cv2.waitKey(0)
cv2.destroyAllWindows()

localhost:8889/nbconvert/html/Desktop/kavya/programming languages/opencv/filtering.ipynb?download=false 1/2


1/11/2021 filtering

In [ ]: import cv2
from skimage.util import random_noise
import matplotlib.pyplot as plt

def filtering(type):
if type=="median":
median1=cv2.medianBlur(img,5)
cv2.imshow('filtered image',median1)

elif type=="average":
average1=cv2.blur(img,(5,5))
cv2.imshow('filtered image',average1)
elif type=="gaussian":
gaussian1 =cv2.GaussianBlur(img,(5,5),10,15)
cv2.imshow('filtered image',gaussian1)
elif type=="bilateral":
bilateral1 =cv2.bilateralFilter(img,15, 75, 75)
cv2.imshow('filtered image',bilateral1)
elif type=="sharpening":
sharpeningKernel = np.array(([0, -1, 0],[-1, 5, -1],[0, -1, 0]), dtype
="int")
output = cv2.filter2D(img, -1, sharpeningKernel)
cv2.imshow("sharpe image", output)
img = cv2.imread('flower.jpg',1)
#img = random_noise(image, mode='gaussian') #noise will work with only average
filetring and gaussian filtering

type=input()
filtering(type)
cv2.imshow('original image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

localhost:8889/nbconvert/html/Desktop/kavya/programming languages/opencv/filtering.ipynb?download=false 2/2

You might also like