OpenCV C + + How to turn an RGB image into a grayscale image

Source: Internet
Author: User
Tags function prototype

http://m.blog.csdn.net/blog/u014395105/41308979

The recent study of how to use C + + to process images without the use of encapsulated OPENCV code is a good way to better understand OPENCV's internal principles.

Search on the Internet some of the C + + code to achieve RGB (color) image conversion to Gray (grayscale) principle and code, poor readability, so I tidied up a bit, if necessary reprint, please indicate the source, thank you!

First, learning Cvtcolor function


void Cvcvtcolor (const cvarr* SRC, cvarr* dst, int code);

src input 8-bit,16-bit or 32-bit single-fold precision floating-point image.


DST output 8-bit, 16-bit or 32-bit single-precision floating-point image.


The Code color space Conversion mode, which implements different types of color space conversions. For example, Cv_bgr2gray is converted to grayscale, CV_BGR2HSV converts the picture from RGB space to HSV space. When code chooses Cv_bgr2gray, DST needs to be a single-channel picture. When code selects CV_BGR2HSV, the RGB values need to be normalized to 0-1 for 8-bit graphs. So the range of H in the HSV graph is 0-360,s and V is 0-1.

Second, use the function of OPENCV to convert color image to grayscale Image:


#include

using namespace CV;
using namespace Std;

int main ()
{
Mat src = imread ("lena.jpg", 1);

Mat src = imread ("Lena.jpg", 0);


Mat DST;
Namedwindow ("RGB", window_autosize);
Imshow ("RGB", SRC);
Waitkey (0);


Cvtcolor (Src,dst,cv_bgr2gray);
Namedwindow ("GRAY", window_autosize);
Imshow ("GRAY", DST);
Cout<<dst.channels () <<endl;

Waitkey (0);

Src.release ();
Dst.release ();
DestroyWindow ("RGB");
DestroyWindow ("GRAY");
return 0;
}

Three, Imread function prototype is: Mat imread (const string& filename, int flags=1);

Mat is a data structure in OpenCV, where we define a mat-type variable img, which is used to save the read-in image, which is written at the beginning of this article, we use the Imread function to read the image, the first field identifies the image's file name (including the extension), The second field is used to specify the color and depth of the read-in image, which can take several of the following values:


1) cv_load_image_unchanged (<0), read in original image (including alpha channel),


2) Cv_load_image_grayscale (0), read in grayscale image


3) Cv_load_image_color (>0), read in RGB format

Reprint Please specify source: http://blog.csdn.net/u014395105/article/details/41308979

OpenCV C + + How to turn an RGB image into a grayscale image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.