java - Text extraction and segmentation open CV -
i've never used opencv before, i'm trying write neural network system recognize text , need tool text extraction/ segmentation.
how can use java opencv preprocess , segmentate image containing text.
i don't need recognize text, need each letter in separate image.
something :
try code .no need of opencv
import java.awt.image.bufferedimage; import java.util.arraylist; import java.util.list; import org.neuroph.imgrec.imageutilities; public class charextractor { private int croptopy = 0;//up locked coordinate private int cropbottomy = 0;//down locked coordinate private int cropleftx = 0;//left locked coordinate private int croprightx = 0;//right locked coordinate private bufferedimage imagewithchars = null; private boolean endofimage;//end of picture private boolean endofrow;//end of current reading row /** * creates new char extractor soecified text image * @param imagewithchars - image text */ public charextractor(bufferedimage imagewithchars) { this.imagewithchars = imagewithchars; } public void setimagewithchars(bufferedimage imagewithchars) { this.imagewithchars = imagewithchars; } /** * method scans image pixels until finds first black pixel (todo: use foreground color black default). * when finds black pixel, sets croptopy , returns true. if reaches end of image , not find black pixels, * sets endofimage flag , returns false. * @return - returns true when black pixel found , croptopy value changed, , false if croptopy value not changed */ private boolean findcroptopy() { (int y = cropbottomy; y < imagewithchars.getheight(); y++) { // why cropydown? - multiple lines of text using cropbottomy previous line above; first line 0 (int x = cropleftx; x < imagewithchars.getwidth(); x++) { // scan starting previous left crop position - or shoud right??? if (imagewithchars.getrgb(x, y) == -16777216) { // if black rixel (also consider condition close black or not white or different background) this.croptopy = y; // save current y coordiante return true; // , return true } } } endofimage = true; //sets flag if no black pixels found return false; // , return false } /** * method scans image pixels until finds first row white pixels. (todo: background color white default). * when finds line whith white pixels, sets cropbottomy , returns true * @return - returns true when cropbottomy value set, false otherwise */ private boolean findcropbottomy() { (int y = croptopy + 1; y < imagewithchars.getheight(); y++) { // scan image top bottom int whitepixcounter = 0; //counter of white pixels in row (int x = cropleftx; x < imagewithchars.getwidth(); x++) { // scan pixels right starting left crop position if (imagewithchars.getrgb(x, y) == -1) { // if white pixel whitepixcounter++; // increase counter } } if (whitepixcounter == imagewithchars.getwidth()-1) { // if have reached end of line counting white pixels (x pos) cropbottomy = y;// means we've found white line, set current y coordinate minus 1 return true; // cropbottomy , finnish true } if (y == imagewithchars.getheight() - 1) { // if have reached end of image cropbottomy = y; // set crop bottom endofimage = true; // set corresponding endofimage flag return true; // , return true } } return false; // should never happen, possible if image has non white bg } private boolean findcropleftx() { int whitepixcounter = 0; // white pixel counter between letters (int x = croprightx; x < imagewithchars.getwidth(); x++) { // start previous righ crop position (previous letter), , scan following pixels right (int y = croptopy; y <= cropbottomy; y++) { // vertical pixel scan @ current x coordinate if (imagewithchars.getrgb(x, y) == -16777216) { // when find black pixel cropleftx = x; // set cropleftx return true; // , return true } } // bug?: condition looks strange.... might not need whitepixcounter @ all, might used 'i' letter whitepixcounter++; // if not black pixel assume white pixel if (whitepixcounter == 3) { // why 3 pixels? hard coded case , not work in general...!!! whitepixcounter = 0; // why sets zero, has no purporse @ all... } } endofrow = true; // if have reached end of row , have not found black pixels, set endofrow flag return false; // , return false } /** * method scans image pixels right until finds next row pixel white, y1 , y2. * @return - return true when x2 value changed , false when x2 value not changed */ private boolean findcroprightx() { (int x = cropleftx + 1; x < imagewithchars.getwidth(); x++) { // start current cropleftx position , scan pixels right int whitepixcounter = 0; (int y = croptopy; y <= cropbottomy; y++) { // vertical pixel scan @ current x coordinate if (imagewithchars.getrgb(x, y) == -1) { // if have white pixel @ current (x, y) whitepixcounter++; // increase whitepixcounter } } // space! int heightpixels = cropbottomy - croptopy; // calculate crop height if (whitepixcounter == heightpixels+1) { // if white pixel count equal crop height+1 white vertical line, means end of current char/ (+1 case when there 1 pixel; 'w' bug fix) croprightx = x; // set croprightx return true; // , return true } // why need when allready have condiiton in loop? - last letter in row. if (x == imagewithchars.getwidth() - 1) { // if have reached end of row x position croprightx = x; // set croprightx endofrow = true; // set endofrow flag return true; // , return true } } } public list<bufferedimage> extractcharimagestorecognize() { list<bufferedimage> trimedimages = new arraylist<bufferedimage>(); int = 0; while (endofimage == false) { endofrow = false; boolean foundtop = findcroptopy(); boolean foundbottom = false; if (foundtop == true) { foundbottom = findcropbottomy(); if (foundbottom == true) { while (endofrow == false) { boolean foundleft = false; boolean foundright = false; foundleft = findcropleftx(); if (foundleft == true) { foundright = findcroprightx(); if (foundright == true) { bufferedimage image = imageutilities.trimimage(imageutilities.cropimage(imagewithchars, cropleftx, croptopy, croprightx, cropbottomy)); trimedimages.add(image); i++; } } } cropleftx = 0; croprightx = 0; } } } croptopy = 0; cropbottomy = 0; endofimage = false; return trimedimages; } public static void main(string[] args) throws exception { file f=new file("./written.png"); bufferedimage img=imageio.read(f); charextractor ch=new charextractor(img); list<bufferedimage> list=ch.extractcharimagestorecognize(); for(int i=0;i<list.size();i++) { file outputfile = new file("./char_" +i+ ".png"); imageio.write(list.get(i),"png", outputfile); } } }
Comments
Post a Comment