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[
   ImageSize -> ImageDimensions[img],
   AspectRatio -> Automatic
  • $\begingroup$ @kglr. Thank you for the answer, but unfortunately, this doesn't give me what I need $\endgroup$ – Kiril Danilchenko Sep 8 '18 at 13:47
  • $\begingroup$ Can you be more specific about what is wrong with @kglr's answer? It seems to pick a polygon from an image as you request. $\endgroup$ – bill s Sep 14 '18 at 21:35
  • $\begingroup$ @bills The kglr solution takes polygon from the image, but the coordinates of the polygon are not correct. $\endgroup$ – Kiril Danilchenko Sep 15 '18 at 6:02
  • $\begingroup$ @kglr It does not work correctly. Still, it takes a polygon, but with incorrect position (relate to original image) it looks that the mask has a problem with the ratio with the original image. $\endgroup$ – Kiril Danilchenko Sep 15 '18 at 6:02
  • $\begingroup$ Kiril, posted an update that fixes the problem in the original post. $\endgroup$ – kglr Sep 15 '18 at 6:36

You can use ImageAdd using the polygon as mask:

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

enter image description here

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

  • $\begingroup$ 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. $\endgroup$ – Kiril Danilchenko Sep 14 '18 at 10:59
  • $\begingroup$ 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 dropbox.com/s/cxc1jmtfjeq4goj/image%20ex.png?dl=0 $\endgroup$ – Kiril Danilchenko Sep 16 '18 at 8:14
  • 1
    $\begingroup$ @Kiril, for the image i downloaded from the link ImageDimensions is {2048, 1363}, not ` ~(4000,1000)` $\endgroup$ – kglr Sep 16 '18 at 8:32
  • $\begingroup$ something is going wrong.... I get that dimension is {4010, 2668} I work with ver. 11.3 on Win. $\endgroup$ – Kiril Danilchenko Sep 16 '18 at 8:48

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.)


You can use Texture[]

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

pts00Scaled=(#/ImageDimensions[img00])& /@ N[pts00];  

         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[].


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.