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?
CodePudding user response:
Approach:
- For each polygon, draw a
mask
. Use OpenCV drawing calls to draw a white filled polygon on black background. Use no anti-aliasing. - Use the mask to select all the pixels of that area.
values = img[mask]
- Take the median of that list. Per-channel median should be okay to use:
np.median(values, axis=0)
And that's it.
CodePudding user response:
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