Image processing ''if input is logical (binary), it must be two-dimensional error "

Hello,
I have an image extracted from the optical microscopy. While using the code below the I, I2, K are given as a unit8 in Matlab. However, while using binary the bw becomes a logical one and Matlab could not provide a figure. Does anybody know how can I correct this error?
First, I thought that it was because of the .jpg format of the photo extracted from optical microscopy. Actually, the microscope does have just .tiff and .jpg format. It seems that changing the figure format to .png also does not work. Maybe it should be extracted as .png!
Does any body have any idea?
figure(1)
I = imread('main.jpg');
imshow(I)
se = strel('disk',15);
background = imopen(I,se);
figure(2)
I2 = I - background;
imshow(I2)
figure(3)
K = imadjust(I2,[0.05 0.2],[]);
imshow(K)
figure(4)
bw = imbinarize(K);
bw = bwareaopen(bw,50);
%bmw = rgb2gray(bw);
imshow(bw)

 Accepted Answer

bw = imbinarize(rgb2gray(K));
imshow(bw)
Complete Code:
I = imread('main.jpg');
figure(1), imshow(I)
se = strel('disk',15);
background = imopen(I,se);
figure(2), figure(2)
I2 = I - background;
figure(3),imshow(I2)
K = imadjust(I2,[0.05 0.2],[]);
figure(4),imshow(K)
bw = imbinarize(rgb2gray(K));
figure(5),imshow(bw)
bw=bwareaopen(bw,50);
figure(6),imshow(bw)

6 Comments

Thank you Dr. Kaylan,
Would you please also let me know how can we calculate the area of t he black area in the figure in help document?Attzhed is the link:
I mean we have the grain area and I want to calculate the non-grain area or area of the whole plate considering that I do not have the scale of the figure.
Let say image is image1
im_data=imread('image1,jpg');
bw_image=im2bw(rgb2gray(im_data));
[r c]=size(im_data);
non_grain_area=(r*c-sum(bw_image));
grain_area=sum(bw_image);
You can also find the percentage by dividing all by r*c
Hope it helps!
Sure! Thanks.
Just one more question. Why MATLAB gives the white part of the picture in the imread('main.jpg')? I mean why not black part? Is it a standard or so?
Hello,
You said that r*c gives the total area of the picture. What exactly is this area in scale? What does it mean by size?
Also, when I change this radius 15 in se = strel('disk',15); the answer is changed. What is this exactly?
I mean when I divide the total grain area to that total area sometimes it gives me an anormal number and it depends on the radius and the numbers we used in K = imadjust(I2,[0.05 0.2],[]);!
Area in term of pixels, we know the total number of pixel in the image. Certain number of values of pixels/total number of the pixels
If you looking in terms of cm,mm etc, its different issue.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!