Reputation: 41
hi can anyone help me to debug my codes i'm working on cropping the biggest rectangle and applying transformation to it. I uploaded the picture so you can see it. im using python 2.7 on raspberry pi and opencv 3.3.0
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)
this is my code but i always getting and error like this.
this is the error i got
Traceback (most recent call last): File "crop.py", line 11, in contours,hierarchy = cv2.findContours(edges,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) ValueError: too many values to unpack
Upvotes: 0
Views: 483
Reputation: 25154
Look at the docs, findContours returns three values, use as following:
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
Upvotes: 1