본문 바로가기
Programmer/openCV for Android

Android openCV #4 - 임계값 영상 구현하기 (Threshold Image)

by JaehwanPark 2017. 2. 28.

Android openCV #4 - 임계값 영상 구현하기 (Threshold Image)


아래는 원본 및 Threshold 적용 이미지 입니다.

순서대로 원본, THRESH_BINARY 적용, THRESH_BINARY + THRESH_OTSU 적용



1)threshold 함수를 사용한 임계영상 구현

int processToThreshold(Mat img_input, Mat &img_result){
cvtColor(img_input,img_result,CV_RGB2GRAY);
Mat srcImage = img_result;

Mat destImage1;
double th1 = threshold(srcImage,destImage1,100,255,THRESH_BINARY);
LOGD("threshold th1 = %f",th1);
img_result = destImage1.clone();

return (0);
}
int processToThreshold(Mat img_input, Mat &img_result){
cvtColor(img_input,img_result,CV_RGB2GRAY);
Mat srcImage = img_result;

Mat destImage1;
double th1 = threshold(srcImage,destImage1,100,255,THRESH_BINARY + THRESH_OTSU);
LOGD("threshold th1 = %f",th1);
img_result = destImage1.clone();

return (0);
}

1)adaptiveThreshold 함수를 사용한 방법

int processToadaptiveThreshold(Mat img_input, Mat &img_result){
cvtColor(img_input,img_result,CV_RGB2GRAY);
Mat srcImage = img_result;

Mat destImage1;
adaptiveThreshold(srcImage,destImage1,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,21,5);
img_result = destImage1.clone();

return (0);
}


위의 예제는 링크의 소스를 베이스로 해서 진행되고 있습니다.

http://technote.tistory.com/3



아래는 openCV Documentation 관련 자료 입니다. ( http://docs.opencv.org/3.2.0/ )

enum  

cv::ThresholdTypes { 
  cv::THRESH_BINARY = 0, 
  cv::THRESH_BINARY_INV = 1, 
  cv::THRESH_TRUNC = 2, 
  cv::THRESH_TOZERO = 3, 
  cv::THRESH_TOZERO_INV = 4, 
  cv::THRESH_MASK = 7, 
  cv::THRESH_OTSU = 8, 
  cv::THRESH_TRIANGLE = 16 
}

threshold.png


§ threshold()

double cv::threshold(InputArray src,
OutputArray dst,
double thresh,
double maxval,
int type 
)

Applies a fixed-level threshold to each array element.

The function applies fixed-level thresholding to a single-channel array. The function is typically used to get a bi-level (binary) image out of a grayscale image ( cv::compare could be also used for this purpose) or for removing a noise, that is, filtering out pixels with too small or too large values. There are several types of thresholding supported by the function. They are determined by type parameter.

Also, the special values cv::THRESH_OTSU or cv::THRESH_TRIANGLE may be combined with one of the above values. In these cases, the function determines the optimal threshold value using the Otsu's or Triangle algorithm and uses it instead of the specified thresh . The function returns the computed threshold value. Currently, the Otsu's and Triangle methods are implemented only for 8-bit images.

Parameters
srcinput array (single-channel, 8-bit or 32-bit floating point).
dstoutput array of the same size and type as src.
threshthreshold value.
maxvalmaximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
typethresholding type (see the cv::ThresholdTypes).
See also
adaptiveThresholdfindContourscompareminmax
Examples:
ffilldemo.cpp.


댓글