Contrast adjustment: How to convert OpenCV code (.pde) into Matlab code?

2 次查看(过去 30 天)
I am working on adjusting the RMS contrast of a video (3GB) on Matlab. RMS contrast is calculated by this equation: http://en.wikipedia.org/wiki/Contrast_%28vision%29#RMS_contrast. One of the credidable ways to work this out is by Jeff Thompson (https://github.com/jeffThompson/ProcessingTeachingSketches/blob/master/ImageProcessingAndOpenCV/MeasureImageBrightnessAndContrast/MeasureImageBrightnessAndContrast.pde). However, it was written in OpenCV. There is no other website writing it as far as I'm concerned.
Can anyone help me out on converting it to Matlab? Your help would be greatly appreciated.
/*
MEASURE IMAGE BRIGHTNESS AND CONTRAST
Jeff Thompson | 2013 | www.jeffreythompson.org
Measures the overall brightness and contrast in an image.
NOTE: this is average for both measures, meaning localized areas of
extreme brightness/contrast may throw off the measurements.
Contrast based on this formula:
http://en.wikipedia.org/wiki/Contrast_%28vision%29#RMS_contrast
Thanks to Amnon Owed for helping figure out the contrast/RMS algorithm!
https://forum.processing.org/topic/calculate-image-contrast-using-root-mean-square-rms#25080000001971367
*/
// file to load (gray, mountain, truck)
String filename = "truck.jpg";
boolean normalizeRange = true; // normalize results to range of 0-1
PImage img;
float brightness = 0;
float contrast = 0;
void setup() {
img = loadImage(filename);
size(img.width, img.height);
image(img, 0,0);
loadPixels(); // load pixels into array, iterate!
// find average brightness across image
for (color c : pixels) {
float r = c >> 16 & 0xFF; // extract RGB values quickly (better than red(), etc)
float g = c >> 8 & 0xFF;
float b = c & 0xFF;
brightness += (0.2126 * r) + (0.7152 * g) + (0.0722 * b); // scales RGB to perceived brightness
if (normalizeRange) {
brightness /= 255.0; // normalize to 0-1
}
}
brightness /= pixels.length; // average result
println("Average brightness: " + brightness);
// find contrast by comparing average brightness with current value
for (color c : pixels) {
float r = c >> 16 & 0xFF;
float g = c >> 8 & 0xFF;
float b = c & 0xFF;
float pxIntensity = (0.2126 * r) + (0.7152 * g) + (0.0722 * b);
if (normalizeRange) {
pxIntensity /= 255.0; // normalizes to range 0-1
}
contrast += pow((brightness - pxIntensity), 2);
}
contrast /= pixels.length;
println("Average contrast: " + contrast);
}

回答(1 个)

VINAYAK LUHA
VINAYAK LUHA 2023-11-25
Hi Alexander,
I understand that you have an OpenCV code snippet for calculating RMS contrast in an image. Further, you also want its MATLAB equivalent code.
Here is the MATLAB equivalent code of the OpenCV code you mentioned -
filename = 'img.jpg';
normalizeRange = false; % normalize results to range of 0-1
img = imread(filename);
brightness = 0;
contrast = 0;
h =size(img,1);
w =size(img,2);
% find average brightness across image
for i = 1:h
for j=1:w
r = img(i,j,1);
g = img(i,j,2);
b = img(i,j,3);
brightness = brightness + double(0.2126 * r) + double(0.7152 * g) + double(0.0722 * b); % scales RGB to perceived brightness
if normalizeRange
brightness = brightness / 255.0; % normalize to 0-1
end
end
end
brightness = brightness / (w*h); % average result
disp(['Average brightness: ', num2str(brightness)]);
% find contrast by comparing average brightness with current value
for i = 1:h
for j=1:w
r = img(i,j,1);
g = img(i,j,2);
b = img(i,j,3);
pxIntensity = double(0.2126 * r) + double(0.7152 * g) + double(0.0722 * b);
if normalizeRange
pxIntensity = pxIntensity / 255.0; % normalizes to range 0-1
end
contrast = contrast + (brightness - pxIntensity)^2;
end
end
contrast = contrast / (w*h);
disp(['Average contrast: ', num2str(contrast)]);
Hope this helps
Regards,
Vinayak Luha

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by