Convert RGB to Black & White in OpenCV

前端 未结 5 920
礼貌的吻别
礼貌的吻别 2020-11-28 04:03

I would like to know how to convert an RGB image into a black & white (binary) image.

After conversion, how can I save the modified image to disk?

5条回答
  •  执笔经年
    2020-11-28 04:23

    A simple way of "binarize" an image is to compare to a threshold: For example you can compare all elements in a matrix against a value with opencv in c++

    cv::Mat img = cv::imread("image.jpg", CV_LOAD_IMAGE_GRAYSCALE); 
    cv::Mat bw = img > 128;
    

    In this way, all pixels in the matrix greater than 128 now are white, and these less than 128 or equals will be black

    Optionally, and for me gave good results is to apply blur

    cv::blur( bw, bw, cv::Size(3,3) );
    

    Later you can save it as said before with:

    cv::imwrite("image_bw.jpg", bw);
    

提交回复
热议问题