c++ - Convert RGB to Black & White in OpenCV -
I would like to know how to make a RGB image a black and amp; White (binary) image
How can I save modified image discs after conversion?
AFAIK, you have to convert it to grayscale and then it must have thresholds in binary.
Read 1 image as a grayscale image If you are reading RGB image from disk, then you can read it directly as a grayscale image, such as:
// C IplImage * Im_gray = cvLoadImage ("image.jpg", CV_LOAD_IMAGE_GRAYSCALE); // C ++ (OpenCV 2.0) mat IM_grey = imed ("image .jpg", cv_load_IMUnURRACLAL);
Convert a RGB image to a grayscale image by Convert to 3 binary You can change your grayscale image to binary image or change it. e.g. You can do the following in C (you can do this in C ++ with Mat and the corresponding function): In the above example, there are 128 thresholds. Save to disc 4 im_rgb
Otherwise, you must convert the RGB image you already received to a grayscale image < / P>
// 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);
// 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 & gt; 128;
// c csSaveImage ("image_bw.jpg", img_bw); // C ++ imwrite ("image_bw.jpg", img_bw);
Comments
Post a Comment