I have the following code (which is in fact just 1 part of 4 needed to run all the project I am working on..):
#python classify.py --model models/svm.cpickle
Used this code to do the job. It detects region of text/digits in images.
import cv2
image = cv2.imread("C:\\Users\\Bob\\Desktop\\PyHw\\images\\test5.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
_, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # get contours
idx =0
# for each contour found, draw a rectangle around it on original image
for contour in contours:
idx += 1
# get rectangle bounding contour
[x,y,w,h] = cv2.boundingRect(contour)
# discard areas that are too large
if h>300 and w>300:
continue
# discard areas that are too small
if h<40 or w<40:
continue
# draw rectangle around contour on original image
#cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
roi = image[y:y + h, x:x + w]
cv2.imwrite('C:\\Users\\Bob\\Desktop\\' + str(idx) + '.jpg', roi)
cv2.imshow('img',roi)
cv2.waitKey(0)
The code is based on this other question/answer: Extracting text OpenCV
This is a starter solution.
I don't have anything in Python for the time being but it shouldn't be hard to convert this plus the OpenCV function calls are similar and I've linked them below.
TLDR;
Find the centre of your boundingRects, then find the distance between them. If one rect is a certain threshold away, you may assume it as being a space.
First, find the centres of your bounding rectangles
vector<Point2f> centres;
for(size_t index = 0; index < contours.size(); ++index)
{
Moments moment = moments(contours[index]);
centres.push_back(Point2f(static_cast<float>(moment.m10/moment.m00), static_cast<float>(moment.m01/moment.m00)));
}
(Optional but recommended)
You can draw the centres to have a visual understanding of them.
for(size_t index = 0; index < centres.size(); ++index)
{
Scalar colour = Scalar(255, 255, 0);
circle(frame, circles[index], 2, colour, 2);
}
With this, just iterate through them confirming that the distance to the next one is within a reasonable threshold
for(size_t index = 0; index < centres.size(); ++index)
{
// this is just a sample value. Tweak it around to see which value actually makes sense
double distance = 0.5;
Point2f current = centres[index];
Point2f nextPoint = centres[index + 1];
// norm calculates the euclidean distance between two points
if(norm(nextPoint - current) >= distance)
{
// TODO: This is a potential space??
}
}
You can read more about moments, norm and circle drawing calls in Python.
Happy coding, Cheers mate :)