Beispiel #1
0
    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)
Beispiel #2
0
 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
Beispiel #3
0
    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 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]
Beispiel #6
0
 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()
Beispiel #7
0
def locatePlanarObject(ptpairs, src_corners, dst_corners):
    import numpy as _n

    n = len(ptpairs)
    if n < 4:
        return 0
    pt1 = cv.asMat(_n.array([cv.asndarray(pair[0]) for pair in ptpairs]))
    pt2 = cv.asMat(_n.array([cv.asndarray(pair[1]) for pair in ptpairs]))
    h = cv.findHomography(pt1, pt2, method=cv.RANSAC,
                          ransacReprojThreshold=5)[:]

    for i in range(4):
        x = src_corners[i].x
        y = src_corners[i].y
        Z = 1. / (h[2, 0] * x + h[2, 1] * y + h[2, 2])
        X = (h[0, 0] * x + h[0, 1] * y + h[0, 2]) * Z
        Y = (h[1, 0] * x + h[1, 1] * y + h[1, 2]) * Z
        dst_corners[i] = cv.Point(int(X), int(Y))

    return 1
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)
Beispiel #9
0
    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
        }
Beispiel #10
0
 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))
Beispiel #11
0
    ]

    # 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" \
            "Usage: find_obj [<object_filename> <scene_filename>]\n" \
            % (object_filename, scene_filename))
        exit(-1)
    object = cv.Mat(object_color.size(), cv.CV_8UC1)
    cv.cvtColor(object_color, object, cv.CV_BGR2GRAY)

    # corners
    src_corners = [
        cv.Point(0, 0),
        cv.Point(object.cols, 0),
        cv.Point(object.cols, object.rows),
        cv.Point(0, object.rows)
    ]
    dst_corners = [cv.Point()] * 4

    # find keypoints on both images
    surf = cv.SURF(500, 4, 2, True)
    mask = cv.Mat()
    tt = float(cv.getTickCount())
    objectKeypoints = cv.vector_KeyPoint()
    objectDescriptors = surf(object, mask, objectKeypoints)
    print("Object Descriptors: %d\n" % len(objectKeypoints))
    imageKeypoints = cv.vector_KeyPoint()
    imageDescriptors = surf(image, mask, imageKeypoints)