Not registered yet?
Register now! It is easy and done in 1 minute and gives you access to special discounts and much more!
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?
Simple binary threshold method is sufficient.
using namespace std;
using namespace cv;
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
You can use GaussianBlur to get a smooth black and white image.
This seemed to have worked for me!
Mat a_image = imread(argv);
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);
Use cv2 and Python:
1- Grayscale Image
im_gray = cv2.imread('grayscale_image.png', cv2.CV_LOAD_IMAGE_GRAYSCALE)
2- Convert image to Binary
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
3- Store to Disck
This applies the threshold indeed but the bitdepth remains 8.
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:
Is JPEG really the right format for B&W????
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.
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.
@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.
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:
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
IplImage *im_rgb = cvLoadImage("image.jpg");
IplImage *im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
Mat im_rgb = imread("image.jpg");
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):
IplImage* im_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);
cvThreshold(im_gray, im_bw, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
Mat img_bw = im_gray > 128;
In the above example, 128 is the threshold.
4. Save to disk
Depending on the application you may want to do a dithering rather than a simple threshold.
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
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.
I would like to see your this work : the images have to be converted from colour -> grayscale -> black + white as you mentioned, Thanks
The link mentioned above doens't work, this one does : link