我以前发过here
我看过this post
尽管社区提供了大量信息,但我无法使用cv2.findContours()顺利跟踪图像。在上一篇文章中,我询问了如何生成样条曲线以平滑地跟踪曲线,我现在的重点是获得对象的平滑跟踪,而不管为轮廓生成了多少个点。我总是得到边缘参差不齐的结果:
我想要的输出与此类似,这是我在Adobe Illustrator中手动创建的:
我做过大量的模糊和阈值化实验,但一直无法得到一个平滑的轮廓。我运行的是openCV 3.3.0版。在
import numpy as np
import cv2
import math
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
print(cv2.__version__)
im = cv2.imread('img.jpg')
# orient the image properly
# grab the dimensions of the image and calculate the center
# of the image
(h, w) = im.shape[:2]
center = (w / 2, h / 2)
# rotate the image 180 degrees
M = cv2.getRotationMatrix2D(center, 180, 1.0)
rotated = cv2.warpAffine(im, M, (w, h))
# flip the image across
flippedColor = cv2.flip(rotated, 1) #for testing
imgray = cv2.cvtColor(rotated, cv2.COLOR_BGR2GRAY)
flipped = cv2.flip(imgray, 1)
(thresh, binRed) = cv2.threshold(flipped, 180, 255, cv2.THRESH_BINARY)
_, Rcontours, hier_r = cv2.findContours(binRed,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
r_areas = [cv2.contourArea(c) for c in Rcontours]
max_rarea = np.argmax(r_areas)
CntExternalMask = np.ones(binRed.shape[:2], dtype="uint8") * 255
contour= Rcontours[max_rarea]
cv2.drawContours(flippedColor,[contour],-1,(255,0,0),1)
这是我的结果。绿色轮廓为原始轮廓,红色轮廓为近似轮廓,灰色点为近似点。在
更多细节请参考我的另一个答案:Is there a function similar to OpenCV findContours that detects curves and replaces points with a spline?
我可以用这个代码向你展示这种效果。在
相关问题 更多 >
编程相关推荐