我尝试在python中运行以下代码来检测图像中的行,但是我收到一个错误,抱怨图像不是8位单通道图像。
img = cv2.imread("source.jpg")
gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)
gb_kernel = cv2.getGaborKernel((ks, ks),sig,th,lm,0,0,cv2.CV_32F)
img_filtered = cv2.filter2D(gray, cv2.CV_32F, gb_kernel.transpose())
retval, thresh = cv2.threshold(img_filtered, 254, 255, cv2.THRESH_BINARY_INV)
print thresh.shape
lines = cv2.HoughLinesP(thresh, 1, np.pi/180, 200, 800, 0)
python输出:
(1440, 993)
OpenCV Error: Bad argument (The source image must be 8-bit, single-channel) in cvHoughLines2, file /Users/ericchaves/Projects/opencv-env/opencv-2.4.7/modules/imgproc/src/hough.cpp, line 712
Traceback (most recent call last):
File "detect-lines.py", line 22, in <module>
lines = cv2.HoughLinesP(thresh, 1, np.pi/180, 200, 800, 0)
cv2.error: /Users/ericchaves/Projects/opencv-env/opencv-2.4.7/modules/imgproc/src/hough.cpp:712: error: (-5) The source image must be 8-bit, single-channel in function cvHoughLines2
我在这里做错了什么?
答案 0 :(得分:1)
你在cv.filter2D为ddepth提供了cv2.CV_32F,所以img_filtered可能是浮动的
答案 1 :(得分:1)
我相信你应该通过CV_8U
,因为图像是灰度的:
img_filtered = cv2.filter2D(gray, cv2.CV_8U, gb_kernel.transpose())