Skip to main content

Compiled Cython Code - Detects colors in images 2-3 x faster than Numpy

Project description

Compiled Cython Code - Detects colors in images 2-3 x faster than Numpy

pip install locate-pixelcolor-cythonsingle

Tested+compiled against Windows 10 / Python 3.10 / Anaconda

If you can't import it, compile it on your system (code at the end of this page)

How to use it in Python

import numpy as np
import cv2
from locate_pixelcolor_cythonsingle import search_colors
# 4525 x 6623 x 3 picture https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/
picx = r"C:\Users\hansc\Downloads\pexels-alex-andrews-2295744.jpg"
pic = cv2.imread(picx)
colors0 = np.array([[255, 255, 255]],dtype=np.uint8)
resus0 = search_colors(pic=pic, colors=colors0)
colors1=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)
resus1 =  search_colors(pic=pic, colors=colors1)
####################################################################
%timeit resus0 = search_colors(pic=pic, colors=colors0)
51 ms ± 201 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

b,g,r = pic[...,0],pic[...,1],pic[...,2]
%timeit np.where(((b==255)&(g==255)&(r==255)))
150 ms ± 209 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
####################################################################
%timeit resus1 =  search_colors(pic=pic, colors=colors1)
443 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit np.where(((b==66)&(g==71)&(r==69))|((b==62)&(g==67)&(r==65))|((b==144)&(g==155)&(r==153))|((b==52)&(g==57)&(r==55))|((b==127)&(g==138)&(r==136))|((b==53)&(g==58)&(r==56))|((b==51)&(g==56)&(r==54))|((b==32)&(g==27)&(r==18))|((b==24)&(g==17)&(r==8)))
1 s ± 16.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
####################################################################

The Cython Code

# cython: language_level=3

import numpy as np
cimport numpy as np

cpdef searchforcolor(np.uint8_t[::1] pic, np.uint8_t[::1] colors, int width, int totallengthpic, int totallengthcolor, int[::1] outputx, int[ ::1] outputy, int[::1] lastresult):
    cdef int counter = 0
    cdef unsigned char r, g, b
    cdef int i, j

    for i in range(0, totallengthcolor, 3):
        r = colors[i]
        g = colors[i + 1]
        b = colors[i + 2]
        for j in range(0, totallengthpic, 3):
            if ( r== pic[j]) and (g == pic[j+1]) and (b == pic[j+2]):
                dividend = j // 3
                quotient = dividend // width
                remainder = dividend % width
                outputx[counter] = quotient
                outputy[counter] = remainder
                lastresult[0] = counter
                counter += 1

# .\python.exe .\colorsearchcythonsinglesetup.py build_ext --inplace

setup.py to compile the code

# cython: language_level=3

from setuptools import Extension, setup
from Cython.Build import cythonize
import numpy as np
ext_modules = [
    Extension("colorsearchcythonsingle", ["colorsearchcythonsingle.pyx"], include_dirs=[np.get_include()],define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")])
]

setup(
    name='colorsearchcythonsingle',
    ext_modules=cythonize(ext_modules),
)


# .\python.exe .\colorsearchcythonsinglesetup.py build_ext --inplace

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_cythonsingle-0.10.tar.gz (80.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

locate_pixelcolor_cythonsingle-0.10-py3-none-any.whl (81.4 kB view details)

Uploaded Python 3

File details

Details for the file locate_pixelcolor_cythonsingle-0.10.tar.gz.

File metadata

File hashes

Hashes for locate_pixelcolor_cythonsingle-0.10.tar.gz
Algorithm Hash digest
SHA256 0671279b59409c759aaee245386fb137eae4ed3ae2e08395524d031eaf29974d
MD5 87b4c471dd5912e1119bb307a34e984b
BLAKE2b-256 fa11bf5099b8d2f22bcf22302d658b47215bc8dd7cf6d79fb42f2734bd48aace

See more details on using hashes here.

File details

Details for the file locate_pixelcolor_cythonsingle-0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for locate_pixelcolor_cythonsingle-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 fa93c70538a42a40a91e20ffa02dfb3e14601f8f99f50df0e6fd8e9f53c4ae09
MD5 f4a60384fe87b410736985b8a14a04fc
BLAKE2b-256 42de9c7de1ed959f99a0c114e0e25ca549650719124331641f88fc0a8ccea79e

See more details on using hashes here.

Supported by

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