def on_timer(self, *args): if not self.need_redraw: return self.need_redraw = False self.mask.fill(0) length = len(self.lasso_selection.dataspace_points) if length == 0: return def convert_poly(poly): tmp = cv.asvector_Point2i(poly) return cv.vector_vector_Point2i([tmp]) # 在遮罩数组上绘制套索多边形 for poly in self.lasso_selection.disjoint_selections: poly = poly.astype(np.int) print poly.shape cv.fillPoly(self.mask_img, convert_poly(poly), cv.Scalar(1, 1, 1, 1)) poly = N - poly # 绘制对称多边形 cv.fillPoly(self.mask_img, convert_poly(poly), cv.Scalar(1, 1, 1, 1)) # 更新遮罩图像 self.data["mask_img"] = self.mask # 更新滤波图像 data = self.data["filtered_img"] data[:] = fft.ifft2(self.fimg * fft.fftshift(self.mask)).real self.data["filtered_img"] = data
def mouse_call_back(event, x, y, flags, user_data): global seed # 右键松开时,初始化种子图像 if event == cv.CV_EVENT_RBUTTONUP: img2[:] = img[:] markers[:] = 0 seed = 1 cv.imshow("Watershed Demo", img2) if seed == len(marks_color): return # 左键按下时,在种子图像上添加种子 if flags == cv.CV_EVENT_FLAG_LBUTTON: pt = cv.Point(x, y) cv.circle(markers, pt, 5, cv.Scalar(seed, seed, seed, seed), cv.CV_FILLED) cv.circle(img2, pt, 5, marks_color[seed], cv.CV_FILLED) cv.imshow("Watershed Demo", img2) # 左键松开时,使用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)
def redraw(self): img = self.img.clone() cv.floodFill(img, cv.Point(*self.point), cv.Scalar(255, 0, 0, 255), loDiff=cv.asScalar(self.lo_diff[0]), upDiff=cv.asScalar(self.hi_diff[0]), flags=self.option_) self.data["img"] = img[:, :, ::-1]
def painter_updated(self): for _, _, x, y in self.painter.track: # 在储存选区的mask上绘制圆形 cv.circle(self.mask, cv.Point(int(x), int(y)), int(self.painter.r), cv.Scalar(255, 255, 255, 255), thickness=-1) # 宽度为负表示填充圆形 self.inpaint() self.painter.track = [] self.painter.request_redraw()
w, h = img.size().width, img.size().height def blend(img, img2): """ 混合两幅图像, 其中img2有4个通道 """ #使用alpha通道计算img2的混和值 b = img2[:,:,3:] / 255.0 a = 1 - b # img的混合值 #混合两幅图像 img[:,:,:3] *= a img[:,:,:3] += b * img2[:,:,:3] img2[:] = 0 for i in xrange(0, w, w/10): cv.line(img2, cv.Point(i,0), cv.Point(i, h), cv.Scalar(0, 0, 255, i*255/w), 5) blend(img, img2) img2[:] = 0 for i in xrange(0, h, h/10): cv.line(img2, cv.Point(0,i), cv.Point(w, i), cv.Scalar(0, 255, 0, i*255/h), 5) blend(img, img2) cv.namedWindow("Draw Demo") cv.imshow("Draw Demo", img) cv.waitKey(0)
return 1 if __name__ == '__main__': if len(sys.argv) == 3: object_filename = sys.argv[1] scene_filename = sys.argv[2] else: object_filename = "box.png" scene_filename = "box_in_scene.png" cv.namedWindow("Object", 1) cv.namedWindow("Object Correspond", 1) colors = [ cv.Scalar(0, 0, 255), cv.Scalar(0, 128, 255), cv.Scalar(0, 255, 255), cv.Scalar(0, 255, 0), cv.Scalar(255, 128, 0), cv.Scalar(255, 255, 0), cv.Scalar(255, 0, 0), cv.Scalar(255, 0, 255), cv.Scalar(255, 255, 255), ] # read the two images object_color = cv.imread(object_filename, cv.CV_LOAD_IMAGE_COLOR) image = cv.imread(scene_filename, cv.CV_LOAD_IMAGE_GRAYSCALE) if not object_color or not image: print("Can not load %s and/or %s\n" \