gpt4 book ai didi

python - 如何裁剪最大的矩形并应用环绕变换

转载 作者:行者123 更新时间:2023-12-02 17:37:17 25 4
gpt4 key购买 nike

嗨,有人可以帮我调试我的代码吗?我正在裁剪最大的矩形并将其应用于变换。我上传了图片,因此您可以看到它。我在树莓派和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

这是我得到的错误

追溯(最近一次通话):
在第11行的文件“crop.py”中
等高线,层次结构= cv2.findContours(edges,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
ValueError:太多值无法解包

最佳答案

看一下文档,findContours返回三个值,用法如下:

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

关于python - 如何裁剪最大的矩形并应用环绕变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49546486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com