我想通过将厚度设置为-1来填充区域。结果令人难以置信:第二个轮廓被填充,但第一个轮廓没有被填充。
var result= new Image<Bgr, byte>(source.Width, source.Height);
result.SetValue(new Bgr(255,255,255));
VectorOfVectorOfPoint contours =new VectorOfVectorOfPoint();
var temp = source.Canny(128, 255);
CvInvoke.FindContours(temp , contours,null, RetrType.External,ChainApproxMethod.ChainApproxSimple);
CvInvoke.DrawContours(result, contours, 1, new MCvScalar(0,255,0),-1);
CvInvoke.DrawContours(result, contours, 0, new MCvScalar(0,255, 0), -1);
result =result.GetSubRect(getMaxRectangle(contours))
我尝试使用不同的
LineType
但失败了,尤其是LineType.Filled
导致错误:OpenCV: connectivity == 8 || connectivity == 4
。我想知道为什么会这样。
我会用Python编写代码,如果它对你有帮助的话:
gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) # convert to gray
_, threshold = cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY_INV) # inverse thresholding
contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # find contours
imContoured = cv2.drawContours(im.copy(), contours, 0, (0,0,255), -1) # fill it blue (in cv it will be red)
imContoured = cv2.drawContours(imContoured, contours, 1, (0,255,0), -1) # fill it green
这是结果:
所以基本上不需要做Canny边缘检测,你的图像适合一些阈值(记得使用
cv2.THRESH_BINARY_INV
)