5

I have four coordinates in the image, and I am interested in taking this polygon from the image. I try to do it in the following manner:

Edit: I use the solution from the @kglr answer, unfortunately, it does not take the correct part of the image. The link to the image is: https://www.dropbox.com/s/w58sikz2jip2sbg/19725.tiff?dl=0

coords = {{1983.44`, 992.097`}, {2034.18`, 1009.01`}, {2027.91`, 
    1027.18`}, {1977.8`, 1009.01`}, {1983.44`, 992.097`}};

polygon = Graphics[{
    EdgeForm[{Thick, Black}],
    FaceForm[], Polygon@coords
    },
   ImagePadding -> 0,
   PlotRangePadding -> 0,
   ImageMargins -> 0
   ];
mask = Graphics[
   Polygon@coords,
   ImageSize -> ImageDimensions[img],
   AspectRatio -> Automatic
   ];
C. E.
  • 70,533
  • 6
  • 140
  • 264
Kiril Danilchenko
  • 2,019
  • 1
  • 9
  • 18

3 Answers3

6

You can use ImageAdd using the polygon as mask:

image = Import["ExampleData/spikey.tiff"]

enter image description here

SeedRandom[1]
coords = Transpose[{RandomReal[ImageDimensions[image][[1]], 4], 
    RandomReal[ImageDimensions[image][[2]], 4]}];
coords = coords[[FindCurvePath[coords][[1]]]];
polygon = Graphics[{EdgeForm[{Thick, Black}], FaceForm[], Polygon@coords}, 
   PlotRange -> Thread[{0, ImageDimensions[image]}]];
mask = Graphics[Polygon@coords, 
   PlotRange -> Thread[{0, ImageDimensions[image]}]];

Show[image, polygon]

enter image description here

ImageAdd[image, mask]

enter image description here

For image2 from the updated version of the question:

coords2 = {1977.8, 1009.01, 1983.44, 992.097, 2034.18, 1009.01, 2027.91, 1027.18};
coords2 = Partition[coords2, 2];
mask2 = Graphics[Polygon@coords2, PlotRange -> Thread[{0, ImageDimensions[image2]}]];

Show[image2, Graphics[{Red, mask2[[1]]}]]

enter image description here

ImageAdd[image2, mask2]

enter image description here

ImageTake[ImageAdd[image2, mask2], 1363 - { 1035, 975}, {1945, 2048}]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Thank you @kglr. Unfortunately, this solution didn't work for mine in real data. I edit the question with the link to a real image and the coordinates. – Kiril Danilchenko Sep 14 '18 at 10:59
  • Thank you @kglr the size of the polygon now is correct. But its position doesn't right. Take a look at the coordinates; the x value is ~2020 and y values is~1000. The dimension of the image is ~(4000,1000). So I expect that the position of the polygon will be in the middle of the image (the x value) and y value is into the first half of the image height. I expected (and this is my goal to crop this part of the original image) that the polygon's position will be as in the attached image https://www.dropbox.com/s/cxc1jmtfjeq4goj/image%20ex.png?dl=0 – Kiril Danilchenko Sep 16 '18 at 08:14
  • 1
    @Kiril, for the image i downloaded from the link ImageDimensions is {2048, 1363}, not ~(4000,1000) – kglr Sep 16 '18 at 08:32
  • something is going wrong.... I get that dimension is {4010, 2668} I work with ver. 11.3 on Win. – Kiril Danilchenko Sep 16 '18 at 08:48
3

HighlightImage and ImageTrim can deal with polygons directly. That can be used to extract polygons from images:

polygon = Polygon[{{1132., 621.4}, {1153., 951.1}, {1490., 919.1}, {1555., 583.6}}];
highlighted = HighlightImage[img, {EdgeForm[], polygon}, "Remove"]

Mathematica graphics

ImageTrim[highlighted, polygon]

Mathematica graphics

If we didn't have HighlightImage and ImageTrim then I would do it like this:

{dimx, dimy} = ImageDimensions[img];
mask = Rasterize@Graphics[{White, polygon},
    Background -> Black,
    PlotRange -> {{0, dimx}, {0, dimy}},
    ImageSize -> {dimx, dimy}
    ];
ImageCrop[mask img + ColorNegate[mask]]

Mathematica graphics

(One can also use addition instead of multiplication, as in kglr's answer, with ImageCrop.)

C. E.
  • 70,533
  • 6
  • 140
  • 264
1

You can use Texture[]

img00=ExampleData[{"TestImage", "Lena"}] //ImageTake[#,200]& //Show[#,Frame-> True]&  

pts00={{10,50},{250,10},{150,200},{50,150}};
pts00Scaled=(#/ImageDimensions[img00])& /@ N[pts00];  

Graphics[
      {
         Texture[img00 ],
         Polygon[pts00,VertexTextureCoordinates-> pts00Scaled],
      },
   ImageSize-> 500,Frame-> True,
   PlotRange-> {{0,512},{0,200}}
  ]  

enter image description here

enter image description here

here is a advanced use of Texture[] on Polygon[].

andre314
  • 18,474
  • 1
  • 36
  • 69