【发布时间】:2019-07-09 06:17:54
【问题描述】:
我正在为需要制作模板的图像比较工作。
当前图片:
我可以为所需的图像上色,但无法裁剪所需的图像,颜色图像的代码如下:
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:
# print "square"
cv2.drawContours(img,[cnt],0,(0,0,255),-1)
elif len(approx) == 9:
# print "half-circle"
cv2.drawContours(img,[cnt],0,(255,255,0),-1)
elif len(approx) > 15:
# print "circle"
cv2.drawContours(img,[cnt],0,(0,255,255),-1)
cv2.imwrite('./test/Image_crop.jpg', img)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
- 我想裁剪 红色 的图像。
- 使用不规则尺寸保存裁剪图像。
- 除了 cv2.drawContours 还有其他方法可以获取所需的图像吗
使用 python 帮助我解决问题。
【问题讨论】:
-
保存不规则尺寸的裁剪图像 ?我认为这是不可能的,因为图像通常被认为是
mxn矩阵但是您可以使用 alpha 通道来隐藏图像的某些部分,但它总是会是完美的矩形。 -
@ZdaR 我会试试 alpha 通道
-
看看this post。
标签: python opencv image-processing crop image-compression