difference between two programs
Mostrar comentarios más antiguos
I have two small programs which must have the same result but in executing them I have some difference and I don't understand where is the problem. The task is to write a simple stand-alone program that converts the loaded source image in the negative one.
X=imread('mosque.jpg');
imshow(X);
[n,m]=size(X);
B=zeros(n, m, 'uint8');
for i=1:n
for j=1:m
B(i,j)=255-X(i,j);
end
end
subplot(2,2,1);
imshow(X);
subplot(2,2,2);
imhist(X);
subplot(2,2,3);
imshow(B);
subplot(2,2,4);
imhist(B);
the second program without the for
X=imread('mosque.jpg');
imshow(X);
[n,m]=size(X);
B=zeros(n, m, 'uint8');
B=255.-X;
subplot(2,2,1);
imshow(X);
subplot(2,2,2);
imhist(X);
subplot(2,2,3);
imshow(B);
subplot(2,2,4);
imhist(B);
2 comentarios
John D'Errico
el 19 de Oct. de 2019
Note that for these two statements:
B=zeros(n, m, 'uint8');
B=255.-X;
The first is irrelevant. There is no need to preallocate B there, because the second line just overwrites it.
Had you done this instead?
B = 'Mississippi';
B=255.-X;
The result would have been the same.
However, in the case of the for loops, since there you are populating B one element at a time, it is very important to preallocate B.
boutheina gherib
el 19 de Oct. de 2019
Respuesta aceptada
Más respuestas (2)
boutheina gherib
el 19 de Oct. de 2019
0 votos
boutheina gherib
el 20 de Oct. de 2019
Categorías
Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!