Technical Solutions
How can I visually isolate and measure a round object within my MATLAB image?
Date Last Modified: Friday, June 26, 2009
| Solution ID: |
|
1-1BA60 |
| Product: |
|
Image Processing Toolbox |
| Reported in Release: |
|
R13 |
| Platform: |
|
All Platforms |
| Operating System: |
|
All OS |
Subject:
How can I visually isolate and measure a round object within my MATLAB image?
Problem Description:
How can I visually isolate and measure a round object within my MATLAB image?
Do you have an example that shows how to do the following in MATLAB:
1. Select a round object in the image 2. Fill the rest of image with a background color 3. Draw an outline around the object 4. Find the diameter and center of the object
Solution:
Here is an example on how to visually isolate and measure a round object within an image in MATLAB:
I = imread('eight.tif'); imshow(I) BW = roipoly(I);
% manually select region here
BW1 = not(BW); J = roifill(I,BW1); imshow(J)
% using ipexsegcell demo % type 'ipexsegcell' from the MATLAB command prompt % to view documentation for this demo BWs = edge(J, 'sobel', (graythresh(I) * .1)); figure, imshow(BWs), title('binary gradient mask'); se90 = strel('line', 3, 90); se0 = strel('line', 3, 0); BWsdil = imdilate(BWs, [se90 se0]); figure, imshow(BWsdil), title('dilated gradient mask'); BWdfill = imfill(BWsdil, 'holes'); figure, imshow(BWdfill); title('binary image with filled holes'); BWnobord = imclearborder(BWdfill, 4); figure, imshow(BWnobord), title('cleared border image'); seD = strel('diamond',1); BWfinal = imerode(BWnobord,seD); BWfinal = imerode(BWfinal,seD); figure, imshow(BWfinal), title('segmented image'); BWoutline = bwperim(BWfinal); Segout = I; Segout(BWoutline) = 255; figure, imshow(Segout), title('outlined original image');
% find diameter and center of the selected coin [i,j] = find(BWfinal); i1 = unique(i); j1 = unique(j); diameter = max(i)-min(i) center_i = floor(mean(i1)) center_j = floor(mean(j1))
|
|
|
|