How can I take the rgb value of an image and color correct to an original rgb value?

Illustration
Brooklyn - 2023-06-08T13:03:04+00:00
Question: How can I take the rgb value of an image and color correct to an original rgb value?

I am needing to write a code that takes the rgb value of a reference square in an image, and color correct it to the orginal rgb value of the reference square.   I will have various images with this reference square that will change colors depending on how the sunlight hits it, so I need to correct the whole image back to the original color. Is there a code that can be written to do this? Additionally, I mainly am focused on the blue values, but how will the change in values be stored?

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

You forgot to attach your reference image and the image that needs correction.
 
Basically you need to segment the image to find the square in both images, then assign the reference image colors to the test image. Assuming you've done the segmentation and have the square masks, you then do this.
 
 
% Split test and reference image into component color channels.
[rTest, gTest, bTest] = imsplit(testImage);
[rRef, gRef, bRef] = imsplit(refImage);
% Assign mean color from ref image to test image, but only in the square region.
rTest(testSquaremask) = mean(rRef(refSquareMask));
gTest(testSquaremask) = mean(gRef(refSquareMask));
bTest(testSquaremask) = mean(bRef(refSquareMask));
% Rebuild test image from component color channels.
testImage = cat(3, rtest, gTest, bTest);
The pixels in square region of the test image will be the same (uniform) color for all pixels in the square.
 
 
 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!