Using OR logical operator

3 visualizzazioni (ultimi 30 giorni)
Avinash Bhatt
Avinash Bhatt il 10 Lug 2019
Commentato: Steven Lord il 10 Lug 2019
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 Commenti
vidhathri bhat
vidhathri bhat il 10 Lug 2019
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 il 10 Lug 2019
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.

Accedi per commentare.

Risposte (2)

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

Peter Jarosi
Peter Jarosi il 10 Lug 2019
Modificato: Peter Jarosi il 10 Lug 2019
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)

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by