Click here to Skip to main content
12,996,841 members (135,085 online)
Rate this:
Please Sign up or sign in to vote.
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[^]

below has code

           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)
                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[^]
Posted 24-May-12 21:39pm
Updated 25-May-12 6:00am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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?
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
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;
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170622.1 | Last Updated 26 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100