在 python 中使用 cv2.approxPolyDP() 绘制等高线
Drawing contours using cv2.approxPolyDP() in python
我无法使用 cv2.approxPolyDP()
绘制形状的完整轮廓。
我得到以下结果:
但我想要这样的输出:
这是我的代码:
import cv2
im = cv2.imread('C:\Python27\Test\Targets\s1.jpg') # read picture
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # BGR to grayscale
ret, thresh = cv2.threshold(imgray, 200, 255, cv2.THRESH_BINARY)
countours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
epsilon = 0.1 * cv2.arcLength(countours[0], True)
approx = cv2.approxPolyDP(countours[0], epsilon, True)
cv2.drawContours(im, approx, -1, (0, 255, 0), 3)
cv2.imshow("Contour", im)
cv2.waitKey(0)
cv2.destroyAllWindows()
我尝试了相同的代码。但是我能够得到这个:
在 Java 这行得通
MatOfPoint2f newContour = new MatOfPoint2f();
largestContour.convertTo(newContour, CvType.CV_32F);
double arcLength = Imgproc.arcLength(newContour, closed);
double epsilon = 0.2 * arcLength;
MatOfPoint2f approxCurve = new MatOfPoint2f();
Imgproc.approxPolyDP(newContour, approxCurve, epsilon, closed);
// convert back to form that can be plotted by drawContours
MatOfPoint largestOutline = new MatOfPoint();
approxCurve.convertTo(largestOutline, CvType.CV_32S);
cv2.CHAIN_APPROX_SIMPLE
去除所有冗余点并压缩轮廓,从而节省内存。如果您将 findContours()
函数传递给 cv2.CHAIN_APPROX_NONE
参数而不是 cv2.CHAIN_APPROX_SIMPLE
,您的问题将得到解决。
您的代码应更改如下:
_, countours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_NONE)
很久以前的事了,但我建议你这样
contours,hierarchy = cv2.findContours(thresh, 1, 2)
contours_sizes= [(cv2.contourArea(cnt), cnt) for cnt in contours]
biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]
countours = biggest_contour
这将找到图像的最大轮廓,忽略小点和噪声
我无法使用 cv2.approxPolyDP()
绘制形状的完整轮廓。
我得到以下结果:
但我想要这样的输出:
这是我的代码:
import cv2
im = cv2.imread('C:\Python27\Test\Targets\s1.jpg') # read picture
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # BGR to grayscale
ret, thresh = cv2.threshold(imgray, 200, 255, cv2.THRESH_BINARY)
countours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
epsilon = 0.1 * cv2.arcLength(countours[0], True)
approx = cv2.approxPolyDP(countours[0], epsilon, True)
cv2.drawContours(im, approx, -1, (0, 255, 0), 3)
cv2.imshow("Contour", im)
cv2.waitKey(0)
cv2.destroyAllWindows()
我尝试了相同的代码。但是我能够得到这个:
在 Java 这行得通
MatOfPoint2f newContour = new MatOfPoint2f();
largestContour.convertTo(newContour, CvType.CV_32F);
double arcLength = Imgproc.arcLength(newContour, closed);
double epsilon = 0.2 * arcLength;
MatOfPoint2f approxCurve = new MatOfPoint2f();
Imgproc.approxPolyDP(newContour, approxCurve, epsilon, closed);
// convert back to form that can be plotted by drawContours
MatOfPoint largestOutline = new MatOfPoint();
approxCurve.convertTo(largestOutline, CvType.CV_32S);
cv2.CHAIN_APPROX_SIMPLE
去除所有冗余点并压缩轮廓,从而节省内存。如果您将 findContours()
函数传递给 cv2.CHAIN_APPROX_NONE
参数而不是 cv2.CHAIN_APPROX_SIMPLE
,您的问题将得到解决。
您的代码应更改如下:
_, countours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_NONE)
很久以前的事了,但我建议你这样
contours,hierarchy = cv2.findContours(thresh, 1, 2)
contours_sizes= [(cv2.contourArea(cnt), cnt) for cnt in contours]
biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]
countours = biggest_contour
这将找到图像的最大轮廓,忽略小点和噪声