Convert color images to binary images, which can be implemented in matlab 7 and not run in 2014a

3 views (last 30 days)
codes are as this
clc;
clear all;
close all;
B = imread('1.jpg');
A= rgb2gray(B);
[m,n]=size(A);
for i=1:1:m
for j=1:1:n
if(A(i,j)>110)
A(i,j)=255;
end
end
end
imwrite(A,'1.jpg')
The picture has been placed in the running folder, but the result of running in 2014a is
错误使用 rgb2gray>parse_inputs (line 80)
MAP must be a m x 3 array.
出错 rgb2gray (line 35)
X = parse_inputs(X);
出错 tuxiangchuli (line 6)
A= rgb2gray(B);
>>
why?thanks a lot!

Answers (1)

Image Analyst
Image Analyst on 9 Aug 2020
Only convert to gray scale if the image is definitely RGB. Make sure you use all 3 outputs when using size() with images. See Steve's blog.
And no for loop needed:
B = imread('1.jpg');
[rows, columns, numberOfColorChannels] = size(B);
if numberOfColorChannels == 3
A = rgb2gray(B); % Convert to gray scale.
else
A = B; % It's already gray scale so no conversion needed.
end
mask = A > 110
A(mask) = 255;

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!