如何裁剪最大的矩形并应用包裹变换

时间:2018-03-29 01:05:33

标签: python opencv raspberry-pi

嗨,任何人都可以帮我调试我的代码我正在裁剪最大的矩形并对其应用转换。我上传了图片,你可以看到它。我在raspberry pi和opencv 3.3.0上使用python 2.7

enter image description here

import cv2
import os
import numpy as np

im = cv2.imread('image.png')

# Use a blurring effect, to (hopefully) remove these high frequency 
#noises.

image_blurred = cv2.GaussianBlur(im,(3,3),0)



#apply a canny edge-detector

edges = cv2.Canny(image_blurred,100,300,apertureSize = 3)



#finding the contours in the image

contours,hierarchy = cv2.findContours(edges,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)



#to find the biggest rectangle. For each contour cnt, first find the 
#convex hull, then use approaxPolyDP to simplify the contour as much as 
#possible.

hull = cv2.convexHull(cnt)
simplified_cnt = cv2.approxPolyDP(hull,0.001*cv2.arcLength(hull,True),True)



#after finding (hopefully) the right quadrilateral, is transforming back 
#to a rectangle. For this you can use findHomography to come up with a 
#transformation matrix.

(H,mask) = cv2.findHomography(cnt.astype('single'),np.array([[[0., 0.]],[[2150., 0.]],[[2150., 2800.]],[[0.,2800.]]],dtype=np.single))



#for the final tranformation on crop image using warpPerspective

final_image = cv2.warpPerspective(image,H,(2150, 2800))

cv2.imshow("Show",final_image)

cv2.waitKey(0)

这是我的代码,但我总是得到这样的错误。

enter image description here

这是我得到的错误

追踪(最近一次通话):   文件" crop.py",第11行,in     contours,hierarchy = cv2.findContours(edges,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) ValueError:要解压缩的值太多

1 个答案:

答案 0 :(得分:1)

查看文档,findContours返回三个值,使用如下:

im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)