darken an image issue

14 visualizaciones (últimos 30 días)
Brandi Abele
Brandi Abele el 13 de Mzo. de 2018
Comentada: Walter Roberson el 8 de Oct. de 2024
I am trying to adjust an image and make it darker, I also need to change the forloop so that it only makes 1/2 of the image dark. Here is the code that I have so far. It works to pull up my image but doesn't adjust the color
function [output] = darken(filename, darkness_number)
if ~strcmp(class(filename), 'char')
disp('filename must be a string')
return;
end
if (darkness_number>255|darkness_number<0)
disp('darkness_number out of range')
return;
end
image_matrix=imread(filename);
imshow(image_matrix);
output=zeros(size(image_matrix));
for i=1:size(image_matrix,1)
for j=1:size(image_matrix,2)
output(i,j)=image_matrix(i,j)+max(output(i,j)-darkness_number);
end
end
end
  1 comentario
Walter Roberson
Walter Roberson el 8 de Oct. de 2024
output(i,j)=image_matrix(i,j)+max(output(i,j)-darkness_number);
darkness numberr is presumably a scalar.
output(i,j) is a scalar. output(i,j)-darkness_number is a scalar. max() of a scalar is the scalar.
Note: your code assumes that imread() is returning a 2D array. That is not a good assumption.

Iniciar sesión para comentar.

Respuestas (1)

DGM
DGM el 8 de Oct. de 2024
Editada: DGM el 8 de Oct. de 2024
It is darkening the image, but the output is the wrong class. You're presuming that all images are single-channel grayscale images in uint8. Then you preallocate the output in double-precision float instead of uint8, so the output is always improperly-scaled.
% read an image, presume it's the correct type
% clobber the figure and transform the image
outpict = darken0('cameraman.tif',32); % an improperly-scaled image
imshow(outpict) % displayed on the wrong scale
% this shouldn't be necessary
outpict = uint8(outpict); % fix the class
imshow(outpict) % display the result
There's no point in preallocating anyway, because the loops serve no purpose. The entire function can be reduced to three lines, and it actually supports more types of images.
inpict = imread('cameraman.tif'); % read the image
outpict = darken(inpict,32); % transform the image
imshow(outpict) % show the result
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function outpict = darken(inpict,darkenamt)
% SYNOPSIS GOES HERE
% clamp the parameter
darkenamt = min(max(darkenamt,0),255);
% if you're going to presume the input class, enforce it
inpict = im2uint8(inpict);
% that entire pile of loops simplifies to this
outpict = inpict - darkenamt;
end
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [output] = darken0(filename, darkness_number)
% use error() instead of disp() to actually halt execution.
if ~strcmp(class(filename), 'char')
disp('filename must be a string')
return;
end
% just clamp the parameter instead of barfing unnecessary errors
% pay attention to the linter squiggles
if (darkness_number>255 | darkness_number<0)
disp('darkness_number out of range')
return;
end
% this could be any number of numeric classes (or logical)
% it could be grayscale or RGB or indexed color,
% and it may or may not have transparency information.
% unless you're going to internally handle
% all the types of output that imread() will produce,
% don't write functions that do the file reading internally.
% do it outside the function where it can be managed.
image_matrix = imread(filename);
% why would you want to see the input image for 50ms
% before the subsequent code replaces it with something else?
% don't write utilities that clobber figures for no purpose.
% do this outside the function where it can be managed.
%imshow(image_matrix);
% your preallocated array is the wrong class for the presumed input class
output = zeros(size(image_matrix)); % double
% these loops are unnecessary window-dressing
for i = 1:size(image_matrix,1)
for j = 1:size(image_matrix,2)
% the call to max() does nothing, since the argument is a scalar
% since output(i,j) is always zero, there's no point in it being in the expression.
output(i,j) = image_matrix(i,j) + max(output(i,j)-darkness_number);
end
end
end
I have no idea why the transformation is written with a bunch of superluous things. Maybe it was supposed to be something else, but it's not. Likewise, nothing about this darkens half the image. It's not even clear what "half" means in this context.
See also:

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by