Sunday, 18 March 2018

Text separation using OpenCv python

       

import numpy as np
import time
import cv2

# Load an color image in grayscale
img = cv2.imread('1.PNG',0)
cv2.imshow('Realimage',img)
Img_height = np.size(img, 0)
Img_width = np.size(img, 1)
print(Img_height)
print("-----------------------------------------")
imgray = img
thresh = cv2.adaptiveThreshold(imgray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)#cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0,255,0), 3)
#cnt = contours[4]
for cnt in contours:
 #cv2.drawContours(img, [cnt], 0, (128,255,0), 2)
 x,y,w,h = cv2.boundingRect(cnt)
 img =cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
 crop_img = img[y:y+h, x:x+w]
 height_sub = np.size(crop_img, 0)
 width_sub = np.size(crop_img, 1)
 print(height_sub)
 if Img_height/2 <height_sub and Img_width/2 > width_sub:
     cv2.imwrite('./dataIMG/'+ str(time.time())+'.png',crop_img)
 
cv2.imshow('image',imgray)

cv2.waitKey(0)
cv2.destroyAllWindows()




Input

Output