1

I have a list of the coordinates of a polygon's vertices:

polygon = [(1389, 176), (1165, 63), (965, 93), (819, 224), (692, 473), (590, 673), 
(590, 858), (541, 1073), (531, 1272), (512, 1492), (463, 1779), 
(483, 1935), (863, 1672), (1233, 1701), (1560, 1794), (1828, 1770), 
(2111, 1896), (2165, 2111), (2179, 2243), (2267, 2179), (2316, 1658), 
(2233, 1277), (2169, 707), (2072, 395), (1862, 166), (1662, 102)]

and this can be mapped to a certain image(a pixel array). If I want to only keep the polygon area of the image, what should I do with python?

user6191682
  • 67
  • 1
  • 3
  • 9
  • You mean clipping the image using that polygon?! – giosans Jun 20 '17 at 07:38
  • Yes! That's what I mean. – user6191682 Jun 20 '17 at 07:39
  • The solution is creating a masked array. Look at this similar answer: https://stackoverflow.com/questions/22588074/polygon-crop-clip-using-python-pil – giosans Jun 20 '17 at 07:46
  • My images are x-ray films, which are in dicom format, and I can read them as pixel array. I don't know how to deal them with 'RGB' as mentioned in the answer. – user6191682 Jun 20 '17 at 07:57
  • You can read it with pydicom https://pydicom.readthedocs.io/en/stable/viewing_images.html . If that's x-ray it will have only intensity [thus a single channel] as third dimension. That means that you'll have to change the code from that answer into: `newImArray[:,:,0] = imArray[:,:,0]` and use `Image.fromarray()` with a gray scale parameter. If still cannot work with that, you can point me to a dicom file on the web – giosans Jun 20 '17 at 08:35
  • 1
    Thank you! I finally solved this problem by applying a mask on the image, as this answer suggested: https://stackoverflow.com/questions/3654289/scipy-create-2d-polygon-mask. – user6191682 Jun 21 '17 at 01:40

0 Answers0