How to extract polygon area in Python

0. Overview

When using the COCO Format, you often want to extract only certain objects. At that time, it is necessary to use the polygon information as a mask to extract the object from the image. Even if I look for this method, I can't find it. This time I will introduce such a method.

1. Code

First of all, you should be able to see it in seconds by looking at the code below.

import cv2

src_img = cv2.imread("YOUR_PATH")

# parameter
sg = [1, 1, 2, 2, 3, 3]
bb = [1, 1, 2, 2]

# working file
mask = np.zeros_like(src_img) # (y, x, c)

# segmentation data
sg = np.asarray(sg[0])
poly_number = int(len(sg)/2)
poly = np.zeros( (poly_number, 2) )
for i in range(poly_number):
  poly[i][0] = sg[(i * 2) + 0] # x
  poly[i][1] = sg[(i * 2) + 1] # y

# generate mask
mask = cv2.fillConvexPoly(mask, np.array(poly, 'int32'), color=(255, 255, 255))

# generate src_img and mask_image
cv2.imwrite("./src_img.png ", src_img)
cv2.imwrite("./mask.png ", mask)

# masked image
masked_src_img = np.where(mask==255, src_img, mask)
cv2.imwrite("./procData/masked.png ", masked_src_img)

# cropping img
bb = np.asarray(bb, 'int32')
offset_x = bb[0]
offset_y = bb[1]
length_x = bb[2]
length_y = bb[3]
cv2.imwrite("./masked_crop.png ", masked_src_img[offset_y : (offset_y + length_y), offset_x : (offset_x + length_x)])

First, the polygon is converted to a mask image below. It should be noted that an error other than ʻint32` will occur.

cv2.fillConvexPoly(mask, np.array(poly, 'int32'), color=(255, 255, 255))

Next, you only have to specify that the part of the mask image with the 255 value uses the value of src_img.

masked_src_img = np.where(mask==255, src_img, mask)

Finally, if you crop in the Bounding box, you can extract a specific area.

Enjoy !

Recommended Posts

How to extract polygon area in Python
How to develop in Python
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to extract any appointment in Google Calendar with Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to install Python
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to implement Discord Slash Command in Python
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
Summary of how to use MNIST in Python
How to specify TLS version in python requests
How to check / extract files in RPM package
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to handle datetime type in python sqlite3
How to make Python Interpreter changes in Pycharm
How to plot autocorrelation and partial autocorrelation in python
How to remove duplicate elements in Python3 list
20th Offline Real-time How to Write Problems in Python
How to retrieve the nth largest value in Python
[For beginners] How to use say command in python!
How to convert / restore a string with [] in python
How to get the variable name itself in python
How to install Python [Windows]
To flush stdout in Python
How to get the number of digits in Python