You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Number of pixel, image outline and image processing
5 views (last 30 days)
Show older comments
Syahrul Niezam
on 1 Feb 2012
I'm still new in Matlab. I had segmented gray scale image into four regions like this.
I would like to calculate area/no. of pixel for each region within each human image. My idea is drawing outline of each human images and then calculate no. of pixel for white, light gray and dark gray regions in the image. Is there any idea to do that, or better idea? What i learnt and found only the method for binary image, not same with this one.
Thanks in advance.
Accepted Answer
Sean de Wolski
on 1 Feb 2012
doc bwconncomp
doc regionprops
doc bwarea
19 Comments
Syahrul Niezam
on 1 Feb 2012
Thank you.
I think, those are basic in calculating area etc. But it supposedly for binary image. Am I correct?
Anton Semechko
on 1 Feb 2012
Yep, regionprops is for binary images, but it also accepts labeled images as input, see documentation for bwlabel
Sean de Wolski
on 1 Feb 2012
regionprops will work on a label image (multiple levels of gray where each level is treated as a separate object). My idea for bewconncomp/bwarea is that you could regroup into binary objects - this may not be th ebest approach however and a direct call to regionprops would work.
Syahrul Niezam
on 1 Feb 2012
Thank you for your responds.
Do you mean I should apply bwconncomp on the image, and then regionprops?
Sean de Wolski
on 1 Feb 2012
It depends. Do you want each blob, regardless of the colors in it, to be treated as one object if the pixels are connected? If so, binarize and use bwconncomp and regionprops. If you want the different colors to be grouped together regardless of their location - regionprops directly.
Syahrul Niezam
on 2 Feb 2012
Is it possible to draw outline of each human, and the rest objects, and then calculate area/no. of pixel for white, light gray and dark gray area?
Syahrul Niezam
on 2 Feb 2012
I'm sorry, what I understand is bwconncomp is only for binary image. It's not working above image. Correct me if I'm wrong.
Walter Roberson
on 2 Feb 2012
Sean said "if so, binarize and use bwconncomp". You did not binarize.
binaryimg = logical(YourImage);
Syahrul Niezam
on 2 Feb 2012
I'm sorry for my mistake.
This is my code that I tried:
c = imread('image1_118.jpg');
a=double(c);
b = zeros(size(a));
b = uint8(a<90);
b(a >= 110) = 180;
b(a >= 150) = 200;
b(a >= 200) = 255;
subplot(121);figure(1);imshow(b);
y=logical(b);
C = bwconncomp(y);
CC = labelmatrix(C);
subplot(122);figure(1);imshow(y);
The bwconncomp part does not work well.
Walter Roberson
on 2 Feb 2012
Please be more specific about "does not work well".
You do not appear to be displaying C or CC.
Syahrul Niezam
on 2 Feb 2012
This is the code to binarize the image using logical. The result image is attached too.
http://i41.tinypic.com/33u3mrp.jpg
c = imread('image1_118.jpg');
a=double(c);
b = zeros(size(a));
b = uint8(a<90);
b(a >= 110) = 180;
b(a >= 150) = 200;
b(a >= 200) = 255;
subplot(221);figure(1);imshow(b);
y=logical(b);
%x=uint8(y);
%C = bwconncomp(y);
%CC = labelmatrix(C);
subplot(222);figure(1);imshow(y);
%subplot(223);figure(1);imshow(C);
However, the result of C did not come out.
Walter Roberson
on 2 Feb 2012
Your quantization is wrong. You assign the value 1 where a < 90, the value 0 where a was 90 to 109, the value 180 where a was 110 to 149, the value 200 where a was 150 to 199, and the value 255 where a was 200 to 255.
When you logical() this, the only parts that will come out 0 will be the parts where you had 0 in the matrix -- that is, the places where a was 90 to 109. Everything else in b is non-zero, including where a was < 90.
You need to re-think what value you want for a < 90 and for a from 90 to 109.
Syahrul Niezam
on 2 Feb 2012
Thank you for your responds.
I would like to assign the value 0 (black) for b where a<90, b=180 where 91<a<110; b=200 where 111<a<200; and b=255 (white) where 201<a<255.
If that possible, are logical, bwconncomp and labelmatrix can be applied?
Walter Roberson
on 2 Feb 2012
binmap = uint8([0 180 200 255]);
[counts, binidx] = histc(a(:), [0 90 110 200 256]);
b = reshape( binmap(binidx), size(a) );
And proceed.
Note: you did not indicate what should happen for a = 90 exactly, or a = 110 exactly, or a = 200 exactly or a = 255 exactly. The code I gave here uses 90 <= a < 110, 110 <= a < 200, 200 <= a < 256.
Syahrul Niezam
on 2 Feb 2012
Ops, i did mistake. It supposedly b=0 if a<=90; b=180 when 90<a=<110; b=200 when 110<a<=150; and b=255 when 150<a<=255.
That what's I desired to do so before calculating no. of pixel in same area and so forth.
Walter Roberson
on 2 Feb 2012
Okay in that case use
[counts, binidx] = histc(a(:), [0 91 111 151 256]);
Syahrul Niezam
on 2 Feb 2012
So, the whole code wlii be like this?
binmap = uint8([0 180 200 255]);
[counts, binidx] = histc(a(:), [0 91 111 151 256]);
b = reshape( binmap(binidx), size(a) );
subplot(221);figure(1);imshow(b);
y=logical(b);
x=uint8(y);
C = bwconncomp(y);
CC = labelmatrix(C);
subplot(222);figure(1);imshow(y);
subplot(223);figure(1);imshow(C);
Syahrul Niezam
on 2 Feb 2012
I would like to ask, is there any idea how to do labeling on non-binary image; like image above? What I understand, bwlabel is for binary (black and white) image only. Is it possible to 'recreate' or renovate itself?
Sean de Wolski
on 2 Feb 2012
A label image can be _ANY_ image if you choose to treat it as such. That is, you can treat equal intesities as objects.
More Answers (1)
Image Analyst
on 1 Feb 2012
See my image segmentation tutorial: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862. I do that exact thing, except for coins instead of humans.
8 Comments
Syahrul Niezam
on 2 Feb 2012
I had studied through the codes. However, it is for binary image. I am not counting the whole area of human image, but area of each white, light gray and dark gray regions within human image. (I hope my English is understandable).
Is it possible to draw outline of each human, and the rest objects, and then calculate area/no. of pixel for white, light gray and dark gray area?
Image Analyst
on 2 Feb 2012
It is not for a binary image. It uses a gray scale image, just like you do. There is a binary image that defines where you look in the gray scale image to compute areas and intensities, etc. but the original image is still gray scale and you should be able to adapt it very easily to your image. Very easily.
Syahrul Niezam
on 2 Feb 2012
I would like to ask, is there any idea how to do labeling on non-binary image; like image above? What I understand, bwlabel is for binary (black and white) image only. Is it possible to 'recreate' or renovate itself?
Image Analyst
on 2 Feb 2012
You can't do labeling on a non-binary image. It doesn't make sense. You have to have an image that separates your image into foreground and background, and that is a binary image. How can you do anything without deciding what is foreground and background? Tell me, does your image have a foreground? Of course it does.
Syahrul Niezam
on 2 Feb 2012
I want to apply labeling in above image, so that I can calculate no. of pixel for each levels; white, light gray, dark gray and black.
http://i44.tinypic.com/2nq8piw.jpg
However, I would like to calculate separately for each subject(human and non-human).
I really hope any code, basic or idea to do that.
Image Analyst
on 2 Feb 2012
First use bwareaopen() to get rid of small regions. Then say
binaryImage = yourImage >= 100 & yourImage <= 150; % Or whatever it needs for the range you're interested in.
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'area');
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)