def redraw(self): edge_img = cv.Mat() # 边缘检测 cv.Canny(self.img_gray, edge_img, self.th1, self.th2) 3 ### # 计算结果图 if self.show_canny: show_img = cv.Mat() cv.cvtColor(edge_img, show_img, cv.CV_GRAY2BGR) else: show_img = self.img.clone() 4 ### # 线段检测 theta = self.theta / 180.0 * np.pi lines = cv.HoughLinesP(edge_img, self.rho, theta, self.hough_th, self.minlen, self.maxgap) for line in lines: cv.line(show_img, cv.asPoint(line[:2]), cv.asPoint(line[2:]), cv.CV_RGB(255, 0, 0), 2) 5 ### # 圆形检测 circles = cv.HoughCircles(self.img_smooth, 3, self.dp, self.mindist, param1=self.param1, param2=self.param2) for circle in circles: cv.circle(show_img, cv.Point(int(circle[0]), int(circle[1])), int(circle[2]), cv.CV_RGB(0, 255, 0), 2) cv.imshow("Hough Demo", show_img)
def make_grid_img(self): img = self.img.clone() for i in range(0, self.w, 30): cv.line(img, cv.Point(i, 0), cv.Point(i, self.h), cv.CV_RGB(0, 0, 0), 1) for i in range(0, self.h, 30): cv.line(img, cv.Point(0, i), cv.Point(self.w, i), cv.CV_RGB(0, 0, 0), 1) return img
def redraw(self): # 同时显示两幅图像 w = self.img1.size().width h = self.img1.size().height show_img = cv.Mat(cv.Size(w * 2, h), cv.CV_8UC3) for i in range(3): show_img[:, :w, i] = self.img1[:] show_img[:, w:, i] = self.img2[:] # 绘制特征线条 if self.draw_circle: self.draw_keypoints(show_img, self.keypoints1, 0) self.draw_keypoints(show_img, self.keypoints2, w) # 绘制直线连接距离小于阈值的两个特征点 for idx1 in np.where(self.mindist < self.max_distance)[0]: idx2 = self.idx_mindist[idx1] pos1 = self.keypoints1[int(idx1)].pt pos2 = self.keypoints2[int(idx2)].pt p1 = cv.Point(int(pos1.x), int(pos1.y)) p2 = cv.Point(int(pos2.x) + w, int(pos2.y)) cv.line(show_img, p1, p2, cv.CV_RGB(0, 255, 255), lineType=16) cv.imshow("SURF Demo", show_img)
def affine(self): self.img2 = cv.Mat() M = cv.asMat(self.m, force_single_channel=True) cv.warpAffine(self.img1, self.img2, M, self.img1.size(), borderValue=cv.CV_RGB(255, 255, 255))
def redraw(self): M = cv.asMat(self.m, force_single_channel=True) size = cv.Size(int(self.size[0, 0]), int(self.size[0, 1])) img2 = cv.Mat() if size.width > 0 and size.height > 0: cv.warpAffine(self.img, img2, M, size, borderValue=cv.CV_RGB(255, 255, 255)) cv.imshow("Affine Demo", img2)
def process(self, input_images, connected_outs): if len(input_images) == 0: return FAIL src = input_images['Input'] dist_res = int(self.getParamContent('Distance resolution')) angle_res = int(self.getParamContent('Angle resolution (degrees)')) acc_thresh = int(self.getParamContent('Accumulator threshold')) min_length = int(self.getParamContent('Minimum length')) max_gap = int(self.getParamContent('Maximum gap')) choice = self.getParamContent("Type of Hough transform") if src.ndim > 2: print "In '%s': The hough transform takes a binary image (or 8-bit) as input." % self.name return FAIL color_dst = numpy.empty((src.shape[0], src.shape[1], 3), dtype='uint8') pycv.cvtColor(pycv.asMat(src), pycv.asMat(color_dst), pycv.CV_GRAY2BGR) if choice == "Standard": lines = pycv.HoughLines(pycv.asMat(src), dist_res, pycv.CV_PI / angle_res, acc_thresh) margin = 0.04 n = 8 pi = math.pi h, w = src.shape[0:2] for i in range( min(len(lines), int(self.getParamContent("draw # lines")))): l = lines[i] rho = l[0] theta = l[1] if theta > 3 * pi / 4: theta -= pi if abs(rho) < w / n and abs(theta) < margin: pass elif abs(rho) > w - w / n and abs(theta) < margin: pass elif abs(rho) < h / n and abs(theta - pi / 2) < margin: pass elif abs(rho) > h - h / n and abs(theta - pi / 2) < margin: pass else: continue a = math.cos(theta) b = math.sin(theta) x0 = a * rho y0 = b * rho pt1 = pycv.Point(int(round(x0 + 2000 * (-b))), int(round(y0 + 2000 * (a)))) pt2 = pycv.Point(int(round(x0 - 2000 * (-b))), int(round(y0 - 2000 * (a)))) pycv.line( pycv.asMat(color_dst), pt1, pt2, pycv.CV_RGB(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), 2, 8) else: lines = pycv.HoughLinesP(pycv.asMat(src), dist_res, pycv.CV_PI / angle_res, acc_thresh, min_length, max_gap) for l in lines: pycv.line(pycv.asMat(color_dst), pycv.Point(int(l[0]), int(l[1])), pycv.Point(int(l[2]), int(l[3])), pycv.CV_RGB(*getRandColor()), 2, 8) self.lines = [(item[0], item[1]) for item in lines] return { self.output_names[0]: color_dst, self.output_names[1]: self.lines }
def draw_keypoints(self, img, keypoints, offset): for kp in keypoints: center = cv.Point(int(kp.pt.x) + offset, int(kp.pt.y)) cv.circle(img, center, int(kp.size * 0.25), cv.CV_RGB(255, 255, 0))
# 左键松开时,使用watershed进行图像分割 if event == cv.CV_EVENT_LBUTTONUP: seed += 1 tmp_markers = markers.clone() cv.watershed(img, tmp_markers) color_map = tmp_markers[:].astype(np.int) img3 = img2.clone() img4 = cv.asMat(palette[color_map]) cv.addWeighted(img3, 1.0, img4, mask_opacity, 0, img3) cv.imshow("Watershed Demo", img3) # 区域的颜色列表 marks_color = [ cv.CV_RGB(0, 0, 0), cv.CV_RGB(255, 0, 0), cv.CV_RGB(0, 255, 0), cv.CV_RGB(0, 0, 255), cv.CV_RGB(255, 255, 0), cv.CV_RGB(0, 255, 255), cv.CV_RGB(255, 0, 255), cv.CV_RGB(255, 255, 255) ] # 将颜色列表转换为调色板数组,只取前三个通道的值 palette = np.array([c.ndarray[:-1] for c in marks_color], dtype=np.uint8) seed = 1 # 从序号1开始设置区域颜色 mask_opacity = 0.5 # 绘制区域颜色的透明度