Convert RGB to Black & White in OpenCV

172,660

Solution 1

AFAIK, you have to convert it to grayscale and then threshold it to binary.

1. Read the image as a grayscale image If you're reading the RGB image from disk, then you can directly read it as a grayscale image, like this:

// C
IplImage* im_gray = cvLoadImage("image.jpg",CV_LOAD_IMAGE_GRAYSCALE);

// C++ (OpenCV 2.0)
Mat im_gray = imread("image.jpg",CV_LOAD_IMAGE_GRAYSCALE);

2. Convert an RGB image im_rgb into a grayscale image: Otherwise, you'll have to convert the previously obtained RGB image into a grayscale image

// C
IplImage *im_rgb  = cvLoadImage("image.jpg");
IplImage *im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
cvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);

// C++
Mat im_rgb  = imread("image.jpg");
Mat im_gray;
cvtColor(im_rgb,im_gray,CV_RGB2GRAY);

3. Convert to binary You can use adaptive thresholding or fixed-level thresholding to convert your grayscale image to a binary image.

E.g. in C you can do the following (you can also do the same in C++ with Mat and the corresponding functions):

// C
IplImage* im_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);
cvThreshold(im_gray, im_bw, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

// C++
Mat img_bw = im_gray > 128;

In the above example, 128 is the threshold.

4. Save to disk

// C
cvSaveImage("image_bw.jpg",img_bw);

// C++
imwrite("image_bw.jpg", img_bw);

Solution 2

This seemed to have worked for me!

Mat a_image = imread(argv[1]);

cvtColor(a_image, a_image, CV_BGR2GRAY);
GaussianBlur(a_image, a_image, Size(7,7), 1.5, 1.5);
threshold(a_image, a_image, 100, 255, CV_THRESH_BINARY);

Solution 3

I do something similar in one of my blog postings. A simple C++ example is shown.

The aim was to use the open source cvBlobsLib library for the detection of spot samples printed to microarray slides, but the images have to be converted from colour -> grayscale -> black + white as you mentioned, in order to achieve this.

Solution 4

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);

Solution 5

Simple binary threshold method is sufficient.

include

#include <string>
#include "opencv/highgui.h"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat img = imread("./img.jpg",0);//loading gray scale image
    threshold(img, img, 128, 255, CV_THRESH_BINARY);//threshold binary, you can change threshold 128 to your convenient threshold
    imwrite("./black-white.jpg",img);
    return 0;
}

You can use GaussianBlur to get a smooth black and white image.

Share:
172,660
mohammed
Author by

mohammed

Updated on July 08, 2022

Comments

  • mohammed
    mohammed almost 2 years

    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?

  • RidaSana
    RidaSana over 12 years
    I would like to see your this work : the images have to be converted from colour -> grayscale -> black + white as you mentioned, Thanks
  • Yeraze
    Yeraze over 11 years
    The link mentioned above doens't work, this one does : link
  • Mark Ransom
    Mark Ransom over 11 years
    Depending on the application you may want to do a dithering rather than a simple threshold.
  • alvaropgl
    alvaropgl almost 9 years
    I don't know... I think its depends in what type of image are you saving. For photographs (i was working with photos) I think is the right format.
  • user1741137
    user1741137 almost 9 years
    Well, the problem with JPEG is that it introduces artefacts and degrades the quality of your picture somewhat. I would use PNG or for really small files TIFF with CCITT Fax 4 compression.
  • Alexander Abakumov
    Alexander Abakumov almost 9 years
    @user1741137, How to save a Mat as a TIFF with CCITT Fax 4 compression with OpenCV? Using Highgui.imwrite("sample.tiff", binaryImage); I always get a TIFF with LZW compression and 8-bit color depth - see my question here.
  • evk1206
    evk1206 about 8 years
    in CvtColor function do we need to use CV_RGB2GRAY or CV_BGR2GRAY. I have read that opencv is based on BGR format and we need to use that , but in the gray or binary image I am not seeing any differences when using either of them