Ask Your Question
0

[SOLVED]I have downgraded python from 3.7 to 3.5,but my cropping code for image pre-processing doesn't seem to work with the lesser version

asked 2019-05-17 04:18:20 -0500

LEENA gravatar image

updated 2019-05-20 06:58:22 -0500

supra56 gravatar image

this is my code

 import cv2
 import numpy as np
img1 = cv2.imread('C:\\Users\\LENOVO\\assertive and mild\\111.jpeg')
img = cv2.imread('C:\\Users\\LENOVO\\assertive and mild\\111.jpeg',0)
gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)

# Create mask
height,width = 137,137
mask = np.zeros((height,width), np.uint8)

edges = cv2.Canny(thresh, 100, 200)
#cv2.imshow('detected ',gray)
cimg=cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 10000, param1 = 50, param2 = 30, minRadius = 0, maxRadius = 0)
for i in circles[0,:]:
i[2]=i[2]+4
# Draw on mask
cv2.circle(mask,(i[0],i[1]),i[2],(255,255,255),thickness=-1)

 # Copy that image using that mask
 masked_data = cv2.bitwise_and(img1, img1, mask=mask)

 # Apply Threshold
_,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY)

# Find Contour
contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h = cv2.boundingRect(contours[0])

# Crop masked_data
crop = masked_data[y:y+h,x:x+w]

#Code to close Window
cv2.imshow('detected Edge',img1)
cv2.imshow('Cropped face',crop)

cv2.waitKey(0)
cv2.imwrite('C:\\Users\\LENOVO\\assertive and mild\\cropped111.jpeg',crop)
cv2.destroyAllWindows()

it is giving the follwoing error on running the code: > TypeError

<ipython-input-5-1cb58adf5ed7> in <module>
 29 # Find Contour
 30 contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
 ---> 31 x,y,w,h = cv2.boundingRect(contours[0])
 32 
 33 # Crop masked_data

TypeError: Expected cv::UMat for argument 'array'

this code was producing the right results on python 3.7 but for tensorflow i had to downgrade it to python 3.5 but now this is giving me errors.

edit retag flag offensive close merge delete

Comments

OpenCV version?

supra56 gravatar imagesupra56 ( 2019-05-18 07:05:03 -0500 )edit
1

the version is 4.1.0

LEENA gravatar imageLEENA ( 2019-05-18 13:19:07 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-05-19 22:26:22 -0500

supra56 gravatar image

updated 2019-05-19 22:26:54 -0500

You are using OpenCV 4.1.0. The error telling you that you must add array at the end of findContours Try this:

contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]
edit flag offensive delete link more

Comments

1

thanks @supra56 it worked!

LEENA gravatar imageLEENA ( 2019-05-20 05:44:21 -0500 )edit

Used python 3.7 that will work too.

supra56 gravatar imagesupra56 ( 2019-05-20 07:00:43 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-05-17 04:18:20 -0500

Seen: 78 times

Last updated: May 20