How can I use a grayscale image as a input in this piece of code?

1 view (last 30 days)
function RedCellCount(dataImg)
data = imread(dataImg.Key{1});
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
diff_im = medfilt2(diff_im, [3 3]);
diff_im = imbinarize(diff_im,0.18);
diff_im = bwareaopen(diff_im,300);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
fileName = dataImg.Key{1};
display(fileName)
display(length(stats));
I'm trying to load grayscale images in this piece of code from a folder whose path is in another .m file. This piece of code works perfectly fine for RGB images. but when i'm trying to use grayscale images its popping up with these errors:
>> RedCellCount
Not enough input arguments.
Error in RedCellCount (line 2)
data = imread(dataImg.Key{1});
and while using mapreduce() for grayscale images in another function, I got this error.
>> MAP must be a m x 3 array.
I've tried everything to make it run but the program is not accepting grayscale images as it is 1-D whereas RGB images are 3-D matrices.
I'm not able to understand why imread() is working for RGB images but not for grayscale images.
data = imread(dataImg.Key{1});
Any help will be highly appreciated. Thanks in advance.
  4 Comments
Geoff Hayes
Geoff Hayes on 4 Aug 2017
FS - how about you show the full error message when your other function tries to pass the grayscale image into RedCellCount? If it is already an image (like you say) then you cannot pass it to RedCellCount since it is assuming that you are passing in a struct that has a filename. You will need to adapt the code to the new (grayscale image) input parameter.
Image Analyst
Image Analyst on 4 Aug 2017
Before it crashes, put this on it's own line without a semicolon:
dataImg.Key{1}
What do you see in the command window? A string that contains a valid filename?

Sign in to comment.

Answers (1)

Julie Kraus
Julie Kraus on 3 Aug 2017
You can make the grayscale image into an RGB image by using
NewImg=repmat(GreyImg,[1,1,3])
If you get an error due to the fact that it is using decimal points as opposed to intigers 1:256, before you run repmat
GreyImgNew=round(256*GreyImg)
  1 Comment
FS
FS on 4 Aug 2017
Thanks for the suggestion. But how am I going to use it when
data = imread(dataImg.Key{1});
is not able to read grayscale images?. Can you please implement this function using your own logic for grayscale images?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!