opencv基礎_14(基本閾值操作)

1、圖像閾值(threshold)

閾值 是什麼?簡單點說是把圖像分割的標尺,這個標尺是根據什麼產生的,閾值產生算法?閾值類型。(Binary segmentation)

2、閾值類型一閾值二值化(threshold binary)

左下方的圖表示圖像像素點Src(x,y)值分佈情況,藍色水平線表示閾值

3、閾值類型一閾值反二值化(threshold binary Inverted)

左下方的圖表示圖像像素點Src(x,y)值分佈情況,藍色水平線表示閾值

4、閾值類型一截斷 (truncate)

左下方的圖表示圖像像素點Src(x,y)值分佈情況,藍色水平線表示閾值

5、閾值類型一閾值取零 (threshold to zero)

6、閾值類型一閾值反取零 (threshold to zero inverted)

7、代碼演示

Mat src;
int iTypeValue = 2;
void CallbackFunc(int threshold_value, void*);

int main(int argc, char** argv) 
{
    //1. 加載圖像
    src = imread("./test.jpg");

    //2. 判斷圖像是否加載成功
    if (!src.data) 
    {
        printf("could not load image...\n");
        return -1;
    }

    //3. 顯示輸入圖像
    namedWindow("input image", CV_WINDOW_AUTOSIZE);    
    imshow("input image", src);

    namedWindow("binary image", CV_WINDOW_AUTOSIZE);

    int iThresholdValue = 127;    
    createTrackbar("Threshold Value:", "binary image", &iThresholdValue, 255, CallbackFunc);
    createTrackbar("Type Value:", "binary image", &iTypeValue, 4, CallbackFunc);

    waitKey(0);

    return 0;
}

void CallbackFunc(int threshold_value, void*)
{
    Mat gray, dst;
    cvtColor(src, gray, CV_BGR2GRAY);
    threshold(gray, dst, threshold_value, 255, THRESH_BINARY | iTypeValue);
    imshow("binary image", dst);
}

8、顯示結果

(1)、輸入圖

(2)、結果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章