-1

I'm making a labeling tool.

Goal :By drawing a polygon on the picture, you have to export the image ​​inside the polygon to the outside.

example ![Example extract enter image description here

This is what I drew in the my program.

my program polygon

But I don't know how to extract this region. I want to know how to extract this area. I have saved the vertices of the picture above in an object. But I don't know how to extract data from the image through these vertices

========================================

So I found this. https://www.codeproject.com/Articles/703519/Cropping-Particular-Region-In-Image-Using-Csharp

but it is not work

Can't convert Bitmap to IplImage

It doesn't work for the same reason. In the post, I am going to use opencvsharp 4.x, but the program I am fixing now is .netframework 3.5, so it does not support opencvsharp 4.x. What should I do?

============================

I made a function referring to the answer, but it doesn't work... I want to know why.

  void CropImage(Bitmap bitmap, Point[] points)
        {
            Rectangle rect = PaddingImage(points, bitmap);
            TextureBrush textureBrush = new TextureBrush(bitmap);
            Bitmap bmp1 = new Bitmap(rect.Width, rect.Height);
            using (Graphics g = Graphics.FromImage(bmp1))
            {
                g.FillPolygon(textureBrush, points);
            }

            string ima_path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            bmp1.Save(ima_path + "\\Image.png", ImageFormat.Png);
        }

extract Image

enter image description here

original

enter image description here

If you use a small polygon, there is no output at all.

enter image description here

You will notice that the two images are slightly different.

It seems to me that the part where the center point is cut and extracted is different. I don't know if what I was thinking is correct.


로다현
  • 125
  • 1
  • 12
  • I expect your answer is [here](https://stackoverflow.com/questions/30954503/how-to-crop-a-polygonal-area-from-an-image-in-a-winform-picturebox). – ProgrammingLlama Dec 07 '21 at 06:21
  • @Llama Isn't that a square cut? The function that determines the minimum rectangle is already in use. – 로다현 Dec 07 '21 at 06:27
  • [See this comment](https://stackoverflow.com/questions/30954503/how-to-crop-a-polygonal-area-from-an-image-in-a-winform-picturebox#comment49952011_30954946). There's no such thing as a polygonal bitmap, so you need to draw it onto a square canvas, right? It doesn't mean that the clipping shape isn't polygonal though. – ProgrammingLlama Dec 07 '21 at 06:28
  • @Llama i'm sorry I haven't been able to provide accurate information. i edited post – 로다현 Dec 07 '21 at 06:32
  • I think you need to detail the exact format you want the pixels in. I assumed you wanted the cropped area on a `Bitmap`, but from your edit that doesn't seem to be the case. As you want something outside what's normal, you will need to explain in detail what you do want. – ProgrammingLlama Dec 07 '21 at 06:33
  • I'm afraid the question still isn't clear as to how your goal differs from the question I linked. – ProgrammingLlama Dec 07 '21 at 06:51
  • I don't think you're going to get a meaningful answer here, I'm afraid. You've rejected cropping to the shape and outputting that as an image, and you won't tell us what format you want the image data in as a result of cropping. We're not mind readers so we can't help. – ProgrammingLlama Dec 07 '21 at 07:01
  • Re your latest edit: how does that differ from the question I linked to?? – ProgrammingLlama Dec 07 '21 at 07:03
  • @Llama In my opinion, in the post you linked, I think that I am getting the image inside the minimum rectangle, not the values ​​inside the line I drew. Have I misunderstood the document? – 로다현 Dec 07 '21 at 07:07
  • Yes, you have misunderstood. – ProgrammingLlama Dec 07 '21 at 07:07

2 Answers2

1

You would create a new bitmap, at least as large as the bounding box of your polygon. Create a graphics object from this new bitmap. You can then draw the polygon to this bitmap, using the original image as a texture brush. Note that you might need to apply transform matrix to translate from the full image coordinates to the cropped image coordinates.

Note that it looks like you have radiological images. These are typically 16 bit images, so they will need to be converted to 8bit mono, or 24bit RGB before they can be used. This should already be done in the drawing code if you have access to the source. Or you can do it yourself.

JonasH
  • 28,608
  • 2
  • 10
  • 23
0

this works for me

    private Bitmap CropImage(Bitmap bitmap, List<Point> points)
    {
        int pminx = 9999, pminy = 9999, pmaxx = 0, pmaxy = 0; System.Drawing.Point[] pcol = new System.Drawing.Point[points.Count]; int i = 0;
        foreach (Point pc in points)
        {
            if (pc.X > pmaxx) pmaxx = (int)pc.X;
            if (pc.Y > pmaxy) pmaxy = (int)pc.Y;
            if (pc.X < pminx) pminx = (int)pc.X;
            if (pc.Y < pminy) pminy = (int)pc.Y;

            pcol[i] = new System.Drawing.Point((int)pc.X, (int)pc.Y);
            i++;
        }
        
        TextureBrush textureBrush = new TextureBrush(bitmap);
        Bitmap bmpWrk = new Bitmap(bitmap.Width, bitmap.Height);

        using (Graphics g = Graphics.FromImage(bmpWrk))
        {
            g.FillPolygon(textureBrush, pcol);
        }

        System.Drawing.Rectangle CropRect = new System.Drawing.Rectangle(pminx, pminy, pmaxx - pminx, pmaxy - pminy);

        return bmpWrk.Clone(CropRect, bmpWrk.PixelFormat);
    }