2014-11-08 143 views
1

我有一张名片的图像,为了从图像中只提取卡片,我已经执行了透视变换。现在,我想把这个图像送到tesseract-ocr引擎。在此之前,我想提取包含一些文本的兴趣区域,并提供该区域而不是整个图像。我如何才能从卡中提取文本。如何在OpenCV中设置感兴趣的区域 - JAVA

这里是图像的例子:enter image description here

回答

2

这里是要为你做这个的代码。我首先找到图像上可用文本的轮廓,然后在实际图像上使用这些轮廓。

 Mat img_grayROI = Highgui.imread(perspective__transform_file, Highgui.CV_LOAD_IMAGE_GRAYSCALE); 

     Imgproc.GaussianBlur(img_grayROI, img_grayROI, new Size(15,15),50.00);Imgproc.THRESH_BINARY_INV, 15, 4); 

     Imgproc.threshold(img_grayROI, img_grayROI, -1, 255, Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU); 

     Imgproc.dilate(img_grayROI, img_grayROI, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2))); 

     Mat heirarchy= new Mat(); 
     Point shift=new Point(150,0); 

     List<MatOfPoint> contours = new ArrayList<MatOfPoint>();  

     Imgproc.findContours(img_grayROI, contours, heirarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); 
     double[] cont_area =new double[contours.size()]; 

     for(int i=0; i< contours.size();i++){ 
      if (Imgproc.contourArea(contours.get(i)) > 50){ 
       Rect rect = Imgproc.boundingRect(contours.get(i)); 
       cont_area[i]=Imgproc.contourArea(contours.get(i)); 

       if (rect.height > 25){ 
        Core.rectangle(result, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255)); 


        System.out.println(rect.x +"-"+ rect.y +"-"+ rect.height+"-"+rect.width); 
        Highgui.imwrite(ROI_file,result); 
       } 
      } 
     } 

perspective_transform是我需要找到ROI的源图像。

希望它有帮助。

相关问题