What is the possible solution to this problem converting to a gray scale image?
2 views (last 30 days)
Show older comments
I am facing this error while running the code for background image subtraction.
>> RemoveBackground('a.jpg','ab.jpg')
??? Error using ==> rgb2gray>parse_inputs at 82
MAP must be a m x 3 array.
Error in ==> rgb2gray at 35
X = parse_inputs(varargin{:});
Error in ==> RemoveBackground at 5
I1=rgb2gray(I);
The complete code is as follows:
function [im]=RemoveBackground(I,Ib)
% Ib=background image
% I=input image
% im=background removed image
I1=rgb2gray(I);
Ib1=rgb2gray(Ib);
Id=imsubtract(Ib1,I1);
Id=Id;
Id=bwareaopen(Id,500,8);
Id=imfill(Id,'holes');
Id=uint8(Id);
[r c]=size(Id);
for i=1:r
for j=1:c
if Id(i,j)==255
Id(i,j)=1;
end
end
end
if size(I, 3) > 1
I = rgb2gray(I);
end
im(:,:,1)=I(:,:,1).*Id;
im(:,:,2)=I(:,:,2).*Id;
im(:,:,3)=I(:,:,3).*Id;
figure,imshow(im)
0 Comments
Accepted Answer
Walter Roberson
on 6 Sep 2015
No, the problem is that you are passing the name of an image rather than the content of the image. You are processing strings, not images.
0 Comments
More Answers (2)
Image Analyst
on 3 Sep 2015
You forgot to attach your images. Why are you trying background subtraction? It's usually for modalities like radiology and fluorescence, and not for things like digital RGB cameras. What are you using?
Your problem is you're trying to call it on a gray scale image instead of a color image.
Try this:
[rows, columns, numberOfColorChannels] = size(I);
if numberOfColorChannels == 3
% If it's color, convert it to gray level.
I1 = rgb2gray(I);
else
I1 = I; % Already grayscale.
end
[rows, columns, numberOfColorChannels] = size(Ib);
if numberOfColorChannels == 3
% If it's color, convert it to gray level.
Ib1 = rgb2gray(Ib);
else
Ib1 = Ib; % Already grayscale.
end
8 Comments
Image Analyst
on 8 Sep 2015
Like this:
a = imread('a.jpg')
b = imread('ab.jpg');
RemoveBackground(a,ab);
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!