通过OpenCV阈值函数threshold实现图像的二值化

通过OpenCV阈值函数threshold实现图像的二值化

二值化和阈值

  1. THRESH_BINARY 二进制阈值化;
  2. THRESH_BINARY_INV 反二进制阈值化;
#include
#include 
#include 
#include 
#include 
using namespace cv;
void PrintMs(const char *text = "")
{
    static long long last = 0;
    long long cur = getTickCount();
    if (last == 0)
    {
        last = cur;
        return;
    }
    long long ms = 0;
    ms = ((double)(cur - last) / getTickFrequency()) * 1000;
    if (*text != 0)
    {
        printf("%s = %dms\n ", text, ms);
    }
    last = getTickCount();
}

int main(int argc, char *argv)
{
    Mat src = imread("lena.jpg");
    Mat bin;
    Mat gray;
    cvtColor(src, gray, COLOR_BGR2GRAY);
    //二进制阈值化
    //像素值大于100,设为255,反之则为0;
    threshold(gray, bin, 100, 255, THRESH_BINARY);
    namedWindow("src");
    namedWindow("bin");
    imshow("src", src);
    imshow("bin", bin);
    waitKey(0);

    return 0;
}

通过OpenCV阈值函数threshold实现图像的二值化_第1张图片

你可能感兴趣的:(OpenCV)