为什么python OpenCV抱怨图像不是单通道8bit?

为什么python OpenCV抱怨图像不是单通道8bit?,python,opencv,image-processing,Python,Opencv,Image Processing,我试图用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.tra

我试图用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
(1440993)
OpenCV错误:cvHoughLines2文件/Users/ericchaves/Projects/OpenCV env/OpenCV-2.4.7/modules/imgproc/src/hough.cpp第712行中的参数错误(源图像必须是8位单通道)
回溯(最近一次呼叫最后一次):
文件“detect lines.py”,第22行,在
lines=cv2.HoughLinesP(阈值,1,np.pi/180200800,0)
cv2.error:/Users/ericchaves/Projects/opencv-env/opencv-2.4.7/modules/imgproc/src/hough.cpp:712:error:(-5)源图像必须是8位的单通道函数cvHoughLines2

我做错了什么?

您在中为ddepth指定了cv2.CV_32F,因此img_filtered可能是float

我认为您应该通过
CV_8U
,因为图像是灰度图像:

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

图像应为灰度,类型为np.uint8。所以把它转换成np.uint8。您的图像可能是np.32。您可以通过
print thresh.dtype
检查它,我需要
np.uint8
而不是
np.int8
——谢谢!