无法在opencv中选择图像角落

时间:2016-08-31 12:04:01

标签: python opencv

我正在尝试选择图像角落并裁剪它,然后对其进行透视变换。

但是当我运行此代码时,带有图像的窗口会打开,但是双击,没有任何反应,所以我双击,我可以选择图像角落。

这是我的代码

from transform import four_point_transform
import imutils
from skimage.filters import threshold_adaptive
import numpy as np
import cv2

image = cv2.imread("test1.jpg")
ratio = image.shape[0] / 500.0
orig = image.copy()


window_name = "Select corner points of object"
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name, image)

pts_1 = []

def callback(event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDBLCLK:
                pts_1.append((x, y))
                cv2.circle(image,(x,y),10,(0,255,0),5)
                cv2.imshow(window_name, image)
                if(len(pts_1)==4):
                        pts = np.array(pts_1,dtype="float32")
                        print(pts_1)
                        warped = four_point_transform(orig, pts)
                        warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
                        warped = threshold_adaptive(warped, 251, offset = 10)
                        warped = warped.astype("uint8") * 255
                        cv2.imshow("Original", imutils.resize(orig, height = 650))
                        cv2.imshow("Scanned", imutils.resize(warped, height = 650))
                        cv2.waitKey(0)
cv2.setMouseCallback(window_name, callback)
key = cv2.waitKey(0)

任何帮助都会受到赞赏吗?

0 个答案:

没有答案