我正在为图像比较工作,我需要做一个模板。在
当前图像:
我可以为所需图像上色,但无法裁剪所需图像,彩色图像的代码如下:
import numpy as np
import cv2
img = cv2.imread('./org.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,1)
contours,h = cv2.findContours(thresh,1,2)
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
print (len(approx))
if len(approx)==5:
# print "pentagon"
cv2.drawContours(img,[cnt],0,255,-1)
elif len(approx)==3:
# print "triangle"
cv2.drawContours(img,[cnt],0,(0,255,0),-1)
elif len(approx)==4:
<