일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- Mac
- License
- aggs
- Kafka
- sort
- 900gle
- Test
- Elasticsearch
- aggregation
- token filter test
- plugin
- Python
- zip 암호화
- springboot
- 차트
- zip 파일 암호화
- ELASTIC
- analyzer test
- 파이썬
- API
- TensorFlow
- licence delete curl
- Java
- license delete
- matplotlib
- high level client
- MySQL
- flask
- docker
- query
Archives
- Today
- Total
개발잡부
opencv 텍스트 영역 감지 본문
반응형
텍스트 영역을 감지해보즈아
개발환경
- 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