user048
user048

Reputation: 9

how to separate BGR components of a pixel in color image using openCV

Since each pixel memory contains 8 bit for each component Blue,Green and Red. So how can I separate these components from Image or Image Matrix. As int Blue = f(Image(X,y));// (x,y) = Coordinate of a pixel of Image similarly, for red and green. So what should be function f and 2D matrix Image;

Thanks in advance

Upvotes: 0

Views: 3015

Answers (1)

user349026
user349026

Reputation:

First off, you must go through the basics of OpenCV and turn your attention towards other parts of image processing. What you ask for is pretty basic and assuming you will be using OpenCV 2.1 and higher,

cv::Mat img =  Read the image off the disk or do something to fill the image.

To access the RGB values

img.at<cv::Vec3b>(x,y); 

But would give the values in reverse that is BGR. So make sure you note this. Basically a cv::Vec3b type that is accessed.

img.at<cv::Vec3b>(x,y)[0];//B
img.at<cv::Vec3b>(x,y)[1];//G
img.at<cv::Vec3b>(x,y)[2];//R

or

Vec3f pixel = img.at<Vec3f>(x, y);

int b = pixel[0];
int g = pixel[1];
int r = pixel[2];

Now onto splitting the image into RGB channels you can use the following

Now down to primitive C style of OpenCV (There C and C++ style supported) You can use the cvSplit function

IplImage* rgb = cvLoatImage("C://MyImage.bmp");

//now create three single channel images for the channel separation
IplImage* r = cvCreateImage( cvGetSize(rgb), rgb->depth,1 );
IplImage* g = cvCreateImage( cvGetSize(rgb), rgb->depth,1 );
IplImage* b = cvCreateImage( cvGetSize(rgb), rgb->depth,1 );

cvSplit(rgb,b,g,r,NULL);

OpenCV 2 CookBook Is one of the best books on OpenCV. Will help you alot.

Upvotes: 1

Related Questions