# Divide an image vertically into two equally luminous parts, ..., calculate the luminance of each part.., PERSONAL NON-FUNDED RESEARCH,

23 views (last 30 days)

Show older comments

David Corwin
on 28 Jun 2022

Commented: David Corwin
on 3 Sep 2022 at 17:45

THIS IS NOT HOMEWORK. PERSONAL NON-FUNDED RESEARCH I will definitely give the person who answers this and matlab community acknowledgement

a. convert a picture to gray scale

rgbImage = imread('photo.jpg');

grayImage = rgb2gray(rgbImage);

b. divide the picture vertically such that the two parts (left and right) have equal luminance

c. . bisect each part so that there are now four parts to this picture Upper left, Upper right, lower left lower right

and. obtain the average luminance of each part.

d. obtain the vector of luminance for each part:: the luminance value multiplied by the distance from the point of intersection to the geometric center of the respective part.

e. However, for the two upper parts, the x,y value of this vector is replaced by x,(1.07)y. in other words the vertical component is lightened by 1.07

f.. add the four resultant vectors to obtain the distance from the intersection

Could you please provide the script for a "photo.jpg" so I could insert my own file. I have trouble running it backwards.

##### 4 Comments

Image Analyst
on 28 Jun 2022

@DGM I guess it doesn't show up when you click his name because Matt closed that thread.

@David Corwin do any of the answers below help?

### Accepted Answer

### More Answers (5)

Image Analyst
on 28 Jun 2022

Edited: Image Analyst
on 28 Jun 2022

Here's a start. See if you can finish it:

% Initialization Steps.

clc; % Clear the command window.

close all; % Close all figures (except those of imtool.)

clear; % Erase all existing variables. Or clearvars if you want.

workspace; % Make sure the workspace panel is showing.

format long g;

format compact;

fontSize = 20;

%--------------------------------------------------------------------------------------------------------

% READ IN IMAGE

folder = [];

baseFileName = 'cameraman.tif';

fullFileName = fullfile(folder, baseFileName);

% Check if file exists.

if ~exist(fullFileName, 'file')

% The file doesn't exist -- didn't find it there in that folder.

% Check the entire search path (other folders) for the file by stripping off the folder.

fullFileNameOnSearchPath = baseFileName; % No path this time.

if ~exist(fullFileNameOnSearchPath, 'file')

% Still didn't find it. Alert user.

errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);

uiwait(warndlg(errorMessage));

return;

end

end

rgbImage = imread(fullFileName);

% Get the dimensions of the image.

% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.

[rows, columns, numberOfColorChannels] = size(rgbImage)

%--------------------------------------------------------------------------------------------------------

% Display the image.

subplot(2, 2, 1);

imshow(rgbImage);

impixelinfo;

axis('on', 'image');

title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');

if numberOfColorChannels > 1

% It's not really gray scale like we expected - it's color.

fprintf('It is not really gray scale like we expected - it is color\n');

% Extract the blue channel.

grayImage = rgbImage(:, :, 3);

else

grayImage = rgbImage;

end

% Update the dimensions of the image.

% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.

[rows, columns, numberOfColorChannels] = size(grayImage)

% Maximize window.

g = gcf;

g.WindowState = 'maximized';

drawnow;

%--------------------------------------------------------------------------------------------------------

% Get means for each "half"

meanLeft = nan(1, columns);

meanRight = nan(1, columns);

for col = 2 : columns

meanLeft(col) = mean2(grayImage(:, 1:col))

meanRight(col) = mean2(grayImage(:, col+1:end))

end

subplot(2, 2, 2);

plot(meanLeft, 'b-', 'LineWidth', 2);

hold on;

grid on;

plot(meanRight, 'r-', 'LineWidth', 2);

legend('Left Rectangle', 'Right Rectangle', 'Location','southwest')

xlabel('Dividing Column', 'FontSize',fontSize)

ylabel('Mean Gray Level', 'FontSize',fontSize)

% Obviously you need to find where they cross.

% Be aware that there may be several dividing columns where the cross

% and the left intensity will equal the right intensity.

% So in that case just use min(abs(meanLeft-meanRight)) to find out the index of the closest match.

[~, bestColumn] = min(abs(meanLeft - meanRight))

xline(bestColumn, 'Color', 'g', 'LineWidth', 2)

subplot(2, 2, 1);

xline(bestColumn, 'Color', 'g', 'LineWidth', 2)

leftImage = grayImage(:, 1:bestColumn);

rightImage = grayImage(:, bestColumn+1:end);

% Display the image.

subplot(2, 2, 3);

imshow(leftImage);

caption = sprintf('Mean = %.3f', meanLeft(bestColumn));

title(caption, 'FontSize', fontSize);

subplot(2, 2, 4);

imshow(rightImage);

caption = sprintf('Mean = %.3f', meanRight(bestColumn));

title(caption, 'FontSize', fontSize);

% c. Bisect each part so that there are now four parts to this picture

% Upper left, Upper right, lower left lower right

midRow = round(rows/2);

partUL = leftImage(1:midRow, :);

partLL = leftImage(midRow+1:end, :);

partUR = rightImage(1:midRow, :);

partLR = rightImage(midRow+1:end, :);

% and obtain the average luminance of each part.

meanUL = mean2(partUL)

meanLL = mean2(partLL)

meanUR = mean2(partUR)

meanLR = mean2(partLR)

% I have no idea what any of the rest of this means.

% d. Obtain the vector of luminance for each part::

% the luminance value multiplied by the distance

% from the point of intersection to the geometric center of the respective part.

% What is the intersection point and what is "the geometric center"? You mena the weighted centroid?

% e. However, for the two upper parts, the x,y value of this vector is replaced by x,(1.07)y. in other words the vertical component is lightened by 1.07

% f. Add the four resultant vectors to obtain the distance from the intersection

##### 0 Comments

Walter Roberson
on 28 Jun 2022

Edited: Walter Roberson
on 28 Jun 2022

"divide the picture vertically such that the two parts (left and right) have equal luminance"

Consider a checkerboard pattern with an even number of rows . If there were an even number of columns then if you divided in the center then the left and right would be identical and so their luminance would be the same. But if the number of columns is odd then when you divide one side has more columns than the other and the luminance cannot be equal.

Therefore the task is not generally possible.

##### 12 Comments

Walter Roberson
on 29 Jun 2022

Means.

peppers only has one balance point; baby.jpg has two balance points, both much further right than you would expect.

format long g

img = imread('peppers.png');

LAB = rgb2lab(img);

L = LAB(:,:,1);

sL = sum(L, 1);

forward = cumsum(sL) ./ (1:length(sL));

backward = fliplr( cumsum(fliplr(sL)) ./ (1:length(sL)) );

difference = forward - backward;

[~, bestidx] = min(abs(difference))

imshow(img)

xline(bestidx);

figure()

plot(difference)

DGM
on 28 Jun 2022

Edited: DGM
on 28 Jun 2022

I'm just going to repost this here, since all the cool kids are posting on this one and the other one is closed anyway. :)

A = imread('peppers.png');

A = rgb2gray(A); % BT601 luma

A = im2double(A);

% split the image into two halves of approx equal weight

Aps = cumsum(sum(A,1),2); % cumulative sum of image profile

lastidx = find(Aps<=(max(Aps)/2),1,'last');

Aw = A(:,1:lastidx); % west half

Ae = A(:,lastidx+1:end); % east half

% show the weights are close to equal

[sum(Aw,'all') sum(Ae,'all')]

% bisect halves

hh = round(size(A,1)/2);

Anw = Aw(1:hh,:);

Asw = Aw(hh+1:end,:);

Ane = Ae(1:hh,:);

Ase = Ae(hh+1:end,:);

% average luma for each quarter [nw; sw; ne; se]

allluma = [mean(Anw,'all'); mean(Anw,'all');

mean(Ane,'all'); mean(Ase,'all')]

% find geometric center vectors

center = [lastidx hh]; % [x y]

allv = [center/2; % [nw; sw; ne; se]

lastidx/2 hh*1.5;

lastidx*1.5 hh/2;

center*1.5];

allv = (allv - center).*[1 1.07; 1 1; 1 1.07; 1 1]

% weight center vectors by luma

allv = allv.*allluma

% find the sum

sumv = sum(allv,1)

##### 12 Comments

Walter Roberson
on 30 Jun 2022

What are you going to do with that information?

The wording suggested a homework assignment to me; if so then there might not be any good reason.

David Corwin
on 30 Jun 2022

##### 3 Comments

DGM
on 2 Jul 2022

Edited: DGM
on 2 Jul 2022

For what it's worth, I'm just going to guess that that's BT2020 luma; at least it's the closest thing I can come up with.

inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1052950/20%20117.76%20127.27%20115.96%20143.58.jpg');

Ay2020 = [0.2627 0.6780 0.0593];

inpict = imapplymatrix(Ay2020,inpict);

mean(inpict(:))

Just looking at an average means I could be completely wrong though.

### See Also

### Community Treasure Hunt

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

Start Hunting!