Hello. There is anyway that I can crop a defined area in contours ? I’m trying to crop a license car plate, but all algorithms that I tried work sometimes cropping exactly area of license car, sometimes contours find the right rectangle but it crops the wrong one, if I have x and y approximately could I do anything better ? Thanks all
please present your data and code. then we have something to work with.
and there’s no need to tag this discussion “opencv”. the whole forum is opencv.
Right. The code works, but when a start getting lots of images, the percents of cropped plates is too low. So I thought setting the size of plate image could be a good idea.
img = cv2.imread('3.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
contours,h = cv2.findContours(thresh,1,2)
largest_rectangle = [0,0]
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
if len(approx) == 4:
area = cv2.contourArea(cnt)
if area > largest_rectangle[0]:
largest_rectangle = [cv2.contourArea(cnt), cnt, approx]
x, y, w, h = cv2.boundingRect(largest_rectangle[1])
roi = img[y: y + h, x : x + w]
plt.imshow(roi, cmap = 'gray')
plt.show()
The plate shape is between 35, 80 and 145, 285
and the data please.
okay, so your code gives you upright crops of the license plate… and what don’t you like about that? I hate having to guess. please clarify clearly what you need. show an example (picture) if you need to. that’s clearer than a verbal attempt.
The most of times it gets those pictures below, but sometime it works as well, and a don’t know why is happening that. Maybe setting the size of plate shape could be the solution, but I don’t know how to do it.
looks like a bad threshold. look at the binarized image (output of threshold).