Gray scale is shades of grey , darkest or weakest shade is black and the lightest or strongest shade is white.It has no color components like RGB image.It has one component gray and different intensity levels in between .Gray scale intensity is stored as an 8-bit integer giving 256 possible different shades of gray from black to white.
I'm going to explain this conversion by using 2 methods
- By using inbuilt open CV function
- By using custom Algorithm
**** Note that I'm explaining these tutorials using visual studio
By using inbuilt open CV function
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> using namespace cv; int main() { Mat RGBImage; RGBImage =imread("c:/example_image.png"); Mat grayScaleImage(RGBImage.size(),CV_8UC1); cvtColor(RGBImage,grayScaleImage,CV_RGB2GRAY); namedWindow("RGBImage",1); namedWindow("GrayScaleImage",1); imshow("RGBImage",RGBImage); imshow("GrayScaleImage",grayScaleImage); waitKey(); return 0; }
this is very easy method because inbuilt Open CV function save your day
Mat RGBImage; RGBImage =imread("c:/example_image.png");
From this line you can read image and assign it to RGBImage Mat object
Mat grayScaleImage(RGBImage.size(),CV_8UC1);
From this line we can create Mat object named "grayScaleImage"
As first paramether we have to pass the size of that object and we make the same size as RGBImage by using RGBImage.size()
And also that object bit depth ,data type and number of channels need to mention as next parameter.
CV_8UC1 mean our object has 8 bit depth unsigned char data type (U) with one channel (C1)
cvtColor(RGBImage,grayScaleImage,CV_RGB2GRAY);
From this we convert our RBG image to Gray Scale image by using cvtColor function.
As first paramether we pass sourse image next we pass output image and final parameter we mentioned conversion type .
In this case we convert RGB to Gray scale for that we pass CV_RGB2GRAY as our 3rd parameter.
namedWindow("RGBImage",1); namedWindow("GrayScaleImage",1); imshow("RGBImage",RGBImage); imshow("GrayScaleImage",grayScaleImage
From this line we can get our results.
By using custom Algorithm
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> using namespace cv; int main() { Mat RGBImage; RGBImage =imread("c:/example_image.png"); Mat grayScaleImage(RGBImage.size(),CV_8UC1); int rows=RGBImage.rows; int cols=RGBImage.cols; for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ Vec3b intensity=RGBImage.at<Vec3b>(i,j); int blue=intensity.val[0]; int green =intensity.val[1]; int red=intensity.val[2]; grayScaleImage.at<uchar>(i,j)=blue*0.0722+green*0.7152+red*0.2126; } } namedWindow("RGBImage",1); namedWindow("GrayScaleImage",1); imshow("RGBImage",RGBImage); imshow("GrayScaleImage",grayScaleImage); waitKey(); return 0; }
By using these 2 for loops we can access to each and every location of Mat object (pixel of image)
for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ Vec3b intensity=RGBImage.at<Vec3b>(i,j); int blue=intensity.val[0]; int green =intensity.val[1]; int red=intensity.val[2]; grayScaleImage.at<uchar>(i,j)=blue*0.0722+green*0.7152+red*0.2126; } }
RGB has 3 color component we can consider every pixel is 3 dimensional vector .
Therefore by using this line we can assign it to 3d vector
Vec3b intensity=RGBImage.at<Vec3b>(i,j);
Next we can get each and every 3 color component value from each pixel
int blue=intensity.val[0]; int green =intensity.val[1]; int red=intensity.val[2];
Then we can calculate relevant gray scale value respect to these 3 color component values by using this function
grayScaleImage.at<uchar>(i,j)=blue*0.0722+green*0.7152+red*0.2126;
Then we assign that values to grayScaleImage Mat object pixel values
If you have Any question ,complain or suggestion please leave a comment .I always like to help you. :) See you soon .Thank you.