안녕하십니까 opencv 입문단계에 있는 개발자입니다.
opencv로 사각형 검출하는 코드를 짜고있는데요 생각보다 정확하게 잡히지가 않아서 질문드립니다.
아래는 주요 코드내용입니다.
MainActivity.java
---
private static List<MatOfPoint> dSquare(Mat dst){
ArrayList<MatOfPoint> squares = new ArrayList<>();
Mat gray = new Mat();
Imgproc.cvtColor(dst, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.pyrDown(gray, dsIMG, new Size(gray.cols()/2, gray.rows()/2));
Imgproc.pyrUp(dsIMG, usIMG, gray.size());
Imgproc.Canny(usIMG, bwIMG, 0, threshold);
Imgproc.dilate(bwIMG, bwIMG, new Mat(), new Point(-1, 1), 1);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
cIMG = bwIMG.clone();
Imgproc.findContours(cIMG, contours, hovIMG, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for (MatOfPoint cnt : contours){
MatOfPoint2f curve = new MatOfPoint2f(cnt.toArray());
Imgproc.approxPolyDP(curve, approxCurve, 0.02*Imgproc.arcLength(curve, true), true);
int numberVertices = (int) approxCurve.total();
double contourArea = Imgproc.contourArea(cnt);
if(Math.abs(contourArea) < 100){
continue;
}
//Rectangle detected
if (numberVertices >= 4 && numberVertices <= 6) {
List<Double> cos = new ArrayList<>();
for (int j = 2; j < numberVertices + 1; j++) {
cos.add(angle(approxCurve.toArray()[j % numberVertices], approxCurve.toArray()[j - 2], approxCurve.toArray()[j - 1]));
}
Collections.sort(cos);
double mincos = cos.get(0);
double maxcos = cos.get(cos.size() - 1);
if (numberVertices == 4 && mincos >= -0.1 && maxcos <= 0.3) {
squares.add(cnt);
}
}
}
return squares;
}
---
그리고 결과 이미지입니다.

아래는 원본 이미지입니다.

앱 이미지와같이 중간부분은 아예 검출자체가 안되는것 같습니다.
contrast와 brightness를 바꿔보아도 동일한것같아 질문드립니다.
추가로 표시된 이미지를 순서대로 배열로 저장하는 방법이 따로 있을까요?
고수분들의 많은 도움이 필요합니다 ㅜㅜ
읽어주시는분 모두 감사드립니다~