Question

I want to perform the independent color transformation using opencv c++ , i understand my scenario but I don't know the sequence to perform its implementation in using opencv. Letme me try to explain my scenario for further help.

I want to perform an independent RGB colour transformation on the original image pixel by pixel to increase contrast or make colour cast using opencv. Here above independent transformation means two things:

First, value of one color channel doesn't impact value of the other channels.

Second, pixels at different positions don't affect each other.

So the color transformation can be explained in such expressions:

Rxy' = transformRedChannel(Rxy, x, y)

Gxy' = transformGreenChannel(Gxy, x, y)

Bxy' = transformBlueChannel(Bxy, x, y)

In the above expression, Rxy stands for the the red channel value of a pixel at position(x, y) in the original image which is in range 0~255 and Rxy' stands for that in the goal image. The same holds for blue and green channels.

Since the function can be described by a [0, 255] -> [0, 255] mapping array, it is easy for function using 255 different gray image to generate the mapping array.

Was it helpful?

Solution

Accessing RGB Values of an Image can be done either by the Method showed in Accessing certain pixel RGB value in openCV

valRed   = image.at<cv::Vec3b>(row,col)[0]; //R
valGreen = image.at<cv::Vec3b>(row,col)[1]; //G
valBlue  = image.at<cv::Vec3b>(row,col)[2]; //B

Compute your new values and write them to another image using:

image.at<cv::Vec3b>(row,col)[0] = newval[0];  //R
image.at<cv::Vec3b>(row,col)[1] = newval[1];  //G
image.at<cv::Vec3b>(row,col)[2] = newval[2];  //B

or, if you really want to use split (and create 3 new images, one per channel), you can use this:

split(img , colors);

Read:

valRed   = colors[0].at<uchar>(row,col)
valGreen = colors[1].at<uchar>(row,col)
valBlue  = colors[2].at<uchar>(row,col)

Write:

colors[0].at<uchar>(row,col) = newValRed;    //R
colors[1].at<uchar>(row,col) = newValGreen;  //G
colors[2].at<uchar>(row,col) = newValBlue;   //B
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top