有關opencv的學習(13)—使用固定閾值處理圖像

     在對圖像的處理中,我們可以使用閾值的方法創建二值圖像,從而從圖像當中提取出有意義元素。

     代碼如下:


#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
//#include "onMouse.h"


using namespace cv;
using namespace std;


int main()
{
    Mat image=imread("/Users/zhangxiaoyu/Desktop/1.png",0);//0爲讀出的是灰度圖像
    
    if(image.empty())
    {
        cout<<"Error!cannot be read...../n";
        return -1;
    }
    
    
    //使用固定的閾值
    cv::Mat fixedthreshold;
    cv::threshold(image, fixedthreshold, 120, 255, cv::THRESH_BINARY);//設定閾值爲120
    
    cv::namedWindow("threshold-image");
    cv::imshow("threshold-image", fixedthreshold);
    
    waitKey(0);
    
    
    
    
    return 0;

}

        其中:

        cv::threshold(image, fixedthreshold, 120, 255, cv::THRESH_BINARY);//設定閾值爲120

       設定閾值爲120,

        CV_THRESH_BINARY:如果 src(x,y)>threshold ,dst(x,y) = max_value; 否則,dst(x,y)=0;

意爲將圖像中所有像素值高於120的像素點置爲255(白色),低於120的像素點置爲0(黑色)。

使用的圖片如下所示:


運行程序,效果如下所示:







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