【问题标题】:How to set Region of Interest in OpenCV - JAVA如何在 OpenCV 中设置感兴趣区域 - JAVA
【发布时间】:2014-11-08 05:22:46
【问题描述】:

我有一张名片的图像,我在其上执行了透视变换,以便仅从图像中提取名片。现在,我想将此图像提供给 tesseract-ocr 引擎。在此之前,我想提取有一些文本的感兴趣区域并提供它而不是整个图像。如何仅从卡片中提取文本。

以下是图片示例:

【问题讨论】:

    标签: java opencv ocr tesseract


    【解决方案1】:

    这是将为您执行此操作的代码。我首先找到图像上可用文本的轮廓,然后在实际图像上使用这些轮廓。

         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 的源图像。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 2012-04-25
      • 2018-01-15
      • 2013-02-28
      • 2017-09-28
      相关资源
      最近更新 更多