Skip to main content

RGB search with numba - 2-3 x faster than numpy

Project description

RGB search with numba - 2-3 x faster than numpy

pip install locate-pixelcolor-numba

Important!

This is a compiled .pyd file (Numba AOT), if you can't import it, run the following code to generate a new pyd file, and replace it with the old .pyd file.

from numba_aot_compiler import compnumba #pip install numba-aot-compiler
import numpy as np
from numba import uint8, uint16


def search_colors(r, g, b, rgbs, divider):
    res = np.zeros(b.shape, dtype=np.uint16)
    res2 = np.zeros(b.shape, dtype=np.uint16)
    zaehler = 0
    for rgb in rgbs:
        rr, gg, bb = rgb
        for i in range(r.shape[0]):
            if r[i] == rr:
                if g[i] == gg:
                    if b[i] == bb:
                        dvquot, dvrem = divmod(i, divider)
                        res[zaehler] = dvquot
                        res2[zaehler] = dvrem
                        zaehler = zaehler + 1
    results = np.dstack((res[:zaehler], res2[:zaehler]))
    return results


compi2 = compnumba(
    fu=search_colors,
    funcname="search_colors",
    file="searchcolorsnumba",
    folder="locate_pixelcolor_numba",
    signature=(uint8[:], uint8[:], uint8[:], uint8[:, :], uint16),
    parallel=True,
    fastmath=True,
    nogil=True,
)

How to use it

from locate_pixelcolor_numba import search_colors
import cv2
import time
import numpy as np
pic = cv2.imread(r"pexels-alex-andrews-2295744.jpg") # https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/
colors = np.array([(66,  71,  69),(62,  67,  65),(144, 155, 153),(52,  57,  55),(127, 138, 136),(53,  58,  56),(51,  56,  54),(32,  27,  18),(24,  17,   8),],dtype=np.uint8)
search_colors(pic,colors)
Out[2]: 
array([[[   0, 4522],
        [   3, 4522],
        [   3, 4523],
        ...,
        [6622, 4522],
        [6622, 4523],
        [6622, 4524]]], dtype=uint16)
%timeit search_colors(pic,colors)
413 ms ± 1.22 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# More benchmarks: https://github.com/hansalemaos/locate_pixelcolor_cpp

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

locate_pixelcolor_numba-0.11.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

locate_pixelcolor_numba-0.11-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

Details for the file locate_pixelcolor_numba-0.11.tar.gz.

File metadata

File hashes

Hashes for locate_pixelcolor_numba-0.11.tar.gz
Algorithm Hash digest
SHA256 a67a601cc616ad6c82aba1e4f6f543f9c68a5e88ffdaf7b7d0c1377cac9af12b
MD5 c32ba8c68d6d919885dd4161876bb5ee
BLAKE2b-256 0059e1dfb51c225699388d671a4eac77cbe6f633a2dd5ac5e5e6d6f1a8789d60

See more details on using hashes here.

File details

Details for the file locate_pixelcolor_numba-0.11-py3-none-any.whl.

File metadata

File hashes

Hashes for locate_pixelcolor_numba-0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 c8943a7425073654ad77d459da76d09bed5cfbea2c9b9c29c3f2abf005091a5d
MD5 b20328de20e10428f40736c3b9bfae81
BLAKE2b-256 d68b3ea90f7cded847ae5c284e649c6990f5520242c7b281856ab418843007d2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page