What is the meaning of if (ndims(a)==2 | (a(:,:,1)==a(:,:,2) & a(:,:,2)==a(:,:,3))) this line?

2 views (last 30 days)
My code is
clc
clear all
close all
dirpath = 'E:\4-1\image-thesis\implementation\shaila\';
f=dir(fullfile(dirpath,'*.jpg'));
f = f(arrayfun(@(x) x.name(1), f) ~= '.');
for i=1:length(f)
a=imread(strcat(dirpath,f(i).name));
if (ndims(a)==2 | (a(:,:,1)==a(:,:,2) & a(:,:,2)==a(:,:,3)))
%if (ndims(a)==2)
figure;imshow(a)
end
end
What is the meaning of if (ndims(a)==2 | (a(:,:,1)==a(:,:,2) & a(:,:,2)==a(:,:,3))) this line?
  1 Comment
Matt Kindig
Matt Kindig on 28 May 2013
Edited: Matt Kindig on 28 May 2013
The 'if' statement has the following logic:
1. If the number of dimensions of the image is 2, then imshow() the image.
2. If the number of dimensions is not 2, but all three "planes" of the image (i.e., the values in the third dimension) are all the same, imshow() it.
Basically, this code is reading images (with imread()), and, if a) the image is an indexed image, or b) the image is an RGB image with R, G, and B all the same (which occurs for a grayscale image), then display it using imshow(). Otherwise, skip it.

Sign in to comment.

Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!