我试图编写一个python代码,使用OpenCV从输入图像中获取透视图。如何消除由此产生的错误?
这段代码是关于扭曲给定的图像,并使用hough transfom检测图像中的圆形对象(棋盘格) 下面是我的代码的输入文件我试图编写一个python代码,使用OpenCV从输入图像中获取透视图。如何消除由此产生的错误?,opencv,Opencv,这段代码是关于扭曲给定的图像,并使用hough transfom检测图像中的圆形对象(棋盘格) 下面是我的代码的输入文件 具有用于计算透视图的所需维度的JSON文件 { “规范董事会”:{ “tl_tr_br_bl”:[ [ 622, 85 ], [ 1477, 66 ], [ 1420, 835 ], [ 674, 837 ] ], “条形图宽度至棋盘格宽度”:0.716, “板宽到板高”:1.03, “管道长度到线路板高度”:0.36 } } 我的代码 #import necessary
#import necessary packages
import cv2
import json
import numpy as np
from operator import itemgetter
from glob import glob
#load file
input_file=open('3913.jpg.info.json', 'r')
json_decode = json.load(input_file)
result = []
result.append(json_decode['canonical_board']['tl_tr_br_bl'])
result.append(json_decode['canonical_board']['bar_width_to_checker_width'])
result.append(json_decode['canonical_board']['board_width_to_board_height'])
result.append(json_decode['canonical_board']['pip_length_to_board_height'])
print("tl_tr_br_bl:",result[0])
print("bar_width_to_checker_width:",result[1])
print("board_width_to_board_height",result[2])
print("pip_length_to_board_height",result[3])
normal_img = cv2.imread('3913.jpg')
pts1 = np.float32([[454, 83], [1240, 79], [1424, 808], [275, 842]])
pts2 = np.array([[0.397],[0.986],[0.402]], dtype=np.float32)
M = cv2.getPerspectiveTransform(pts1.astype(np.float32), pts2)
dst = cv2.warpPerspective(normal_img, M, (1300, 800))
#perspective of the original image shown
cv2.imshow(dst)
#converting the image into grayscale
gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
#locating the circles using hough transform
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)
# ensure at least some circles were found
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
no_of_circles = len(circles)
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.imshow("output", np.hstack([image, output]))
cv2.waitKey(0)
#number of circles
print("number of circles detected-",no_of_circles)
我收到的错误
error Traceback (most recent call last)
<ipython-input-12-efcd2ec83d0c> in <module>
37 pts2 = np.array([[0.397],[0.986],[0.402]], dtype=np.float32)
38
---> 39 M = cv2.getPerspectiveTransform(pts1.astype(np.float32), pts2)
40
41 dst = cv2.warpPerspective(normal_img, M, (1300, 800))
error: OpenCV(4.1.2) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/imgwarp.cpp:3391: error: (-215:Assertion failed) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'getPerspectiveTransform'
错误回溯(最近一次调用上次)
在里面
37 pts2=np.array([[0.397]、[0.986]、[0.402]],dtype=np.float32)
38
--->39 M=cv2.getPerspectiveTransform(pts1.astype(np.float32),pts2)
40
41 dst=cv2.0(正常值,M,(1300,800))
错误:OpenCV(4.1.2)/Users/travis/build/skvark/OpenCV-python/OpenCV/modules/imgproc/src/imgwarp.cpp:3391:错误:(-215:断言失败)src.checkVector(2,CV_32F)==4&&dst.checkVector(2,CV_32F)==4在函数“getPerspectiveTransform”中
您的pts2阵列错误。应该是四分,而不是三分。点需要是二维的,而不是一维的。为什么pts2浮点值小于1?我想你一定有整数值。因此,您可能需要缩放dst坐标系。输出图像数据类型必须与输入图像数据类型相同。当然可以,但您能建议对代码进行任何更改以解决此问题吗?