votchallenge / toolkit

The official VOT Challenge evaluation and analysis toolkit
http://www.votchallenge.net/
GNU General Public License v3.0
155 stars 43 forks source link

Polygon rasterization #1

Closed alanlukezic closed 4 years ago

alanlukezic commented 4 years ago

A polygon is not rasterized into the binary mask in the same way if using within the toolkit or just by using OpenCV. It looks like the OpenCV fillConvexPoly works differently if some coordinates are out of image. For more details see code example.

import cv2
import numpy as np
from vot.region.shapes import RegionType, Polygon

p = [660, 570, 635, 527, 723, 476, 748, 519]
img_sz = [960, 540]  # [width, height]

m = np.zeros((img_sz[1], img_sz[0]), dtype=np.uint8)
cv2.fillConvexPoly(m, np.reshape(p, (-1, 1, 2)), 1)

poly = Polygon([(p1, p2) for p1, p2 in zip(p[::2], p[1::2])])
toolkit_m = poly.convert(RegionType.MASK).get_array(img_sz)

diff = np.sum(np.abs(toolkit_m - m))
print('Number of total pixels which are different:', diff)

cv2.namedWindow('m', cv2.WINDOW_NORMAL)
cv2.imshow('m', (255 * m).astype(np.uint8))
cv2.waitKey(10)
cv2.namedWindow('toolkit_m', cv2.WINDOW_NORMAL)
cv2.imshow('toolkit_m', (255 * toolkit_m).astype(np.uint8))
cv2.waitKey(10)
cv2.namedWindow('diff', cv2.WINDOW_NORMAL)
cv2.imshow('diff', (255 * np.abs(toolkit_m - m)).astype(np.uint8))
cv2.waitKey(0)
lukacu commented 4 years ago

This issue is no longer valid as the rasterization code has been replaced.