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 Center および File ExchangeImage Processing Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by