opencv:二值图像的概念

灰度图像与二值图像

二值分割

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char** argv)
{
    Mat src = imread("f:/images/shuang001.jpg");
    if (src.empty())
    {
        printf("Could not find the image!\n");
        return -1;
    }

    namedWindow("input", WINDOW_AUTOSIZE);
    imshow("input", src);

    Mat gray, binary;
    cvtColor(src, gray, COLOR_BGR2GRAY);
    imshow("gray", gray);

    threshold(gray, binary, 127, 255, THRESH_BINARY);
    imshow("binary", binary);

    threshold(gray, binary, 127, 255, THRESH_BINARY_INV);
    imshow("binary invert", binary);

    threshold(gray, binary, 127, 255, THRESH_TRUNC);
    imshow("binary trunc", binary);

    threshold(gray, binary, 127, 255, THRESH_TOZERO);
    imshow("binary to zero", binary);

    threshold(gray, binary, 127, 255, THRESH_TOZERO_INV);
    imshow("binary to zero invert", binary);


    waitKey(0);
    destroyAllWindows();

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wbyixx/p/12305849.html