aks*_*tia 2 opencv convolution computer-vision lowpass-filter opencv-contour
我是计算机视觉的新手。因此,我不知道以下代码的内部实现,因此无法调试该错误。任何人都可以在以下代码中指出错误吗?
该代码使用Box Filter和Edge detection Kernel矩阵的组合将停车图像转换为二进制图像。然后我试图找到轮廓。现在我知道可以在二进制图像上找到轮廓,可以使用cv2.threshold()函数导出轮廓,从Filter和Kernel矩阵获得的图像也不是二进制图像吗?
import cv2
import numpy as np
import matplotlib.pyplot as plt
img=cv2.imread('parking spot1.jpg',1)
k3 = np.array(([-1,-1,-1],[-1,8,-1],[-1,-1,-1]))
low_filter = cv2.boxFilter(img, -1, (4,4))
output_low = cv2.filter2D(low_filter, -1, k3)
plt.subplot(2, 2, 1)
plt.imshow(img)
plt.title('Original Image')
plt.subplot(2, 2, 2)
plt.imshow(output_low)
plt.title('matrix1')
plt.show()
img, ret, heirarchy = cv2.findContours(output_low, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)
您认为我做错了什么?我非常感谢您对此问题的解释或指导。
非常感谢。
我面临的错误是:
()最近追溯(最近一次通话)----> 1 img,ret,heirarchy = cv2.findContours(output_low,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
错误:OpenCV(3.4.3)/io/opencv/modules/imgproc/src/contours.cpp:199:错误:(-210:不支持的格式或格式组合)[开始]当模式!=时,FindContours仅支持CV_8UC1图像。 CV_RETR_FLOODFILL否则仅在函数'cvStartFindContours_Impl中支持CV_32SC1图像
Please read the documentation of filtering properly. It is clearly stated that output has the same size and type as source. Therefore your output_low is three dimensional and cv2.findContours cannot be applied to it. You can take a threshold as you have stated or can simply convert output_low to grayscale and then find contours.
EDIT
I guess my answer was not clear in stating the difference between channels and the depth of the image. The image obtained from filter and kernel matrix is not necessarily one dimensional or grayscale. It has the same size as the input matrix and that means the number of channels is also equal. Therefore if your input image is in grayscale then the output will be one dimensional.
You can refer here to read more about the difference between channels and depth.
Therefore if you want the results of cv2.boxFilter and cv2.filter2D in grayscale just convert the original image to grayscale before applying filtering on it.
img = cv2.imread('opencv1.png', 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
k3 = np.array(([-1,-1,-1],[-1,8,-1],[-1,-1,-1]))
low_filter = cv2.boxFilter(gray, -1, (4,4))
output_low = cv2.filter2D(low_filter, -1, k3)
Run Code Online (Sandbox Code Playgroud)
Your error clearly states that FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images. The C1 in CV_8UC1 and CV_32SC1 denotes that the number of channels can only be one.