What is the possible solution to this problem converting to a gray scale image?

2 views (last 30 days)
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)

Accepted Answer

Walter Roberson
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.

More Answers (2)

Image Analyst
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
Innovative
Innovative on 8 Sep 2015
Thank You Walter Roberson for the suggestion and Thank you Image Analyst for the syntax. Both suggestion and syntax worked and My code got debugged!!
The basic entity where I were going dumb were these commands. im(:,:,2)=I(:,:,2).*Id; im(:,:,3)=I(:,:,3).*Id;
I just commented them , ran the code with this : im(:,:,1)=I(:,:,1).*Id; and followed the syntax mentioned by Image Analyst and My problem got solved.

Sign in to comment.


Innovative
Innovative on 8 Sep 2015
Thenk You very much Image Analyst and Walter Roberson for Thy Timely HELP !!

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!