I have used this question to create those boxes, but I am not good with python. How do I now create 3 images from the 3 boxes that i have created? I searched the internet and i can't find the right answer. Thanks!
Asked
Active
Viewed 648 times
0
-
Please check [this](https://stackoverflow.com/a/15589825/8353711). This is what you required? – shaik moeed May 20 '19 at 06:11
-
I am not very good with python as I said. When I try this code i get `NameError: name 'y' is not defined` – Dimitar May 20 '19 at 06:16
-
Those are dimensions of image you want to crop. – shaik moeed May 20 '19 at 06:18
-
I have no idea how to get the dimensions of the boxes – Dimitar May 20 '19 at 06:49
1 Answers
1
Replace the below code. I have followed the link you have provided in question.
var=1
for contour in contours:
convex_contour = cv2.convexHull(contour)
area = cv2.contourArea(convex_contour)
if area > AREA_THRESHOLD:
cv2.drawContours(img, [convex_contour], -1, (255,0,0), 3)
# get rectangle bounding contour
[x,y,w,h] = cv2.boundingRect(contour)
crop_img = img[y:y+h, x:x+w]
cv2.imwrite("crop"+str(var)+".png", crop_img)
var+=1
This will save the cropped images in current program running location in .png
format

shaik moeed
- 5,300
- 1
- 18
- 54
-
that works great the only problem i have right now is that the `var` variable is not consistent. What i mean is that first image is the box on the bottom right and then the box on the left side and then the box on the right upper part, is there a way to keep the boxes from left-top to right-bottom, so the code can read them in order after that? thanks! – Dimitar May 20 '19 at 07:37
-
`var` is used just to give naming for files. The order of cropped images is depend on how the `contours` are sorted. – shaik moeed May 20 '19 at 08:37