def skeleton_text(self): self.gray_img = cv2.bitwise_not(self.gray_img) self.gray_img = imutils.skeletonize(self.gray_img, (3, 3), cv2.MORPH_ELLIPSE) self.gray_img = cv2.dilate(self.gray_img, None) self.gray_img = cv2.bitwise_not(self.gray_img) return self.gray_img
def make_prediction(image) : res = image.T resized = cv2.resize(res[0],(56,56)) resized = imutils.skeletonize(resized,size=(3,3)) resized = transform(resized) client_socket.send(resized.tobytes()) prediction = int(client_socket.recv(1024).decode()) print(prediction)
def auto_canny(img_path): img = cv2.imread(img_path, 0) img_canny = imutils.auto_canny(img) img_skeleton = imutils.skeletonize(img, size=(3, 3)) plt.subplot(131) plt.imshow(imutils.opencv2matplotlib(img_canny)) plt.axis('off') plt.subplot(132) plt.imshow(imutils.opencv2matplotlib(img)) plt.axis('off') plt.subplot(133) plt.imshow(imutils.opencv2matplotlib(img_skeleton)) plt.axis('off') plt.show()
def dust_analysis(self, custom_lcb_img=None, debug=False): heatmap = self.heatmap if custom_lcb_img is None else custom_lcb_img R = heatmap[:, :, 2].astype(np.uint16) # R means LCB points G = heatmap[:, :, 1].astype(np.uint16) # G means it is near LCB points defects = cv2.convertScaleAbs(R + G) for i in range(9): defects = cv2.medianBlur(defects, 3) defects = imutils.skeletonize(defects, (3, 3), cv2.MORPH_ELLIPSE) defects[defects > 0] = 255 cnts = cv2.findContours(defects.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] for c in cnts: center, radius = cv2.minEnclosingCircle(c) point_in_contour = cv2.pointPolygonTest(c, center, False) dust_type = None if point_in_contour < 0: dust_type = "Lens" self.dust_stat.append([center, radius, dust_type]) elif R[int(center[1]), int(center[0])] > 0: dust_type = "IRCF" self.dust_stat.append([center, radius, dust_type]) # else: # dust_type = "Flare" # self.dust_stat.append([center, radius, dust_type]) if debug: cv2.circle(heatmap, (int(center[0]), int(center[1])), int(radius), (255, 0, 255), 1) cv2.putText(heatmap, dust_type, (int(center[0]) + int(radius), int(center[1] - int(radius))), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 0, 255)) if debug: print(np.count_nonzero(R)) cv2.imshow("defects", cv2.resize(defects, (600, 400))) # cv2.drawContours(heatmap, cnts, -1, (255, 0, 255), 1) cv2.imshow("heatmap", cv2.resize(heatmap, (600, 400))) cv2.waitKey(0)
def skeleton(self, frames): image = cv2.imread(frames, 1) cv2.imshow('image', image) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) skeleton = imutils.skeletonize(gray, size=(3, 3)) cv2.imshow("Skeleton", skeleton) cv2.imwrite('skeleton[i].jpg', skeleton) k = cv2.waitKey(0) if k == 27: # wait for ESC key to exit cv2.destroyAllWindows() elif k == ord('s'): # wait for 's' key to save and exit cv2.imwrite('messigray.png', image) cv2.destroyAllWindows()
def skeletonize(self, pic): self.log.info("applying skeletonize filter") gray = cv2.cvtColor(pic, cv2.COLOR_BGR2GRAY) skelton = imutils.skeletonize(gray, size=(3, 3)) return skelton
# 3. RESIZING # loop over varying widths to resize the image to for width in (400, 300, 200, 100): # resize the image and display it resized = imutils.resize(workspace, width=width) cv2.imshow("Width=%dpx" % (width), resized) # wait for a keypress, then close all the windows cv2.waitKey(0) cv2.destroyAllWindows() # 4. SKELETONIZATION # skeletonize the image using a 3x3 kernel cv2.imshow("Original", logo) gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY) skeleton = imutils.skeletonize(gray, size=(3, 3)) cv2.imshow("Skeleton", skeleton) cv2.waitKey(0) cv2.destroyAllWindows() # 5. MATPLOTLIB # INCORRECT: show the image without converting color spaces plt.figure("Incorrect") plt.imshow(cactus) # CORRECT: convert color spaces before using plt.imshow plt.figure("Correct") plt.imshow(imutils.opencv2matplotlib(cactus)) plt.show() # 6. URL TO IMAGE
def annotate_image_array_3(image_in): # Get Image properties g.vi_width = image_in.shape[1] g.vi_height = image_in.shape[0] """ Hough Transform parameters """ g.rho = 0.8 # (2 -> 0.8) distance resolution in pixels of the Hough grid g.theta = 1 * np.pi/180 # angular resolution in radians of the Hough grid g.threshold = 25 # (15 -> 25) minimum number of votes (intersections in Hough grid cell) g.min_line_length = 50 # (10 -> 50) minimum number of pixels making up a line g.max_line_gap = 200 # (20 -> 200) maximum gap in pixels between connectable line segments """ Given an image Numpy array, return the annotated image as a Numpy array """ # Replace hsv mask filetring and canny filter with a second method #image_in = cv2.resize(image_in, (640, 480)) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(image_in) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: image1=cv2.cvtColor(image_in, cv2.COLOR_RGB2BGR) cv2.namedWindow('image_in', cv2.WINDOW_NORMAL) cv2.imshow('image_in', image1) # Read in and grayscale the image gray = grayscale(image_in) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(gray) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: cv2.namedWindow('gray', cv2.WINDOW_NORMAL) cv2.imshow('gray', gray) # Improve contrast gray = cv2.equalizeHist(gray) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(gray) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: cv2.namedWindow('gray_eq_hist', cv2.WINDOW_NORMAL) cv2.imshow('gray_eq_hist', gray) # Gaussian blur #filter_gray = cv2.GaussianBlur(gray, (7, 7), 0) #filter_gray = cv2.GaussianBlur(gray, (3,3), 5) #filter_gray = cv2.GaussianBlur(gray, (3,3), 5) #filter_gray = cv2.medianBlur(gray, 5) filter_gray = cv2.bilateralFilter(gray,9,50,50) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(filter_gray) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: cv2.namedWindow('blur_gray', cv2.WINDOW_NORMAL) cv2.imshow('blur_gray', filter_gray) # Threshold filter_gray_thresh = cv2.adaptiveThreshold(filter_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(filter_gray_thresh) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: cv2.namedWindow('thresh_gray', cv2.WINDOW_NORMAL) cv2.imshow('thresh_gray', filter_gray_thresh) # Opening (erode/dilate = noise cleanup) kernel = np.ones((2,2),np.uint8) erode_frame = cv2.morphologyEx(filter_gray_thresh, cv2.MORPH_OPEN, kernel) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(erode_frame) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: cv2.namedWindow('erode_frame', cv2.WINDOW_NORMAL) cv2.imshow('erode_frame', erode_frame) # Skeletonize (substract/bitwise_or) # http://opencvpython.blogspot.fr/2012/05/skeletonization-using-opencv-python.html # TODO KO roi_frame = skimage.morphology.skeletonize(roi_frame) ####kernel = np.ones((1,1),np.uint8 ####img_morphex = cv2.morphologyEx(erode_frame, cv2.MORPH_OPEN, kernel) ####blur = cv2.GaussianBlur(img_morphex,(1,1),0) ####ret3,th4 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) ####print ('th4:',th4) ####th4[th4 == 255] = 1 ####skeleton_frame = skimage.morphology.skeletonize(th4) skeleton_frame = imutils.skeletonize(erode_frame, size=(3, 3)) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(skeleton_frame) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: cv2.namedWindow('skeleton_frame', cv2.WINDOW_NORMAL) cv2.imshow('skeleton_frame', skeleton_frame) bitwise_frame = cv2.subtract(filter_gray_thresh, erode_frame) ####bitwise_frame = cv2.subtract(filter_gray_thresh, skeleton_frame) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(bitwise_frame) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: cv2.namedWindow('bitwise_frame', cv2.WINDOW_NORMAL) cv2.imshow('bitwise_frame', bitwise_frame) # Create masked edges using trapezoid-shaped region-of-interest imshape = image_in.shape vertices = np.array([[\ ((imshape[1] * (1 - g.trap_bottom_width)) // 2, imshape[0]),\ ((imshape[1] * (1 - g.trap_top_width)) // 2, imshape[0] - imshape[0] * g.trap_height),\ (imshape[1] - (imshape[1] * (1 - g.trap_top_width)) // 2, imshape[0] - imshape[0] * g.trap_height),\ (imshape[1] - (imshape[1] * (1 - g.trap_bottom_width)) // 2, imshape[0])]]\ , dtype=np.int32) if DEBUG_LEVEL >= DEBUG_LEVEL2: print (get_fl_name(), 'vertices:', vertices , 'vertices.shape:', vertices.shape) masked_edges = region_of_interest(bitwise_frame, vertices) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(masked_edges) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: cv2.namedWindow('masked_edges', cv2.WINDOW_NORMAL) cv2.imshow('masked_edges', masked_edges) # Run Hough on edge detected image line_image = hough_lines(masked_edges, g.rho, g.theta, g.threshold, g.min_line_length, g.max_line_gap) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL2: imshow(line_image) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL2: cv2.namedWindow('line_image', cv2.WINDOW_NORMAL) cv2.imshow('line_image', line_image) # Draw lane lines on the original image initial_image = image_in.astype('uint8') annotated_image = weighted_img(line_image, initial_image) if DEBUG_IMSHOW_LEVEL >= DEBUG_LEVEL1: imshow(annotated_image) if DEBUG_CV2IMSHOW_LEVEL >= DEBUG_LEVEL1: image1 = cv2.cvtColor(annotated_image, cv2.COLOR_RGB2BGR) cv2.namedWindow('annotated_image', cv2.WINDOW_NORMAL) cv2.imshow('annotated_image', image1) cv2.waitKey(1) return annotated_image
def skeletonization(self): # No img = self.to_gray_scales() skeleton = imutils.skeletonize(img, size=(5, 5)) return skeleton
# rotation rotation_image = imutils.rotate(image, 90) rotation_bound_image = imutils.rotate_bound(image, 45) cv2.imwrite(r'images/rotation_image_90.jpg', rotation_image) cv2.imwrite(r'images/rotation_bound_image_45.jpg', rotation_bound_image) # resizing: 等比例缩放 resized_image = imutils.resize(image, width=400) cv2.imwrite(r'images/resized_image_400.jpg', resized_image) # skeletonization: 提取拓扑骨架(topological skeleton) # size:粒度,越小越耗时 # 转成灰度图 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) skeleton_image = imutils.skeletonize(gray_image, size=(3, 3)) skeleton_Canny_image = cv2.Canny(gray_image, 60, 200) cv2.imwrite(r'images/gray_image.jpg', gray_image) cv2.imwrite(r'images/skeleton_image.jpg', skeleton_image) cv2.imwrite(r'images/skeleton_Canny_image.jpg', skeleton_Canny_image) # matplotlib 显示 # cv2.cvtColor 将 BGR 序列转换为 RGB 序列 # 使用 imutils.opencv2matplotlib plt.figure('Opencv vs Matplotlib') plt.subplot(1, 2, 1) plt.imshow(image) plt.axis('off') plt.title('Incorrect') plt.subplot(1, 2, 2) plt.imshow(imutils.opencv2matplotlib(image))
#-*- coding: utf-8 -* import imutils import cv2 img = cv2.imread('/Users/hackerifyouwant/Downloads/index.jpg',cv2.IMREAD_GRAYSCALE) img = imutils.skeletonize(img,size=(3,3)) cv2.imshow("123",img) cv2.waitKey(0) cv2.destroyAllWindows()
# resize the image and display it resized = imutils.resize(workspace, width=width) plt.imshow(resized, cmap='gray', interpolation='bicubic') plt.xticks([]), plt.yticks([]) plt.title("Width=%dpx" % (width)) plt.show() #Skeletonizatrion #encuentra la esquletonizacion de la imagen plt.imshow(logo, cmap='gray', interpolation='bicubic') plt.xticks([]), plt.yticks([]) plt.title("Original") plt.show() gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY) skeleton = imutils.skeletonize(gray, size=(3, 3), structuring=cv2.MORPH_RECT) plt.imshow(skeleton, cmap='gray', interpolation='bicubic') plt.xticks([]), plt.yticks([]) plt.title("Skeleton") plt.show() #Forma incorrecta de mostrar una imagen plt.figure("Incorrecto") plt.imshow(cactus) #forma correcta plt.figure("Correcto") plt.imshow(imutils.opencv2matplotlib(cactus)) plt.show() # Imagen de una URL # Cargar una imagen desde la URL, convertirla al formato de openCV y muestrelo
# cv2.waitKey(0) # 图像大小 # 在保持原图像长宽比例不变的情况下,改变图像大小。也可单独设置长或宽,同时设置长和宽时,以长为标准 resized_w = imutils.resize(img, width=400) resized_h = imutils.resize(img, height=100) resized_wh = imutils.resize(img, width=400, height=50) # cv2.imshow("resized_w", resized_w) # cv2.imshow("resized_h", resized_h) # cv2.imshow("resized_wh", resized_wh) # cv2.waitKey(0) # 骨架化 Skeletonization gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # size是结构化元素内核的大小 skeleton = imutils.skeletonize(gray, size=(5, 5)) # cv2.imshow("gray", gray) # cv2.imshow("Skeleton", skeleton) res = np.hstack((gray, skeleton)) # cv2.imshow("res", res) # cv2.waitKey(0) # 使用Matplotlib展示图片 import matplotlib.pyplot as plt plt.imshow(imutils.opencv2matplotlib(img)) # plt.show() # url转为image url = "https://www.baidu.com/img/[email protected]" logo = imutils.url_to_image(url)
translate_img = imutils.translate(org_img, 20, -75) ''' cv.warpAffine x=25 pixels to the right, y=75 pixels up ''' # 3. Rotation for angle in range(0, 360, 90): rotate_img = imutils.rotate(org_img, angle=angle) cv2.imshow("Angle: %d" % (angle), rotate_img) ''' cv.gerRotationMatrix2D ''' # 4. Resizing for size in (400, 300, 200, 100): resize_img = imutils.resize(org_img, width=size, height=size/2) # 5. Skeletonization gray = cv2.cvtColor(org_img,cv2.COLOR_BGR2GRAY) skeleton_image = imutils.skeletonize(gray, size=(2,3)) cv2.imshow("Skeleton", skeleton_image) # 6. display with matplotlib '''cv.imshow '''