简体   繁体   中英

Given specifc pixel coordinates, get the color at the python

Let's say i have an 800x600 image: 在此处输入图像描述

and I have an HTML map element where it defines multiple poly areas(in this case only one is defined which is Tenessee):

<img src="usa-colored-regions-map.jpg" usemap="#image-map">

<map name="image-map">
    <area target="" alt="" title="" href="" coords="492,379,521,376,562,377,568,363,583,355,589,347,595,340,570,343,535,348,515,349,507,355,494,355,491,370" shape="poly">
</map>

I want to use python to detect the color of each polygon, I want the dominant color only since each state has text in white color that might skew the results.

How can i achieve this?

Approach:

  1. For each polygon, draw a mask . Use OpenCV drawing calls to draw a white filled polygon on black background. Use no anti-aliasing.
  2. Use the mask to select all the pixels of that area. values = img[mask]
  3. Take the median of that list. Per-channel median should be okay to use: np.median(values, axis=0)

And that's it.

I solved the question like this:

req = urllib.request.urlopen(image_url)

arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img: np.array = cv2.imdecode(arr, -1)
# draw polygon on input to visualize
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_poly = img.copy()

coords = '174,477,213,477,213,557,174,557' # from the area tag
points = np.fromstring(coords, sep=',', dtype=np.int32).reshape((-1, 2))

# create mask for polygon
mask = np.zeros_like(gray)
cv2.polylines(img_poly, [points], True, (0, 0, 255), 2)
cv2.fillPoly(mask, [points], (255))
mean = cv2.mean(img_poly, mask)
color = np.empty((4))
color[:] = mean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM