# Take part of the image by coordinates

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
},
ImageMargins -> 0
];
Polygon@coords,
ImageSize -> ImageDimensions[img],
AspectRatio -> Automatic
];

• @kglr. Thank you for the answer, but unfortunately, this doesn't give me what I need – Kiril Danilchenko Sep 8 '18 at 13:47
• 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. – bill s Sep 14 '18 at 21:35
• @bills The kglr solution takes polygon from the image, but the coordinates of the polygon are not correct. – Kiril Danilchenko Sep 15 '18 at 6:02
• @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. – Kiril Danilchenko Sep 15 '18 at 6:02
• Kiril, posted an update that fixes the problem in the original post. – kglr Sep 15 '18 at 6:36

You can use ImageAdd using the polygon as mask:

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


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},

Show[image, polygon]


ImageAdd[image, mask]


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];



ImageAdd[image2, mask2]


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


• 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 dropbox.com/s/cxc1jmtfjeq4goj/image%20ex.png?dl=0 – Kiril Danilchenko Sep 16 '18 at 8:14
• @Kiril, for the image i downloaded from the link ImageDimensions is {2048, 1363}, not  ~(4000,1000) – kglr Sep 16 '18 at 8: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 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"]


ImageTrim[highlighted, polygon]


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

{dimx, dimy} = ImageDimensions[img];
Background -> Black,
PlotRange -> {{0, dimx}, {0, dimy}},
ImageSize -> {dimx, dimy}
];


(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]&

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}}
]


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