opencv阈值操作,基本阈值操作,自适应阈值操作

 本章内容:


     1. 基本阈值操作
     2. 自适应阈值操作

1.基本阈值操作

opencv阈值操作,基本阈值操作,自适应阈值操作_第1张图片

 

输出结果:

2. 自适应阈值操作

opencv阈值操作,基本阈值操作,自适应阈值操作_第2张图片

 

输出结果

opencv阈值操作,基本阈值操作,自适应阈值操作_第3张图片

代码


#include
#include

int main(int argc, char *argv[])
{
    /*
     本章内容:
     1. 基本阈值操作
     2. 自适应阈值操作
    */
    cv::String fileName = "/home/wang/dev/Image/cdut.jpeg";
    cv::Mat src = cv::imread(fileName);
    if(src.data == NULL){
        printf("图像读入失败\n");
        return -1;
    }
    /* 阈值操作
        api接口:CV_EXPORTS_W double threshold( InputArray src, OutputArray dst,
                                       double thresh, double maxval, int type );
        参数:
            @param thresh threshold value.
            @param maxval maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding
            types.
            @param type thresholding type (see #ThresholdTypes).
            @return the computed threshold value if Otsu's or Triangle methods used.
            @sa  adaptiveThreshold, findContours, compare, min, max
        enum cv::ThresholdTypes{
                THRESH_BINARY     = 0, 将大于阈值的灰度值设为最大灰度值。小于阈值的值设为0。
                THRESH_BINARY_INV = 1,  将大于阈值的灰度值设为0。大于阈值的值设为最大灰度值。
                THRESH_TRUNC      = 2,  将大于阈值的灰度值设为阈值。小于阈值的值保持不变。
                THRESH_TOZERO     = 3,  将小于阈值的灰度值设为0,大于阈值的值保持不变。
                THRESH_TOZERO_INV = 4,  将大于阈值的灰度值设为0,小于阈值的值保持不变。
                THRESH_MASK       = 7,
                THRESH_OTSU       = 8, //!< flag, use Otsu algorithm to choose the optimal threshold value
                THRESH_TRIANGLE   = 16 //!< flag, use Triangle algorithm to choose the optimal threshold value
        }

    */
    cv::Mat gray;
    cv::Mat dst1;
    cv::Mat dst2;
    cv::Mat dst3;
    cv::Mat dst4;
    cv::Mat dst5;
    cv::Mat dst6;
    cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
    cv::threshold(gray,dst1,100,255, cv::THRESH_BINARY);
    cv::threshold(gray,dst2,100,255, cv::THRESH_BINARY_INV);
    cv::threshold(gray,dst3,100,255, cv::THRESH_TRUNC);
    cv::threshold(gray,dst4,100,255, cv::THRESH_TOZERO);
    cv::threshold(gray,dst5,100,255, cv::THRESH_TOZERO_INV);
    cv::threshold(gray,dst6,100,255, cv::THRESH_MASK);
    cv::imshow("gray", gray);
    cv::imshow("THRESH_BINARY", dst1);
    cv::imshow("THRESH_BINARY_INV", dst2);
    cv::imshow("THRESH_TRUNC", dst3);
    cv::imshow("THRESH_TOZERO", dst4);
    cv::imshow("THRESH_TOZERO_INV", dst5);
    cv::imshow("THRESH_MASK", dst6);

    /* 2.自适应阈值操作
        THRESH_OTSU       = 8, //!< flag, use Otsu algorithm to choose the optimal threshold value
        THRESH_TRIANGLE   = 16 //!< flag, use Triangle algorithm to choose the optimal threshold value
    */
    cv::Mat dst7;
    cv::Mat dst8;
    int OTSU = cv::threshold(gray,dst7,100,255, cv::THRESH_OTSU);
    int TRIANGLE = cv::threshold(gray,dst8,100,255, cv::THRESH_TRIANGLE);
    std::cout << "OTSU " << OTSU << std::endl;
    std::cout << "TRIANGLE " << TRIANGLE << std::endl;
    cv::imshow("THRESH_OTSU", dst7);
    cv::imshow("THRESH_TRIANGLE", dst8);

    cv::waitKey(0);
    return 1;
}

 

你可能感兴趣的:(opencv计算机视觉,opencv,计算机视觉)