how to make this code make the output as an image?
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mohamed Arafa
le 15 Avr 2022
Commenté : Walter Roberson
le 9 Nov 2023
this code is supposed to import image (dice) then make edits on it using the 2 for loops i added. everything works fine and the image is imported correctly but the output (out) is shown as a matrix not as a black and white image which is what i want.
function out = rec7a2(in1,in2)
in2 = 100;
in1 = imread('dice.jpeg') % a unit8 image input
in1 = im2gray(in1); %change it to black and white
for i = 1:size(in1,1)
for j = 1:size(in1,2)
if in1(i,j) >= in2
out(i,j) = 255 - in2;
else
out(i,j) = in2;
end
end
end
end
0 commentaires
Réponse acceptée
Walter Roberson
le 15 Avr 2022
out(i,j) = 255 - in2;
You are building up a numeric array, and that numeric array is your output variable.
MATLAB does not have any way to store "black and white image" as such. Unless you were to design your own object class, MATLAB cannot do something like:
outimg = rec7a2('flamingos.jpg', 'foggy.png');
to have outimg be an "image" that would show up in graphic form if you were to
disp(outimg)
What MATLAB does have is numeric arrays that can be interpreted as images, and MATLAB has graphics objects that give information about image arrays already drawn on the screen as images.
You could change your code so that it was something like
function outimg = rec7a2(in1,in2)
%then your math stuff
outimg = image(out);
colormap(outimg.Parent, gray(256));
end
That would then return a handle to the graphics object that has displayed the array out as a black and white image. But you would not then be able to process the array of data further -- not unless you fetched it from the graphics handle.
Side note: you really shouldn't ignore your input variables in1 and in2 . If you are going to hard-code those in your function, then there is no point in accepting inputs.
2 commentaires
Walter Roberson
le 19 Avr 2022
function out = rec7a2(in1,in2)
in2 = 100;
in1 = imread('dice.jpeg') % a unit8 image input
Do not do that. Instead create test7a2.m
in2 = 100;
in1 = imread('dice.jpeg') % a unit8 image input
out = rec7a2(in1, in2) ;
and
function out = rec7a2(in1,in2)
% in1 should be image array
% in2 should be numeric threshold
without assigning to in1 or in2 inside the function
Plus de réponses (1)
Milton
le 9 Nov 2023
Quero todo código completo. Output, erosão, dilação entre outros
1 commentaire
Walter Roberson
le 9 Nov 2023
Approximate translation:
I want all the complete code. Output, erosion, dilation among others
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!