반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

opencv 텍스트 영역 감지 본문

OpneCV

opencv 텍스트 영역 감지

닉의네임 2021. 1. 7. 14:32
반응형

텍스트 영역을 감지해보즈아

개발환경

  • java14
  • opencv 4.5.0

실행결과 

텍스트영역 감지

package com.etoos.imagesearch.sample;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TextPyImage {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        String filePath = "/Users/doo/project/imagesearch/common/temp/";
        String file = "1-1.png";

        Mat img = Imgcodecs.imread(filePath + file);
        Mat rgb = new Mat();
        Mat grad = new Mat();

//        Imgproc.pyrDown(img, img);
        Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY);

        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3, 3));
        Imgproc.morphologyEx(img, grad, Imgproc.MORPH_GRADIENT, kernel);

        Mat bw = new Mat();
        Imgproc.threshold(grad, bw, 0.0, 255.0, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);

//        Mat kernel2 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9, 1));
        kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9, 1));
        Mat connected = new Mat();

        Imgproc.morphologyEx(bw, connected, Imgproc.MORPH_CLOSE, kernel);

        Mat connectedCopy = new Mat();
        connected.copyTo(connectedCopy);

        List<MatOfPoint> contours = new ArrayList<>();
        Mat hierarchy = new Mat();
        Imgproc.findContours(connectedCopy, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

        System.out.println("--------------");
        System.out.println(contours.size());

        Iterator<MatOfPoint> each = contours.iterator();
        int cut = 0;

        each = contours.iterator();
        while (each.hasNext()) {
            MatOfPoint contour = each.next();
            Rect rect = Imgproc.boundingRect(contour);
            Imgproc.drawContours(bw, contours, -1, new Scalar(255,255,255));
            float r = Core.countNonZero(bw) / (rect.width * rect.height);

//            System.out.println("rect : " + rect);
//            System.out.println("tl : " + rect.tl());
//            System.out.println("br : " + rect.br());
//            System.out.println("height : " + rect.height);
//            System.out.println("width : " + rect.width);
            System.out.println("r : " + r);

            if (r > 0.45 && (rect.width > (bw.width() * 0.01)) && (rect.height > (bw.height() * 0.02) && rect.height < (bw.height() * 0.15))) {
                Imgproc.rectangle(img, rect.tl(), rect.br(), new Scalar(0,
                        255, 0), 2);
                cut++;
            }
        }

        System.out.println("cut : " + cut);
        System.out.println("--------------");

        HighGui.imshow("Process", img);
        HighGui.waitKey();
        
    }
}
반응형

'OpneCV' 카테고리의 다른 글

SIFT  (0) 2021.09.02
opencv 성인 이미지 검사  (0) 2020.12.31
ubuntu20, openCV450, java14  (0) 2020.12.15
opencv java mac intellij  (0) 2020.10.22
Comments