简体   繁体   中英

Why is python OpenCV complaining about image not being single-channel 8bit?

I'm try to run the following code in python to detect lines in an image, but I'm getting an error complaining that the image is not an 8bit single channel image.

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 output:

(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

What am I doing wrong here?

你在cv.filter2D中为ddepth提供了cv2.CV_32F所以img_filtered可能是浮点数

我相信你应该传递CV_8U因为图像是灰度的:

img_filtered = cv2.filter2D(gray, cv2.CV_8U, gb_kernel.transpose())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM