You are on page 1of 4

[digital image processing]

December 9, 2012

HOW TO CALCULATE ORIGINAL SIZE OF OBJECTS IN DIGITAL IMAGE BY MATLAB

In this article, I will elaborate how do we calculate size of objects in digital image by Matlab. This size is the original one, which is measure in cm. We actually do not need sophisticated algorithm to do that. But when the combination of objects get complicated, we are gonna need some algorithm to solve. Why do we need such algorithm? To extract the objects from background, then distinguish each of them and decide what kind of objects they are. I have prepared some images that contain an object which is square and use these images as input to program. Below are steps I arrange in order to accomplish our task: Read the image(s) Convert to binary file If the detected objects are not white colored then inverse it If the object more than one then distinguish them and then process it individually Get width and height of the object then divide it with value of resolution. One can take the value of image resolution by using IMFINFO from matlab. Convert size of object to cm if resolution in inch or let it as it is if already in cm.

Okay, based on the steps I mention above I already made a program to fulfill the task. Lets take a look.
clear all; close all; clc; %% start filename = % filename % filename % filename

'back.jpg'; % resolusi = 96 = 'back2.jpg'; % resolusi = 200 = 'back3.jpg'; % resolusi = 300 = 'back4.jpg';

I = imread(filename); info = imfinfo(filename); thres = graythresh(I); I2 = ~(im2bw(I, thres)); cmp = bwconncomp(I2); S = regionprops(cmp, {'BoundingBox'}); x = S.BoundingBox(3);

[ janshendry@gmail.com ]

Page 1

[digital image processing]

December 9, 2012

y = S.BoundingBox(4); res = info.ResolutionUnit; resX = info.XResolution; resY = info.YResolution; if strcmp(res, 'Inch') lebar = 2.54 * x / resX; tinggi = 2.54 * y / resY; else lebar = x / resX; tinggi = y / resY; end fprintf('X Resolution = %.2f %s\n', resX, res); fprintf('Y Resolution = %.2f %s\n', resY, res); disp('Size of Object:'); fprintf('Width = %.2f cm\n', lebar); fprintf('Height = %.2f cm\n', tinggi); %% end

When I run this program, I get results below: I have put an square object in an image, size of the object is 2 cm x 2 cm. Now, we ask Matlab to calculate it by using steps we made above.

Result

[ janshendry@gmail.com ]

Page 2

[digital image processing]

December 9, 2012

Result

Result

[ janshendry@gmail.com ]

Page 3

[digital image processing]

December 9, 2012

Result

We finally find the edge. If you have any doubt or question, just send me email. Lets discuss about it.

@thanks

[ janshendry@gmail.com ]

Page 4

You might also like