How to code image types?
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
Hello everyone.
How can I write the "if-else" loop so that MATLAB will identiy it is a Gray/Binary image?
Thanks in advance!
댓글 수: 0
채택된 답변
  Image Analyst
      
      
 2020년 7월 5일
        Try this:
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
	% It's not really gray scale like we expected - it's color.
	% Use weighted sum of ALL channels to create a gray scale image.
% 	grayImage = rgb2gray(grayImage);
	% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
	% which in a typical snapshot will be the least noisy channel.
	grayImage = grayImage(:, :, 1); % Take red channel.
end
if max(grayImage(:)) == 1 && length(unique(grayImage(:))) == 2
    % It's binary because it has only two values: 0 and 1
else
    % It's grayscale
end
You can also use isa(grayImage, 'logical') or similar to identify what class it is.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

