Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more: , +
How Crop biggest object from the image and find center pixel of Cropped image
I did it using BlobCounter function using AForge library (opencv ) but not working

http://img24.imageshack.us/img24/4666/cropimageh.jpg[^]

below has code

C#
   System.Drawing.Bitmap aq = (Bitmap)pictureBox1.Image;
   Invert a = new Invert();
    aq = a.Apply(aq);
    AForge.Imaging.Image.FormatImage(ref aq);

    /// apply grayscale
    IFilter filter = Grayscale.CommonAlgorithms.BT709;
    aq = filter.Apply(aq);

    Threshold th = new Threshold(220);
    aq = th.Apply(aq);

    ///find the biggest object
    BlobCounter bl = new BlobCounter(aq);
    int i = bl.ObjectsCount;
    ExtractBiggestBlob fil2 = new ExtractBiggestBlob();

    int x = 0;
    int y = 0;
    int h = 0;
    if (i > 0)
    {
        fil2.Apply(aq);
        x = fil2.BlobPosition.X;
        y = fil2.BlobPosition.Y;

        h = fil2.Apply(aq).Height;
    }

    System.Drawing.Bitmap Bitmapsource = (Bitmap)pictureBox1.Image;
    Rectangle section = new Rectangle(new Point(x - h, y - h), new Size(1 * h, 1 * h));

    Bitmap CroppedImage = CropImage(Bitmapsource, section);
    pictureBox2.Image = CroppedImage;
}
    //get crap image


    public Bitmap CropImage(Bitmap source, Rectangle section)  {
    Bitmap bmp = new Bitmap(section.Width, section.Height);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
    return bmp;}
}


this the thing i want to do..
crap image from original and get center pixel

http://img829.imageshack.us/img829/5199/centerpicel.jpg[^]
Posted
Updated 25-May-12 6:00am
v2

I have no experience in AForge, but in image processing in general.
- first of all, I am not sure that converting to grayscale is a good idea. You might loos significant information.
- try to build and test your app step by step. In the aforge documentation[^] the sample contains only two statements: create the extractor filter and apply it to the image. The result will be the bigest object. Save it, or show it in your test app, to see if it works.
- did you intentionally re-apply the filter (h = fil2.Apply(aq).Height;)? Better save it on the first apply.
- are you sure that the height of the result is equal to it's width?
- by the way, what is the center pixel of an object?
 
Share this answer
 
v2
Comments
chamara54 25-May-12 11:23am    
first of all thank you for reading my problem :)
after crap the image i need get center pixel value ( It's may 1 X 1 or or 3X 3)
eg: example
http://img829.imageshack.us/img829/5199/centerpicel.jpg
Zoltán Zörgő 25-May-12 12:20pm    
I have understood even from your initial post. But I don't see that you are coding what you want to do. What you extract is not the center pixel. It's a region containing it. By the way, it should be either 1x1 pixels or 2x2 (not 3x3, since that would be 1x1 also).
I will install aforge and give it a try...
chamara54 28-May-12 2:15am    
thanks again for consider my problem by the way u don't need install AForge

just need only AForge DLL and reference

(AForge ,AForge.Math,AForge.Imaging dll''s only )
using AForge;
using AForge.Math;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Imaging.Textures;
How to get the biggest object? Mmmmm,a visual object can range from simple to complex, I bet that code should be used in the context in which it was designed, don't expect that code to find the center pixel of a face, for example. :)

Try using simple images, more like synthesized images unlike noisy images such as the leaf from web cameras or digital cameras. Most of the current computer vision algorithms I have come across are prone to noise and hence are more suited to what are known as "toy problems".

If you need to use real world imagery then your app need to do a lot more of preprocessing to extract relevant features and some knowledge about possible or predefined objects need to be used as prototypes in more complex situations.
 
Share this answer
 
Hi! I have a little experience with OpenCV and I have developed many test applications. Since it were only test I didn't delve deep in that matter, but by the way I have an easier soution.
First of all use the contours and create an edged image using canny edge detection algorithm. Then loop through all the contours and store a rectangle or better 2DBox in an array of variable cvBox2D (struct). Now compare the size of rectangle, you can check if height AND width are greater or compare the height width and Area so that you have more parameter to finally decide which one is biggest contour. Store this contour in another contour variable.
Now to find the center pixel, Use a simple approximate method. Just use the same 2D box variable, there's a parameter called 'center' built inside the struct.
Hope it will help you in some ways.
Thank you.
 
Share this answer
 

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