Using for loop to loop over pixels in RGB image and brighten the image.

73 visualizaciones (últimos 30 días)
Hello I am new to matlab and looking for some guidance! I am trying to brighten an image by specifically using for loops to loop over each pixel and brighten them one by one. Im aware there are faster methods but this is where im starting. When I run my code, I dont get any errors but my output image looks exactly the same as the before. I was wondering if someone could point me in the right direction and tell me what im doing wrong. Thanks!
b = imread('cell.tif');
imwrite(b,'cell.jpg','jpg')
c = imread('cell.tif');
for x = 1:150 % incriment x from 1 to 150
x = x+1;
for y = 1:150 % incriment y from 1 to 150
y = y+1;
t = b(x,y, :);% print pixel value at location
if t<255
t=t+50;
else if t>=255
t=255;
end
end
b(x,y, :) = t;
end
end
subplot(2,1,1); imagesc(c); axis image; axis off; title('before');
subplot(2,1,2); imagesc(a); axis image; axis off; title('after');

Respuesta aceptada

Image Analyst
Image Analyst el 21 de Sept. de 2021
Not sure what your edit to your original question was, but apparently my first answer was not enough for you to figure it out, so here is a complete solution:
% Demo by Image Analyst
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 short g;
format compact;
originalImage = imread('cell.tif');
% Initialize an output image.
finalImage = originalImage;
[rows, columns, numberOfColorChannels] = size(originalImage)
brightnessIncrement = 50;
for x = 1:150 % Increment x from 1 to 150 (or columns)
for y = 1:150 % Increment y from 1 to 150 (or rows)
t = originalImage(y, x, :);
fprintf('For row %d, column %d, original brightness = %d\n', y, x, t); % Print pixel value at location
finalImage(y, x, :) = t + brightnessIncrement;
end
end
cmap = parula(256);
subplot(2,1,1); imshow(originalImage, [0, 255], 'Colormap', cmap); axis('on', 'image'); title('Before'); colorbar;
subplot(2,1,2); imshow(finalImage, [0, 255], 'Colormap', cmap); axis('on', 'image'); title('After'); colorbar;

Más respuestas (1)

Image Analyst
Image Analyst el 21 de Sept. de 2021
What are you doing with c? Nothing. Anyway, c will be the same as the original b. Where are you actually displaying the images and assigning the titles? That code is not shown.
Do not assign x and y (or any loop iterator variable) inside the loop. The for line takes care of incrementing them so there is no need for you to do it manually.
In your b(x, y, :) you reversed x and y. It should be b(y, x, :) because y is the row and x is the column and arrays are indexed b(row, column, colorChannel)
t is uint8 so it will never get more than 255 so there is no need to check for that. It will just automatically clip it to 255.
When you say
t = b(x,y, :);% print pixel value at location
you're assuming b is an RGB image, though it might not be and the code will work even if it's color. However when you say
if t<255
if t came from a RGB image then t will be a 3 element vector and the if doesn't really make sense
  2 comentarios
nopah hardih
nopah hardih el 21 de Sept. de 2021
Hello thanks for the answer. Some of my code was ommited so i have fixed it now. So here is my thought process.
https://jvns.ca/blog/answer-questions-well/
Since b is the image being manipulated, and c never is, why would the image stored in both be the same? Does matlab change the root image forever when you manipulate it? I would have thought it would save a different output image than the original as to not make irreverible mistakes.
As for the for loop, if I were to put 'for x = 1:150', would this simply incriment x from 1 to 150 by increments of 1?
The instructions specifically say, "You will need to make sure the program does not try to create a pixel value that is larger than the pixel can hold. For instance, an 8-bit image can only hold values 0-255 for each color channel." This is why I thought I would have to check to make sure.
As for t being an RGB image how should I be structuring my if else statement? If I print t, it only prints a single pixle value, not a 3 element vector so im confused on how to do this correctly.
Obviously I am new to matlab, as ive said. Youve pointed out what ive done wrong, so how would you go about this application?
Thanks!
Image Analyst
Image Analyst el 21 de Sept. de 2021
b is from a tiff image and c is from a JPG image. Since c will have compression losses, it will not be the same as b for every pixel, either before or after b is changed. And neither will equal the badly-named a because there is no a defined in your code.
Yes, doing for x=1:150 makes x take on the values 1,2,3,4,5.....150. No need to assign x inside the loop.
What the instructions told you was unnecessary. It will automatically clip the values so there is no need to do it yourself. If you wanted to, you'd first have to case b to double and then values could exceed 255 and then you could clip, but this is totally unnecessary.
Your image is gray scale not color so t has only one value.
See my other answer for the solution.

Iniciar sesión para comentar.

Categorías

Más información sobre Image Data Workflows en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by