difference between two programs
Show older comments
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 Comments
John D'Errico
on 19 Oct 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
on 19 Oct 2019
Accepted Answer
More Answers (2)
boutheina gherib
on 19 Oct 2019
0 votes
boutheina gherib
on 20 Oct 2019
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!