Using OR logical operator

조회 수: 4 (최근 30일)
Avinash Bhatt
Avinash Bhatt 2019년 7월 10일
댓글: Steven Lord 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