difference between two programs
显示 更早的评论
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 个评论
John D'Errico
2019-10-19
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
2019-10-19
采纳的回答
更多回答(2 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!