Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
857 views
in Technique[技术] by (71.8m points)

compare and cropping theimage in python using OpenCV

I have two images below :

Image1:

enter image description here

Image2:

enter image description here

I want to crop Image2 in the same manner as Image1. So if I read all the document of my folder then script should automatically crop the image in the same manner as in Image1. I am using the below code for cropping.

image = cv2.imread(path+'passport3.jpg')
y=280
x=0
h=373
w=546
crop = image[y:y+h, x:x+w]
cv2.imshow('Image', crop)
cv2.waitKey(0) 

However If I crop another image then this script does not work there.For example if I run the above script for below image then I am getting unexpected output :

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Resize the image to standard size before cropping because you are cropping with respect to constant position, height and width.

image=cv2.resize(image,(500,500))

I tried it with both of the given examples and its worked.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...