'비트맵'에 해당되는 글 2건

  1. 2018.09.05 안드로이드 findContours 오류 해결
  2. 2018.08.25 [안드로이드] 비트맵 이진화 처리 1


안드로이드 OpenCV의 findContours 사용중 다음과 같은 오류가 발생했을 때 해결하는 방법입니다.

오류 :

Caused by: CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.3) /build/3_4_pack-android/opencv/modules/imgproc/src/contours.cpp:199: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function '_CvContourScanner* cvStartFindContours_Impl(void*, CvMemStorage*, int, int, int, CvPoint, int)'


오류가 나는 이유는 findContours의 입력 값과 출력 값의 데이터 옵션 값이 다르기 때문입니다.


해결 방법 : 

Imgproc.findContours를 호출하기 전

기존의 Mat result 객체를 다음과 같이 처리해줍니다.

Imgproc.cvtColor(src, result, Imgproc.COLOR_BGR2GRAY, 1);


오류해결!


* 참고

문자인식 중 findContours가 이진화(threshold) 된 이미지의 개체들의 blob을 찾지 못할 때 : 이진화 된 이미지의 픽셀 값(색상)을 모두 반전 시켜줍니다.

코드 : 

for(int x=0; x<roi.getWidth(); x++) {

    for(int y=0; y<roi.getHeight(); y++) {

        if(roi.getPixel(x, y) == -1) {

            roi.setPixel(x, y, 0);

        }

        else {

            roi.setPixel(x, y, -1);

        }

    }

}





정보가 유익하셨다면 아래 공감버튼 눌러주시면 감사하겠습니다.

질문사항은 댓글로 달아주시면 성의껏 답변해드리겠습니다.




비트맵 이진화 처리 함수입니다.

먼저 비트맵을 받아 복사한 후

모든 픽셀을 탐색하며 색깔을 변경해줍니다.(GetNewColor)

그리고 새로운 비트맵을 반환합니다.


private Bitmap GetBinaryBitmap(Bitmap bitmap_src) {

    Bitmap bitmap_new=bitmap_src.copy(bitmap_src.getConfig(), true);

    for(int x=0; x<bitmap_new.getWidth(); x++) {

        for(int y=0; y<bitmap_new.getHeight(); y++) {

            int color=bitmap_new.getPixel(x, y);

            color=GetNewColor(color);

            bitmap_new.setPixel(x, y, color);

        }

    }

    return bitmap_new;

}


GetNewColor 함수입니다.

GetColorDistance 함수를 통해 흰색과 검정색 중 가까운 색깔을 선택합니다.

BLACK 부분에 0.4를 곱해 BLACK 값을 높여서 좀 더 어두운 환경에서도 이진화를 잘 수행할 수 있도록 했습니다.

흰색 또는 검정색(이진화 값)을 반환합니다.


private int GetNewColor(int c) {

    double dwhite=GetColorDistance(c,Color.WHITE);

    double dblack=GetColorDistance(c,Color.BLACK)*0.4;

    if(dwhite<=dblack) {

        return Color.WHITE;

    }

    else {

        return Color.BLACK;

    }

}


GetColorDistance 함수입니다.

R,G,B 값을 받아와 제곱의 합의 제곱근입니다.(거리 구하는 공식)

거리 값을 반환합니다.


private double GetColorDistance(int c1, int c2) {

    int db= Color.blue(c1)-Color.blue(c2);

    int dg=Color.green(c1)-Color.green(c2);

    int dr=Color.red(c1)-Color.red(c2);

    double d=Math.sqrt(  Math.pow(db, 2) + Math.pow(dg, 2) +Math.pow(dr, 2)  );

    return d;

}


실행 결과입니다.


GetNewColor 함수의 BLACK 및 WHITE값을 적절히 조절하여 최적의 이진화 된 이미지를 얻을 수 있습니다.

이진화 된 이미지를 통해 OCR을 더욱 효과적으로 처리할 수 있습니다.



질문 및 오류사항은 댓글로 달아주세요.



to Top