def sobelDetection(self, img): print(img) Sx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) Sy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) img_x = conv(img, Sx) img_y = conv(img, Sy) img_out = np.sqrt(img_x * img_x + img_y * img_y) img_out = np.ceil((img_out / np.max(img_out)) * 255) plt.imshow(img_out, cmap="gray", interpolation="bicubic") plt.xticks([]), plt.yticks([]) plt.show()
def SobelClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) Sx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) Sy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) img_x = conv(img, Sx) / 8.0 img_y = conv(img, Sy) / 8.0 img_out = np.sqrt(np.power(img_x, 2) + np.power(img_y, 2)) img_out = (img_out / np.max(img_out)) * 255 self.image = img self.displayImage(2) plt.imshow(img_out, cmap='gray', interpolation='bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show()
def CannyClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) h,w=img.shape[:2] #step 1: reduce noise with Gaussian Filter gauss = (1.0 / 57) * np.array( [[0, 1, 2, 1, 0], [1, 3, 5, 3, 1], [2, 5, 9, 5, 2], [1, 3, 5, 3, 1], [0, 1, 2, 1, 0]]) img_blur = conv(img, gauss) #step 2: Finding Gradien img_x=conv(img_blur,np.array([[-0.5,0,0.5]])) img_y=conv(img_blur,np.array([[-0.5,0,0.5]])) E_mag=np.sqrt(np.power(img_x,2)+np.power(img_y,2)) E_mag=(E_mag/np.max(E_mag))*255 #step 3: non-maximum suppresion t_low=4 E_nms=np.zeros((h,w)) for i in np.arange(1,h-1): for j in np.arange(1,w-1): dx=img_x[i,j] dy=img_y[i,j] s_theta=fg.FindingGradien(dx,dy) if locmax.MaxSuppesion(E_mag,i,j,s_theta,t_low): E_nms[i,j]=E_mag[i,j] #step 4: Hysterisis Thresholding t_high=15 E_bin=np.zeros((h,w)) for i in np.arange(1,h-1): for j in np.arange(1,w-1): if E_nms[i,j]>=t_high and E_bin[i,j]==0: ht.HysterisisThres(E_nms,E_bin,i,j,t_low) self.image=img self.displayImage(2) plt.imshow(E_bin,cmap='gray', interpolation='bicubic') plt.xticks([]),plt.yticks([]) plt.show()
def PrewitClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) Px = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]) Py = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]) img_x = conv(img, Px) img_y = conv(img, Py) img_out = np.sqrt((img_x * img_x) + (img_y * img_y)) img_out = (img_out / np.max(img_out)) * 255 self.image = img self.displayImage(2) plt.imshow(img_out, cmap='gray', interpolation='bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show()
def SharpeningClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) gauss = (1.0 / 16) * np.array( [[1, -2, 1], [-2, 5, -2], [1, -2, 1]]) img_out = conv(img, gauss) plt.imshow(img_out, cmap='gray', interpolation='bicubic') plt.xticks([], plt.yticks([])) plt.show()
def MeanClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) gauss = (1.0 / 57) * np.array( [[1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]]) img_out = conv(img, gauss) plt.imshow(img_out, cmap='gray', interpolation='bicubic') plt.xticks([], plt.yticks([])) plt.show()
def MeanClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) laplace = (1.0 / 9) * np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]) img_out = conv(img, laplace) plt.imshow(img_out, cmap='gray', interpolation='bicubic') plt.xticks([], plt.yticks([])) plt.show()
def SharpClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) laplace = (1.0 / 16) * np.array([[0, 0, -1, 0, 0], [0, -1, -2, -1, 0], [-1, -2, 16, -2, -1], [0, -1, -2, -1, 0], [0, 0, -1, 0, 0]]) img_out = conv(img, laplace) plt.imshow(img_out, cmap='gray', interpolation='bicubic') plt.xticks([], plt.yticks([])) plt.show()
def GaussianClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) gauss = (1.0 / 57) * np.array( [[0.0029, 0.0131, 0.0215, 0.0131, 0.0029], [0.0131, 0.0585, 0.0965, 0.0585, 0.0131], [0.0215, 0.0965, 0.1592, 0.0965, 0.0215], [0.0131, 0.0585, 0.0965, 0.0585, 0.0131], [0.0029, 0.0131, 0.0215, 0.0131, 0.0029]]) img_out = conv(img, gauss) plt.imshow(img_out, cmap='gray', interpolation='bicubic') plt.xticks([], plt.yticks([])) plt.show()
def SmoothClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) #h,w=img.shape[:2] gauss = (1.0 / 57) * np.array([[0, 1, 2, 1, 0], [1, 3, 5, 3, 1], [2, 5, 9, 5, 2], [1, 3, 5, 3, 1], [0, 1, 2, 1, 0]]) img_out = conv(img, gauss) # # #cv2.imshow('',img_out) # self.image=img_out # self.displayImage(i) plt.imshow(img_out, cmap='gray', interpolation='bicubic') plt.xticks([], plt.yticks([])) plt.show()
def CannyClicked(self): img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) gaus = (1.0 / 9) * np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) img_gaus = conv(img, gaus) # ubah format gambar img_gaus = img_gaus.astype("uint8") cv2.imshow("gaus",img_gaus) #sobel Sx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) Sy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) img_x = conv(img_gaus, Sx) img_y = conv(img_gaus, Sy) H, W = img.shape[:2] img_out = np.zeros((H, W)) for i in np.arange(H): for j in np.arange(W): x = img_x.item(i, j) y = img_y.item(i, j) hasil = np.abs(x) + np.abs(y) if (hasil > 255): hasil = 255 if (hasil < 0): hasil = 0 else: hasil = hasil img_out.itemset((i, j), hasil) # hasil gradient img_out = img_out.astype("uint8") #arah tepi theta = np.arctan2(img_y, img_x) cv2.imshow("sobel",img_out) #non-max supresion Z = np.zeros((H, W), dtype=np.int32) angle = theta * 180. / np.pi angle[angle < 0] += 180 for i in range(1, H - 1): for j in range(1, W - 1): try: q = 255 r = 255 # angle 0 if (0 <= angle[i, j] < 22.5) or (157.5 <= angle[i, j] <= 180): q = img_out[i, j + 1] r = img_out[i, j - 1] # angle 45 elif (22.5 <= angle[i, j] < 67.5): q = img_out[i + 1, j - 1] r = img_out[i - 1, j + 1] # angle 90 elif (67.5 <= angle[i, j] < 112.5): q = img_out[i + 1, j] r = img_out[i - 1, j] # angle 135 elif (112.5 <= angle[i, j] < 157.5): q = img_out[i - 1, j - 1] r = img_out[i + 1, j + 1] if (img_out[i, j] >= q) and (img_out[i, j] >= r): Z[i, j] = img_out[i, j] else: Z[i, j] = 0 except IndexError as e: pass img_N = Z.astype("uint8") cv2.imshow("NON", img_N) # hysteresis Thresholding menentukan tepi lemah dan kuat weak = 50 strong = 100 for i in np.arange(H): for j in np.arange(W): a = img_N.item(i, j) if (a > weak) : #weak b = weak if (a > strong): #strong b = 255 else: b = 0 img_N.itemset((i, j), b) img_H1 = img_N.astype("uint8") cv2.imshow("hysteresis part 1", img_H1) # hysteresis Thresholding eliminasi titik tepi lemah jika tidak terhubung dengan tetangga tepi kuat strong = 255 for i in range(1, H - 1): for j in range(1, W - 1): if (img_H1[i, j] == weak): try: if ((img_H1[i + 1, j - 1] == strong) or (img_H1[i + 1, j] == strong) or (img_H1[i + 1, j + 1] == strong) or (img_H1[i, j - 1] == strong) or (img_H1[i, j + 1] == strong) or (img_H1[i - 1, j - 1] == strong) or (img_H1[i - 1, j] == strong) or (img_H1[i - 1, j + 1] == strong)): img_H1[i, j] = strong else: img_H1[i, j] = 0 except IndexError as e: pass img_H2 = img_H1.astype("uint8") cv2.imshow("Canny Edge Detection", img_H2)