Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- def addPointNoise(img, percentage, value):
- noise = np.copy(img)
- n = int(img.shape[0] * img.shape[1] * percentage)
- print(n)
- for k in range(1, n):
- i = np.random.randint(0, img.shape[1])
- j = np.random.randint(0, img.shape[0])
- if img.ndim == 2:
- noise[j, i] = value
- if img.ndim == 3:
- noise[j, i] = [value, value, value]
- return noise
- def addSaltAndPepperNoise(img, percentage1, percentage2):
- n = addPointNoise(img, percentage1, 255) # Só
- n2 = addPointNoise(n, percentage2, 0) # Bors
- return n2
- img = cv2.imread('Grape.jpg',cv2.IMREAD_GRAYSCALE);
- imnoisegauss5x5 = cv2.GaussianBlur(img, (5, 5), sigmaX=2.0, sigmaY=2.0)
- threshold, im_thresh = cv2.threshold(imnoisegauss5x5, 170, 255, cv2.THRESH_BINARY)
- kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
- dst = cv2.dilate(im_thresh,kernel,iterations=1)
- dst = cv2.erode(dst,kernel,iterations=2)
- dst = cv2.dilate(dst,kernel,iterations=1)
- dstinv = 255-dst
- blurred = cv2.GaussianBlur(dstinv, (5, 5), 2.0)
- edges = cv2.Canny(blurred, 100, 200, None, 5, True)
- img_ocv_edge = cv2.cvtColor(img.copy(),cv2.COLOR_GRAY2BGR)
- b, g, r = cv2.split(img_ocv_edge)
- r = cv2.bitwise_and(r, ~edges)
- g = cv2.bitwise_or(g, edges)
- b = cv2.bitwise_and(b, ~edges)
- img_ocv_edge = cv2.merge((b, g, r))
- cv2.imwrite('done.jpg',img_ocv_edge)
- cv2.imshow('img', img)
- cv2.waitKey(0)
- cv2.imshow('img', imnoisegauss5x5)
- cv2.waitKey(0)
- cv2.imshow('img', im_thresh)
- cv2.waitKey(0)
- cv2.imshow('img', dst)
- cv2.waitKey(0)
- cv2.imshow('img', dstinv)
- cv2.waitKey(0)
- cv2.imshow('img', edges)
- cv2.waitKey(0)
- cv2.imshow('img', img_ocv_edge)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement