I'm currently trying to implement a function in Python that is supposed to find occurrences of a certain color value in an image in order to determine the bounding box of a color region. This seems to work, albeit at a very slow speed. Iterating over a single 1920x1080 image takes around 30 seconds. I tried converting this into Cython code, which improved the performance only by ~2 seconds per image. This is still shy of what I'm looking for. Since I'm a newbie to Cython I was hoping you could give me some hints on improving this. You can see my code below, thanks a lot!
cimport cython
import numpy as np
cimport numpy as np
@cython.wraparound(False)
@cython.boundscheck(False)
cdef _cget_bboxes_(img):
cdef int y_lim = img.shape[0]
cdef int x_lim = img.shape[1]
cdef np.ndarray img_array = img
color_dict = {}
cdef int y, x
for y in range(y_lim):
for x in range(x_lim):
pix = img_array[y][x]
pix = tuple(pix)
if np.any(pix >= (10, 10, 10)):
if pix not in color_dict:
color_dict[pix] = {"min_x": x, "max_x": x, "min_y": y, "max_y": y, "count": 1}
else:
if color_dict[pix]["min_x"] >= x:
color_dict[pix]["min_x"] = x
if color_dict[pix]["max_x"] <= x:
color_dict[pix]["max_x"] = x
if color_dict[pix]["min_y"] >= y:
color_dict[pix]["min_y"] = y
if color_dict[pix]["max_y"] <= y:
color_dict[pix]["max_y"] = y
color_dict[pix]["count"] += 1
return color_dict