마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

이미지 사각형 검출(opencv) 관련 질문드립니다. [closed]

0 추천

안녕하십니까 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;
}

---

 

그리고 결과 이미지입니다.

app image

 

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

original

 

앱 이미지와같이 중간부분은 아예 검출자체가 안되는것 같습니다.

contrast와 brightness를 바꿔보아도 동일한것같아 질문드립니다.

 

추가로 표시된 이미지를 순서대로 배열로 저장하는 방법이 따로 있을까요?

 

고수분들의 많은 도움이 필요합니다 ㅜㅜ

읽어주시는분 모두 감사드립니다~

질문을 종료한 이유: 답이없음
나는주나니 (140 포인트) 님이 2020년 2월 25일 질문
나는주나니님이 2020년 3월 1일 closed
...