I am near new to OpenCV world. I am working on a project which need (for now) to detect numbers in an image, select them and save.
This is the code I used:
# Importing modules
import cv2
import numpy as np
# Read the input image
im = cv2.imread('C:\\Users\\User\\Desktop\\test.png')
# Convert to grayscale and apply Gaussian filtering
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_gray = cv2.GaussianBlur(im_gray, (5, 5), 0)
# Threshold the image
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
# Find contours in the image
image, ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Bounding rectangle for a set of points
i = 0
#rects = [cv2.boundingRect(ctr) for ctr in ctrs]
#rects.sort()
for ctr in ctrs:
x, y, w, h = cv2.boundingRect(ctrs[i])
# Getting ROI
roi = im[y:y+h, x:x+w]
#cv2.imshow('roi',roi)
#cv2.waitKey()
i += 1
cv2.imwrite('C:\\Users\\User\\Desktop\\crop\\' + str(i) + '.jpg', roi)
#print(rects)
print("OK - NO ERRORS")
It works a half. The problem is the output numbers (in image format, it need to be that way) aren't ordered by the original image (below).
This is the output:
What is wrong in the code ?
Also, you can note the rects
variable. I used it to do some debug and I noted an interesting thing: if I sort it's content, in console the array of images order is right.
Is there a way to sort the images in the original order ?
I also saw this very similar post but I can't understand the solution.
Thank you very much.