Showing posts with label image procecssing. Show all posts
Showing posts with label image procecssing. Show all posts

Detecting Objects' Shape(I) : Round Object

With the powerful technical computing language, the job of detection an object's shape no longer require lengthy codes. What important is the fundamental of mathematic that define the properties of the shape of objects. Let's look into the detection of round object in this section.


1. Original image and the negative image (value 0 represent background while 1 represents object).
S = imread('p6-in1.jpg');
ShowColorImage(S,'Original Image');


S2 = im2bw(S,0.5);
ShowImage(S2,'Binary Image');


2. Labeling the objects in the image. The advance programming language will support this function and label the objects automatically.

// Labeled the component(s), and plot the centroid on the original image
S3 = SearchBlobs(S2);
ShowImage(S3,'Labeled Image');



3. Getting the properties of the objects that require for calculation, which are: the width and height for the object, the area of the object, and the centroid of the object.

ToBeCalculated = CreateFeatureStruct(%t); // Feature struct is generated.
S4 = AnalyzeBlobs(S3,ToBeCalculated);

4. Now, evaluate the "roundness" of the objects. this is evaluated by 2 criteria:
a. Comparing the width and the height diameter for the object,
b. Comparing the Area with the formula pi*r^2.
The "scores" are normalized so that 1 represent "best fit"

for cnt = 1:length(S4)
score1(cnt) = 1-abs(S4(cnt).BoundingBox(3)-S4(cnt).BoundingBox(4))/max([S4(cnt).BoundingBox(3),S4(cnt).BoundingBox(4)]);

score2(cnt) = 1 - abs(%pi*((mean([S4(cnt).BoundingBox(3),S4(cnt).BoundingBox(4)]))/2)^2-length(S4(cnt).PixelIndexList))/length(S4(cnt).PixelIndexList);

end
score = mean([score1 score2],'c');

5. Display the result visually

ShowImage(S2,'0');
[m,n] = size(S2);

for cnt = 1:length(S4)
xnumb(S4(cnt).Centroid(1),m-S4(cnt).Centroid(2),score(cnt));
t = gce();
t.font_foreground = color('red');
end



So now, can you tell which one is the round object by just looking at the "roundness index"?

Drawing Shapes by Overwriting Pixel Value

1. How to highlight certain portion on an image?

The basic concept of highlighting part of an image is “Overwriting Pixel Value” on the image. We start from the basic idea on how to mask portion of image with blank sub-image (black color sub-image). Figure below shows the image with the size of 128x128x3 in which 3 represents RGB. (In grayscale image 3 layers are the same). The second image at the right hand site shows a black sub-image is placed at the upper left of the original image. The Scilab code to perform the operation are as follow:



S = imread('p2-in1.jpg');
S2 = S;
S2(:,:,2) = S;
S2(:,:,3) = S; // Create a gray-scale RGB Image
S = S2; // Save a copy of image
ShowColorImage(S2,'0');



Sblack = uint8(zeros(20,20,3));
S2 = S;
S2(1:20,1:20,:) = Sblack;
ShowColorImage(S2,'0');




2. How to create a color mask rather than black color mask?

Since the 3 layers of image matrix represent RGB layers of the image, we can create the red color mask using following command, and the results are shown as shown.

Sred = Sblack;
Sred(:,:,1)=255;
S2 = S;
S2(1:20,1:20,:) = Sred;
ShowColorImage(S2,'0');



3. How to create a transparent mask?

Simple enough, just play with the values R value, and perform the image addition rather than overwriting the value as follow:

Sred = Sblack;
S2 = S;
S2(1:20,1:20,1) = 255;
ShowColorImage(S2,'0');




4. Finally, how to create an outline for the image?

linelength = 10;
Sblack = uint8(zeros(128+linelength*2,128+linelength*2,3));
S2 = Sblack;
S2((1+linelength):($-linelength),(1+linelength):($-linelength),:)=S;
ShowColorImage(S2,'0');




Note: To run example in this article, please follow the installation of Scilab and toolboxes at previous post.