Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
we were tasked to simply find the orientation of an object in an image.

we were able to find an algorithm which involves using the moments of the image. problem is that we were confused with the results...

the codes we have are as follows:

FOR GETTING THE MOMENTS

C#
public void getMoments()
        {
            int temp = 0;
            for (int x = 0; x < picture.Width; x++)
            {
                for (int y = 0; y < picture.Height; y++)
                {
                    temp = getIntensity(x, y);
                    m00 += temp;
                    m10 += x * temp;
                    m01 += y * temp;
                    m20 += Math.Pow(x, 2) * temp;
                    m02 += Math.Pow(y, 2) * temp;
                    m11 += x * y * temp;
                }
            }
            u11 = m11 - ((m10 * m01) / m00);
            u20 = m20 - ((m10 * m10) / m00);
            u02 = m02 - ((m01 * m01) / m00);
        }


FOR GETTING ORIENTATION
C#
public double computeOrientation()
       {
           getMoments();
           theta = (double).5 *Math.Atan((2 * u11) / (u20 - u02));
           toDegrees();
           return theta;
       }



are our codes wrong somewhere?
Posted

You may use different spatial filters for different directions. If you give weights on the vertical for 3 x 3 or 5 x 5 kernel it masks everything but only shows the vertical lines i.e computation of moments will weighted for only one direction. Same way for horizontal, diagonal etc., assign different color at each filter operations. You will get nice edges. One of such spatial filter is sobel edge filter. http://www.roborealm.com/help/Sobel.php[^]Now you may compute your algorithm how to handle those edges, because an object can have a horizontal, vertical or any angle lines. You have to have an algorithm to find the major axis of it.

Accord .Net is a free library in c# http://accord-net.origo.ethz.ch/[^] which has predefined function which may be useful.

Lot of sophisticated high tech object oriented image segmentation algorithms out there. What I given is a simple idea.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Apr-11 15:19pm    
Nice, a 5,
--SA
Lady Malie 16-Apr-11 23:45pm    
thanks a lot... but i am kinda lost with the kernel part.
Lady Malie 17-Apr-11 4:36am    
how do you use the library? sorry i feel like i'm a total noob...
Albin Abel 17-Apr-11 13:04pm    
Hi Lady Malie, Accord.Net is an extension of AForge.Net. You can find the information of image filters of AForge.Net from here http://www.aforgenet.com/framework/docs/html/cdf93487-0659-e371-fed9-3b216efb6954.htm. http://www.aforgenet.com/framework/features/edge_detectors_filters.html

http://www.aforgenet.com/framework/docs/html/2c8218cc-921c-34d8-5c88-39c652488490.htm

http://www.aforgenet.com/framework/features/morphology_filters.html

If you want to have custom kernels then it is explained here ..
http://www.aforgenet.com/framework/features/convolution_filters.html

To startwith you have to download Aforge.Net and install..Then you may follow this code project article for a few exercises
http://www.codeproject.com/KB/recipes/aforge.aspx
http://www.codeproject.com/KB/GDI-plus/Image_Processing_Lab.aspx

Hoping I have provided enough information
Lady Malie 17-Apr-11 23:40pm    
I'll try these out... thanks..
I am afraid you didn't describe your problem. What do you mean by "we were confused with the results" ???

You are indeed computing the orientation of the ellipse of inertia. This makes sense if the image is binary (white object over a black background) or close to that.

Your formulas look ok. Some of the accumulators can overflow (if they are integer).

Your call of toDegrees is supicious, it has no argument. So I doubt that theta is returned in degrees.

And please, avoid Math.Pow(x, 2), use x * x instead, faster and more accurate.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900