character detection and crop from an image using opencv python
<p>I have a project in which I have to detect Bengali numbers from image. I decided to do an experiment like numbers with spaces and without spaces. My python program can detect all number from with spaces image.</p>

<p>The problem occurred when I gave image without spaces. It couldn't cut number smoothly like the previous one. </p>

<p>here is my code</p>

<pre class="lang-python prettyprint-override"><code>import cv2

image = cv2.imread("number.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 
_,thresh = cv2.threshold(gray,70,255,cv2.THRESH_BINARY_INV) 
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 0) 
_,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) 

i=5
for contour in contours:

    [x,y,w,h] = cv2.boundingRect(contour)

    cv2.imwrite(str(i)+".jpg",image[y:y+h,x:x+h])
    i=i+1</code></pre>

<p>At first I used dilated in for finding contours but it didn't work for number without space image. Then I use directly thresh output and after that I got most of the numbers but I couldn't cut them perfectly because contour area detect number with some portion of other number. Though it didn't have space in 2nd image but still 2 numbers didn't touch them each other. So why the output like this?</p>

<h2>With Space:</h2>

<p><img src="https://i.stack.imgur.com/lpQYZ.png" alt="with space"></p>

<h2>Without Space:</h2>

<p><img src="https://i.stack.imgur.com/PEW7H.png" alt="without space"> </p>

<p>Unfortunately I didn't notice that when I cut rectangle portion I added x:x+h instead of x:x+w. That's the main problem. After modifying that the program worked fine. sorry.</p>

python opencv ocr opencv-contour