Python 轮廓近似OpenCV
我正在使用opencv for python。对于不好的形状,我们使用approxpolyDP() 当使用这个时,我只得到2个点,而不是一个合适的矩形 有人能帮我解释为什么会发生这种事吗Python 轮廓近似OpenCV,python,opencv,Python,Opencv,我正在使用opencv for python。对于不好的形状,我们使用approxpolyDP() 当使用这个时,我只得到2个点,而不是一个合适的矩形 有人能帮我解释为什么会发生这种事吗 import cv2 import numpy as np im = cv2.imread("badrect.png") img = im img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) canny = cv2.Canny(img,100,200) (_,cnts,_
import cv2
import numpy as np
im = cv2.imread("badrect.png")
img = im
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(img,100,200)
(_,cnts,_) = cv2.findContours(canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = cnts[0]
epsilon = 0.1*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
cv2.drawContours(im,approx,-1,(0,255,0),3)
cv2.imshow("img",im)
cv2.waitKey(0)
cv2.destroyAllWindows()
这就是结果的样子。
这就是我想要的结果
提前感谢!:) 问题如下: (1) 太糟糕了,我不得不减少
arcLength()*0.08
,而不是arcLength()*0.1
(2) 你把im和img搞混了,小心点
import cv2
import numpy as np
from matplotlib import pyplot as plt
path = "/Users/summing/Desktop/skM2L.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(ret, thresh) = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
edge = cv2.Canny(thresh, 100, 200)
(cnts, _) = cv2.findContours(edge.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
total = 0
for c in cnts:
epsilon = 0.08 * cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, epsilon, True)
cv2.drawContours(img, [approx], -1, (0, 255, 0), 4)
total += 1
print "I found {0} RET in that image".format(total)
cv2.imshow("Output", img)
cv2.waitKey(0)
exit()
还有我能找到的代码。希望能有所帮助。以下是。使用近似值作为数组。我希望这有帮助
cv2.drawContours(im,[approx],-1,(0,255,0),3)
请解释您的答案为何或如何解决原始海报的问题,这很有帮助。\ uCNTS=cv2.findContours(edge.copy(),cv2.RETR\u EXTERNAL,cv2.CHAIN\u近似值\u SIMPLE)值错误:太多值无法解包(预期为2)如何解决!!!