Using OR logical operator

3 次查看(过去 30 天)
Avinash Bhatt
Avinash Bhatt 2019-7-10
I am using R2013 Matlab and trying to calculate the number of pixels which are greater than 0 and less than 255 using 5X5 window. But every time I run the code, code displays 25 when there are only 3 pixels in the image.
clc
clear all
close all
originalimage=imread('Test.jpg');
figure(1),imshow(originalimage);
title('Original Image');
grayimage=rgb2gray(originalimage);
figure(2),imshow(grayimage);
title('Gray Image');
noisyimage=imnoise(grayimage,'salt & pepper',0.90);
figure(3),imshow(noisyimage);
title('Corrupted Image');
a=sum(grayimage);
% Placment of Window over image
count=0;
a=5;
b=5;
for i=1:a
for j=1:b
%iwn(i,j)
if noisyimage(i,j) > 0 || noisyimage(i,j) < 255
count=count+1;
end
end
end
fprintf('Number of non-corrupted pixels is :');
disp(count)
Please help me solving this issue.
I have attached the test image
  2 个评论
vidhathri bhat
vidhathri bhat 2019-7-10
If you want pixels which are greater than 0 and less than 255, shouldn't you be using logical AND operator
Steven Lord
Steven Lord 2019-7-10
What is the class of your noisyimage variable? If it's an unsigned 8-bit integer (uint8), the elements of that variable cannot be outside the range [0, 255] as that's the allowed range of values for that data type.

请先登录,再进行评论。

回答(2 个)

KSSV
KSSV 2019-7-10
I = imread(myimage) ;
idx = I>= 0 & I<=255 ;

Peter Jarosi
Peter Jarosi 2019-7-10
编辑:Peter Jarosi 2019-7-10
Change your OR operator to AND!
originalimage=imread('Test.jpg');
figure(1),imshow(originalimage);
title('Original Image');
grayimage=rgb2gray(originalimage);
figure(2),imshow(grayimage);
title('Gray Image');
noisyimage=imnoise(grayimage,'salt & pepper',0.90);
figure(3),imshow(noisyimage);
title('Corrupted Image');
a=sum(grayimage);
% Placment of Window over image
count=0;
a=5;
b=5;
for i=1:a
for j=1:b
%iwn(i,j)
if noisyimage(i,j) > 0 && noisyimage(i,j) < 255
count=count+1;
end
end
end
fprintf('Number of non-corrupted pixels is :');
disp(count)
Consider to avoid for loops, becase the following is much faster especially for large images:
originalimage=imread('Test.jpg');
figure(1),imshow(originalimage);
title('Original Image');
grayimage=rgb2gray(originalimage);
figure(2),imshow(grayimage);
title('Gray Image');
noisyimage=imnoise(grayimage,'salt & pepper',0.90);
figure(3),imshow(noisyimage);
title('Corrupted Image');
a=sum(grayimage);
% Placment of Window over image
a=5;
b=5;
idx = (noisyimage(1:a,1:b) > 0) & (noisyimage(1:a,1:b) < 255);
count = sum(sum(idx));
fprintf('Number of non-corrupted pixels is :');
disp(count)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by