Filter löschen
Filter löschen

Using OR logical operator

2 Ansichten (letzte 30 Tage)
Avinash Bhatt
Avinash Bhatt am 10 Jul. 2019
Kommentiert: Steven Lord am 10 Jul. 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 Kommentare
vidhathri bhat
vidhathri bhat am 10 Jul. 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 am 10 Jul. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

KSSV
KSSV am 10 Jul. 2019
I = imread(myimage) ;
idx = I>= 0 & I<=255 ;

Peter Jarosi
Peter Jarosi am 10 Jul. 2019
Bearbeitet: Peter Jarosi am 10 Jul. 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)

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by