def edgeDetectionSobel(img: np.ndarray, thresh: float = 0.2) -> (np.ndarray, np.ndarray): """ Detects edges using the Sobel method :param img: Input image :param thresh: The minimum threshold for the edge response :return: opencv solution, my implementation """ s = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) thresh *= 255 # my_res = np.sqrt((conv2D(img, s) ** 2 + conv2D(img, s.transpose()) ** 2)) my_res = np.sqrt((cv.filter2D(img, -1, s, borderType=cv.BORDER_REPLICATE) ** 2 + cv.filter2D(img, -1, s.transpose(), borderType=cv.BORDER_REPLICATE) ** 2)) my = np.ndarray(my_res.shape) my[my_res > thresh] = 1 my[my_res < thresh] = 0 cv_res = cv.magnitude(cv.Sobel(img, -1, 1, 0), cv.Sobel(img, -1, 0, 1)) v = np.ndarray(cv_res.shape) v[cv_res > thresh] = 1 v[cv_res < thresh] = 0 return cv_res, my_res
def encode_as_struct_elems(image): """ Encode input image as an array of simple fixes sized square shaped structural elements. """ # encoding params anchor_pos = (-1, -1) # structural element params elem_size = 30 # only a single value required to describe a square shaped structural element elem_cg_dist_threshold = 4 # max dist of the CG of the structural element from its center elem_mass_threshold = 0.1 # min required active fraction of the mass within the structural element mask_vals = np.array([np.arange(0, elem_size)]) mask_col = np.repeat(mask_vals, elem_size, axis=0) mask_row = np.array(mask_col).T mask_ones = np.ones([elem_size, elem_size]) tmp_img = np.array(image, dtype=np.uint64) row_conv = cv2.filter2D(tmp_img, -1, mask_row, anchor=anchor_pos) col_conv = cv2.filter2D(tmp_img, -1, mask_col, anchor=anchor_pos) ones_conv = cv2.filter2D(tmp_img, -1, mask_ones, anchor=anchor_pos).astype( np.float64) + 1e-6 result_img_row = np.divide(row_conv, ones_conv) result_img_col = np.divide(col_conv, ones_conv) result_cg = np.stack([result_img_row, result_img_col], axis=2) result_cg_dist = np.sqrt( np.sum(np.power(result_cg - (elem_size / 2), 2), axis=2)) result_cg_thresh_dist_mask = result_cg_dist <= elem_cg_dist_threshold # get mask before calculating complement. result_cg_dist = 1 - scale_to_unit( result_cg_dist) # Sub from 1 to get CG distance complement. return np.multiply(result_cg_dist, result_cg_thresh_dist_mask)
def editor(img, num): vvar = che49.get() hvar = che50.get() svar = che51.get() global output if num != 0: if vvar == 1 and hvar == 1: kernel_3 = np.ones((num, num), dtype=np.float32) / (num * num) output = cv2.filter2D(img, -1, kernel_3) # print('output', output) elif hvar == 1: h_blur = np.zeros((num, num)) h_blur[num // 2, :] = np.ones(num) h_blur = h_blur / num output = cv2.filter2D(img, -1, h_blur) # print('output', output) elif vvar == 1: v_blur = np.zeros((num, num)) v_blur[:, num // 2] = np.ones(num) v_blur = v_blur / num output = cv2.filter2D(img, -1, v_blur) elif svar == 1: output = unsharp_mask(img, (5, 5), 2, num, 0) # print('output', output) else: output = img return output
def AreaMuSigma(img,R): img = np.array(img,dtype = float) h = np.ones((2*R+1,2*R+1)) n = h.sum() c1 = cv2.filter2D(img**2, -1, h/n, borderType=cv2.BORDER_REFLECT) mean = cv2.filter2D(img, -1, h/n, borderType=cv2.BORDER_REFLECT) c2 = mean**2 J = np.sqrt( np.maximum(c1-c2,0) ) return mean,J
def extractingShape(img_path, cell_size=8, block_size=2, bins=9): img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) img = cv2.resize(src=img, dsize=(64, 128)) h, w = img.shape # 128, 64 # gradient xkernel = np.array([[-1, 0, 1]]) ykernel = np.array([[-1], [0], [1]]) dx = cv2.filter2D(img, cv2.CV_32F, xkernel) dy = cv2.filter2D(img, cv2.CV_32F, ykernel) # histogram magnitude = np.sqrt(np.square(dx) + np.square(dy)) orientation = np.arctan(np.divide(dy, dx + 0.00001)) # radian orientation = np.degrees(orientation) # -90 -> 90 orientation += 90 # 0 -> 180 num_cell_x = w // cell_size # 8 num_cell_y = h // cell_size # 16 hist_tensor = np.zeros([num_cell_y, num_cell_x, bins]) # 16 x 8 x 9 for cx in range(num_cell_x): for cy in range(num_cell_y): ori = orientation[cy * cell_size:cy * cell_size + cell_size, cx * cell_size:cx * cell_size + cell_size] mag = magnitude[cy * cell_size:cy * cell_size + cell_size, cx * cell_size:cx * cell_size + cell_size] hist, _ = np.histogram(ori, bins=bins, range=(0, 180), weights=mag) # 1-D vector, 9 elements hist_tensor[cy, cx, :] = hist pass pass # normalization redundant_cell = block_size - 1 feature_tensor = np.zeros([ num_cell_y - redundant_cell, num_cell_x - redundant_cell, block_size * block_size * bins ]) for bx in range(num_cell_x - redundant_cell): # 7 for by in range(num_cell_y - redundant_cell): # 15 by_from = by by_to = by + block_size bx_from = bx bx_to = bx + block_size v = hist_tensor[ by_from:by_to, bx_from:bx_to, :].flatten() # to 1-D array (vector) feature_tensor[by, bx, :] = v / LA.norm(v, 2) np.seterr(divide='ignore', invalid='ignore') # avoid NaN: if np.isnan( feature_tensor[by, bx, :]).any(): # avoid NaN (zero division) feature_tensor[by, bx, :] = v return feature_tensor.flatten() # 3780 features
def calculate_weight_map(tar_img, ref_img, mask): tar_img_YUV = cv2.GaussianBlur(cv2.cvtColor(tar_img, cv2.COLOR_BGR2YUV), (5, 5), 10) ref_img_YUV = cv2.GaussianBlur(cv2.cvtColor(ref_img, cv2.COLOR_BGR2YUV), (5, 5), 10) tar_img_Y = tar_img_YUV[:, :, 0] ref_img_Y = ref_img_YUV[:, :, 0] YUV_diff = np.abs(tar_img_YUV - ref_img_YUV) color_diff = cv2.convertScaleAbs(YUV_diff[:, :, 0] * 0.5 + YUV_diff[:, :, 1] * 0.25 + YUV_diff[:, :, 2] * 0.25) color_diff[mask.overlap == 0] = 0 print('finish YUV_diff') grad_diff_mag = getGradDiff(tar_img_Y, ref_img_Y) grad_diff_mag[mask.overlap == 0] = 0 print('finish grad_diff') color_grad_diff_sum = grad_diff_mag * 100 + color_diff filter_bank = getGarborFilterBank(tar_img_Y, ref_img_Y) # print('finish gabor filter bank') h, w = tar_img_Y.shape tar_result = np.zeros((h, w)) for i in range(len(filter_bank)): temp = cv2.filter2D(tar_img_Y, cv2.CV_64FC1, filter_bank[i]) tar_result += temp**2 tar_result = np.sqrt(tar_result) tar_result[mask.overlap == 0] = 0 print('finish gabor feature target') ref_result = np.zeros((h, w)) for i in range(len(filter_bank)): temp = cv2.filter2D(ref_img_Y, cv2.CV_64FC1, filter_bank[i]) ref_result += temp**2 ref_result = np.sqrt(ref_result) ref_result[mask.overlap == 0] = 0 print('finish gabor feature reference') gabor_result = ref_result + tar_result weight_map = np.multiply(gabor_result, color_grad_diff_sum) print('finish weight map') # cv2.imwrite('./YUV_diff.png',color_diff) # cv2.imwrite('./gradian_diff.png',(grad_diff_mag/np.max(grad_diff_mag)*255).astype(np.uint8)) # cv2.imwrite('./color_grad_diff.png',(color_grad_diff_sum/np.max(color_grad_diff_sum)*255).astype(np.uint8)) # cv2.imwrite(f'./gabor/ref_gobar_final.png',(ref_result/np.max(ref_result)*255).astype(np.uint8) ) # cv2.imwrite(f'./gabor/gobar_final.png',(gabor_result/np.max(gabor_result)*255).astype(np.uint8) ) # cv2.imwrite(f'./gabor/tar_gobar_final.png',(tar_result/np.max(tar_result)*255).astype(np.uint8) ) # cv2.imwrite(f'./gabor/W_final.png',(weight_map-np.mean(weight_map))/np.std(weight_map)*255) return weight_map
def apply_prewitt(img, *params): img = cv2.GaussianBlur(img, (3, 3), cv2.BORDER_DEFAULT) img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) kernel_x = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]]) kernel_y = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]) prewittx = cv2.filter2D(img, -1, kernel_x) prewitty = cv2.filter2D(img, -1, kernel_y) return prewittx + prewitty
def edgeDetectionSobel(I: np.ndarray) -> (np.ndarray, np.ndarray): Gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) Gy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) Gcx = cv2.filter2D(I, -1, Gx) Gcy = cv2.filter2D(I, -1, Gy) rs = np.matmul(Gcx, Gcx) + np.matmul(Gcy, Gcy) rs = np.sqrt(rs) return rs
def hist_extract(image, handHist): hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0, 1], handHist, [0, 180, 0, 256], 1) disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (21, 21)) cv2.filter2D(dst, -1, disc, dst) # dst is now a probability map # Use binary thresholding to create a map of 0s and 1s # 1 means the pixel is part of the hand and 0 means not ret, thresh = cv2.threshold(dst, 150, 255, cv2.THRESH_BINARY) kernel = np.ones((5, 5), np.uint8) thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=8) #thresh = cv2.merge((thresh, thresh, thresh)) return thresh
def prewitt(): image = imageInit image = grayScale() edged_gaussian = cv2.GaussianBlur(image, (3, 3), 0) kernelx = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]]) kernely = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]) img_prewittx = cv2.filter2D(edged_gaussian, -1, kernelx) img_prewitty = cv2.filter2D(edged_gaussian, -1, kernely) edged = img_prewittx + img_prewitty show_img(ImageTk.PhotoImage(Img.fromarray(edged)))
def convDerivative(inImage: np.ndarray) -> np.ndarray: kernel = np.array([[1, 0, -1], [1, 0, -1], [1, 0, -1]]) rows = cv2.filter2D(inImage, -1, kernel) cols = cv2.filter2D(inImage, -1, np.transpose(kernel)) rows = rows * rows cols = cols * cols power = rows * rows + cols * cols for i in range(0, power.shape[0]): for j in range(0, power.shape[1]): power[i, j] = power[i, j]**(0.5) cv2.imshow("myImage", power) cv2.waitKey(0)
def get_driver_dlib(self, image): try: # get faces in image gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) # gray image #faces = self.get_faces(self.face_net, image) #faces = self.face_cascade.detectMultiScale(gray, 1.3, 5) #faces = self.detector(gray, 1) img = image.copy() img = cv.filter2D(img, -1, kernel=np.array([[0, -1, 0], [-1, 5.5, -1], [0, -1, 0]], np.float32)) # Convert to RGB image compatible to dlib.load_rgb_image(f) # http://dlib.net/face_landmark_detection.py.html img = img[:, :, [2, 1, 0]] # BGR => RGB #faces = self.detector(img, 1) faces = self.detector(img) except Exception as ex: print("Exception in detect_image: %s" % ex) faces = None face = None area = 0 for i in faces: face_area = (i.right() - i.left() + 1) * (i.bottom() - i.top() + 1) if face_area > area: face = i area = face_area return face
def sharpening(image_in): alpha = 1 # 扩散系数 kernel = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], np.float32) + alpha * np.array( [[0, -1, 0], [-1, 4, -1], [0, -1, 0]], np.float32) image_out = cv2.filter2D(image_in, -1, kernel=kernel) return image_out
def blur_(images, m=2): # 設置3x3矩陣每個元素為1/9 r = 5 kernel = np.ones((r, r), np.float32) / (r * r) out = [] if m == 1: for i in range(len(images)): # 將圖片經過3x3矩陣平均模糊化去雜訊 dst = cv2.filter2D(images[i], -1, kernel) out.append(dst) elif m == 2: for i in range(len(images)): # 將圖片經過3x3矩陣平均模糊化去雜訊 dst = cv2.medianBlur(images[i], r) out.append(dst) else: for i in range(len(images)): # 將圖片做高斯模糊化去雜訊 dst = cv2.GaussianBlur(images[i], (r, r), 1) out.append(dst) return out
def ireduce(image): out = None h = np.array([1, 4, 6, 4, 1]) / 16 filt = (h.T).dot(h) outimage = cv2.filter2D(image, cv2.CV_64F, filt) out = outimage[::2, ::2] return out
def edgeDetectionCanny(I: np.ndarray) -> (np.ndarray, np.ndarray): #https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html #first blur the image I = blurImage2(I, 5) #claculate magnitude and angle Gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) Gy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) Gcx = cv2.filter2D(I, -1, Gx) Gcy = cv2.filter2D(I, -1, Gy) rs = np.matmul(Gcx, Gcx) + np.matmul(Gcy, Gcy) rs = np.sqrt(rs) angle = np.arctan(Gcx / Gcy)
def edgeDetectionZeroCrossingSimple(img: np.ndarray) -> (np.ndarray): """ Detecting edges using the "ZeroCrossing" method :param I: Input image :return: Edge matrix """ k = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) d = cv.filter2D(img, -1, k, borderType=cv.BORDER_REPLICATE) res = np.zeros(d.shape) # Check for a zero crossing around (x,y) for i in range(0, d.shape[0]): for j in range(0, d.shape[1]): try: if d[i, j] == 0: if (d[i, j + 1] > 0 and d[i, j - 1] < 0) or (d[i, j + 1] < 0 and d[i, j - 1] > 0) or ( d[i + 1, j] > 0 and d[i - 1, j] < 0) or (d[i + 1, j] < 0 and d[i - 1, j] > 0): res[i, j] = 1 elif d[i, j] > 0: if d[i, j + 1] < 0 or d[i + 1, j] < 0: res[i, j] = 1 else: if d[i, j + 1] > 0 or d[i + 1, j] > 0: res[i, j] = 1 except IndexError as e: pass return res
def blurImage1(inImage: np.ndarray, kernelSize: np.ndarray) -> np.ndarray: kernel = CreateBlurKernel(kernelSize) #rs = conv2D(inImage, kernel) rs = cv2.filter2D(inImage, -1, kernel) cv2.imshow("myImage", rs) cv2.waitKey(0) return rs
def keskinlestir(self): if self.tmp is not None: sharpenKernel = np.array( ([[0, -1, 0], [-1, 9, -1], [0, -1, 0]]), np.float32) / 9 sharpen = cv2.filter2D(src=self.tmp, kernel=sharpenKernel, ddepth=-1) self.setPhoto(sharpen)
def convolution(image, filters): result = np.zeros_like(image, dtype=float) # Normalize images for better comparison. # image = (image - image.mean()) // image.std() for kern in filters: fimg = cv2.filter2D(image, cv2.CV_64FC3, kern) np.maximum(result, fimg, result) return result
def gaborFilter(img): cvimg = np.array(img.convert("L")) g_kernel = cv2.getGaborKernel((11, 11), 8.0, np.pi/4, 10.0, 0.5, 0, ktype=cv2.CV_32F) filtered_img = cv2.filter2D(cvimg, cv2.CV_8UC3, g_kernel) #h, w = g_kernel.shape[:2] #g_kernel = cv2.resize(filtered_img, (3*w, 3*h), interpolation=cv2.INTER_CUBIC) return Image.fromarray(filtered_img).convert('L')
def gray_to_gradient(img): if len(img.shape) == 3: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # row, column = img.shape[0], img.shape[1] img_f = np.copy(img) img_f = img_f.astype("float") kernel_horizontal = np.array([[0, 0, 0], [0, -1., 1.], [0, 0, 0]]) kernel_vertical = np.array([[0, 0, 0], [0, -1., 0], [0, 1., 0]]) dst1 = abs(cv2.filter2D(img_f, -1, kernel_horizontal)) dst2 = abs(cv2.filter2D(img_f, -1, kernel_vertical)) gradient = (dst1 + dst2).astype("uint8") return gradient
def custom_demo(image): kernel = np.array([[ 1, 1, 1, ], [1, -8, 1], [1, 1, 1]]) dst = cv.filter2D(image, cv.CV_32F, kernel=kernel) custom = cv.convertScaleAbs(dst) cv.imshow("result", custom)
def addWeighted(img): # img = color2gray(img) # img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # blur_img = cv2.GaussianBlur(img, (0,0), 25) # usm = cv2.addWeighted(img, 1.5, blur_img, -0.5, 0) # img = cv2.cvtColor(usm, cv2.COLOR_BGR2GRAY) # return img kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) #锐化 return cv2.filter2D(img, -1, kernel=kernel)
def motion_blur(gray, degree, angle): gray = np.array(gray) M = cv2.getRotationMatrix2D((round(degree / 2), round(degree / 2)), angle, 1) motion_blur_kernel = np.diag(np.ones(degree)) motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree)) PSF = motion_blur_kernel / degree blurred = cv2.filter2D(gray, -1, PSF) blurred = cv2.normalize(blurred,None, 0, 255, cv2.NORM_MINMAX) blurred = np.array(blurred, dtype=np.uint8) return blurred,PSF
def iexpand(image): out = None h = np.array([1, 4, 6, 4, 1]) / 16 filt = (h.T).dot(h) outimage = np.zeros((image.shape[0] * 2, image.shape[1] * 2), dtype=np.float64) outimage[::2, ::2] = image[:, :] out = cv2.filter2D(outimage, cv2.CV_64F, filt) return out '''reduce image by 1/2'''
def customer_blur_demo(image, kernel_blur=[[0, -1, 0], [-1, 5, -1], [0, -1, 0]]): #定义卷积核---均值模糊的效果 # kernel = np.ones([5,5],np.float32/25) # 定义卷积核---锐化 kernel = np.array(kernel_blur, np.float32) dst = cv.filter2D(image, -1, kernel=kernel) #cv.imshow('customer_blur_demo',dst) return dst
def testBlur(): img = cv2.imread('resource/one1.png') for ang in range(-360, 360, 15): kernel = blurKernel(length=20, angle=ang) motion_blur = cv2.filter2D(img, -1, kernel) # blur = cv2.GaussianBlur() winname = 'test motion {}'.format(ang) cv2.imshow(winname, motion_blur) cv2.waitKey(0) cv2.destroyWindow(winname)
def convolve(image, kernel): """convolve the image with the kernel. :param img: the image to convolve :type img: cv2 image :param kernel: the kernel to convolve with :type kernel: matrix """ img = np.copy(image) return (cv2.filter2D(img, -1, flip_kernel(kernel)))
def blurImage2(in_image: np.ndarray, kernel_size: int) -> np.ndarray: """ Blur an image using a Gaussian kernel using OpenCV built-in functions :param inImage: Input image :param kernelSize: Kernel size :return: The Blurred image """ sigma = 0.3 * ((kernel_size - 1) * 0.5 - 1) + 0.8 guassian = cv.getGaussianKernel(kernel_size, sigma) guassian = guassian * guassian.transpose() return cv.filter2D(in_image, -1, guassian, borderType=cv.BORDER_REPLICATE)