Skip to main content

Detects colors in images 8 x faster than Numpy / Uses Cython - returns a dict

Project description

Detects colors in images 8 x faster than Numpy / Uses Cython - returns a dict

Tested against Windows 10 / Python 3.11 / Anaconda

pip install locate-pixelcolor-cython-multi-auto-compile

Very fast RGB color search in pictures.

I wrote a couple of variations of this function. All of them can be used in Python.

Cython, detects colors in images 2-3 x faster than Numpy # pre-compiled for Python 3.10 https://github.com/hansalemaos/locate_pixelcolor_cythonsingle

Cython, but with multiple processors (5-10x faster than Numpy) # pre-compiled for Python 3.10 https://github.com/hansalemaos/locate_pixelcolor_cythonmulti

Cupy, using the GPU (up to 8x faster than Numpy) https://github.com/hansalemaos/locate_pixelcolor_cupy

C - shared library (10x faster than Numpy) https://github.com/hansalemaos/locate_pixelcolor_c

C++ - parallel_for - shared library (up to 10x faster than Numpy) https://github.com/hansalemaos/locate_pixelcolor_cpp_parallelfor

C++ - pragma omp - shared library (20x faster than Numpy) https://github.com/hansalemaos/locate_pixelcolor_cpppragma

Numba - compiled - ahead of time (2-3x faster than numpy) https://github.com/hansalemaos/locate_pixelcolor_numba

Numba Cuda - compiled - ahead of time (10x faster than numpy) https://github.com/hansalemaos/locate_pixelcolor_numbacuda

import numpy as np
import cv2
from locate_pixelcolor_cython_multi_auto_compile 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)

# b,g,r = pic[...,0],pic[...,1],pic[...,2]
# %timeit resus0=search_colors(pic,colors0)
# %timeit np.where(((b==255)&(g==255)&(r==255)))
# %timeit resus1=search_colors(pic, colors1)
# %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)))
# 22.9 ms ± 63.9 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 161 ms ± 1.96 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 131 ms ± 400 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 939 ms ± 6.95 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# resus1
# Out[5]:
# defaultdict(list,
#             {(66, 71, 69): array([[   0, 4522],
#                     [   3, 4522],
#                     [   3, 4523],
#                     ...,
#                     [5895,  852],
#                     [6043, 2871],
#                     [6063, 2897]], dtype=int32),
#              (51,
#               56,
#               54): array([[   2, 4522],
#                     [  15, 4523],
#                     [  48, 4035],
#                     ...,
#                     [5898, 1961],
#                     [5898, 1962],
#                     [5903, 1975]], dtype=int32),
#              (127,
#               138,
#               136): array([[   0,   38],
#                     [   2,    0],
#                     [   3,   19],
#                     [   5,   13],
#                     [   6,   10],
#                     [   8,    9],
#                     [   8,   42],
#                     [   9,   34],
#                     [   9,   46],
#                     [  11,    1],
#                     [  11,    8],
#                     [  11,    9],
#                     [  11,   25],
#                     [  13,    0],
#                     [  14,   36],
#                     [  15,   21],
#                     [  15,   22],
#                     [  15,   33],
#                     [  15,   40],
#                     [  49,   56],
#                     [ 562, 3300],
#                     [ 615, 1746],
#                     [ 616, 1738],
#                     [ 662,   15],
#                     [ 663,   11],
#                     [ 894, 3218],
#                     [ 897, 3249],
#                     [ 907, 2166],
#                     [ 937, 1673],
#                     [1241, 3230],
#                     [1245, 1691],
#                     [1246, 1693],
#                     [1318, 1705],
#                     [1319, 1651],
#                     [1366, 1590],
#                     [1371, 1536],
#                     [1372, 1541],
#                     [1379, 1586],
#                     [1379, 1623],
#                     [1382, 1589],
#                     [1383, 1594],
#                     [1384, 1623],
#                     [1386, 1564],
#                     [1391, 1584],
#                     [1394, 1569],
#                     [1394, 1571],
#                     [1394, 1573],
#                     [1398, 1591],
#                     [1400, 1569],
#                     [1404, 1551],
#                     [1404, 1571],
#                     [1405, 1555],
#                     [1406, 1566],
#                     [1412, 1585],
#                     [1414, 1550],
#                     [1414, 1560],
#                     [1414, 1567],
#                     [1415, 1538],
#                     [1416, 1568],
#                     [1420, 1562],
#                     [1420, 1573],
#                     [1421, 1575],
#                     [1423, 1554],
#                     [1438, 1518],
#                     [1479, 3069],
#                     [2295, 2404],
#                     [2310, 2346],
#                     [2312, 2371],
#                     [2323, 2353],
#                     [2323, 2714],
#                     [2391, 2672],
#                     [2642, 3440],
#                     [2851, 2706],
#                     [2863, 2625],
#                     [2921, 2383],
#                     [2926, 2633],
#                     [2932, 2651],
#                     [2951, 2685],
#                     [2953, 2688],
#                     [2972, 2464],
#                     [3148, 2606],
#                     [3295, 2509],
#                     [3311, 2617],
#                     [3314, 2622],
#                     [3320, 2623],
#                     [3325, 2368],
#                     [3331, 2613],
#                     [3332, 2614],
#                     [3337, 2310],
#                     [3343, 2615],
#                     [3372, 2300],
#                     [3375, 2595],
#                     [3382, 2597],
#                     [4208, 2576],
#                     [4227, 2577],
#                     [4268,  289],
#                     [4347,  433],
#                     [4564,  811],
#                     [4686,  884],
#                     [4752,  860],
#                     [4995,  856],
#                     [5013, 2974],
#                     [5050,  460],
#                     [5093, 3457],
#                     [5094, 2925],
#                     [5130, 2849],
#                     [5131, 2850],
#                     [5136,  666],
#                     [5169, 3548],
#                     [5180, 3337],
#                     [5247, 3101],
#                     [5256, 2947],
#                     [5257, 3232],
#                     [5265, 3250],
#                     [5275, 2935],
#                     [5298, 2866],
#                     [5314, 3369],
#                     [5321, 3859],
#                     [5363, 2916],
#                     [5364, 3182],
#                     [5394, 2728],
#                     [5399, 3344],
#                     [5434, 2579],
#                     [5448, 3321],
#                     [5452, 3678],
#                     [5476, 3328],
#                     [5509, 2973]], dtype=int32),
#              (144,
#               155,
#               153): array([[   1,    1],
#                     [   1,   40],
#                     [   2,   21],
#                     [   6,    5],
#                     [   6,   22],
#                     [   6,   25],
#                     [   6,   69],
#                     [   8,   22],
#                     [   9,    1],
#                     [   9,   38],
#                     [   9,   66],
#                     [   9,   75],
#                     [  10,   38],
#                     [  11,   17],
#                     [  11,   22],
#                     [  12,   17],
#                     [  13,   37],
#                     [  16,    4],
#                     [  16,   91],
#                     [  17,   81],
#                     [  17,   94],
#                     [  19,   14],
#                     [  20,    0],
#                     [  21,    2],
#                     [  48,   60],
#                     [  63,   57],
#                     [ 877, 3035],
#                     [ 969, 2294],
#                     [1018, 2260],
#                     [1258, 3185],
#                     [1258, 3189],
#                     [1262, 3218],
#                     [1262, 3219],
#                     [1263, 1673],
#                     [1277, 3212],
#                     [1303, 1717],
#                     [1312, 1660],
#                     [1337, 1641],
#                     [1340, 1642],
#                     [1360, 1583],
#                     [1363, 3163],
#                     [1366, 1573],
#                     [1369, 1596],
#                     [1370, 1591],
#                     [1372, 1573],
#                     [1381, 1620],
#                     [1382, 1594],
#                     [1393, 1599],
#                     [1395, 1592],
#                     [1397, 1596],
#                     [1400, 1563],
#                     [1400, 1592],
#                     [1402, 1561],
#                     [1403, 1544],
#                     [1403, 1554],
#                     [1404, 1560],
#                     [1405, 1603],
#                     [1407, 1586],
#                     [1409, 1563],
#                     [1409, 1590],
#                     [1409, 1596],
#                     [1410, 1597],
#                     [1411, 1556],
#                     [1411, 1599],
#                     [1420, 1577],
#                     [1797, 2667],
#                     [2151, 2318],
#                     [2293, 2347],
#                     [2297, 2351],
#                     [2297, 2368],
#                     [2301, 2335],
#                     [2301, 2381],
#                     [2306, 2371],
#                     [2306, 2376],
#                     [2309, 2416],
#                     [2309, 2424],
#                     [2317, 2363],
#                     [2318, 2371],
#                     [2319, 2412],
#                     [2321, 2425],
#                     [2326, 2366],
#                     [2329, 2401],
#                     [2383, 2601],
#                     [2687, 2636],
#                     [2710, 2731],
#                     [2718, 2689],
#                     [2810, 2597],
#                     [2968, 2457],
#                     [2970, 2442],
#                     [3004, 2684],
#                     [3010, 2413],
#                     [3070, 2515],
#                     [3075, 2524],
#                     [3087, 2669],
#                     [3209, 2529],
#                     [3300, 2524],
#                     [3330, 2613],
#                     [3331, 2514],
#                     [3347, 2381],
#                     [3465, 2568],
#                     [3888, 2430],
#                     [3942, 1891],
#                     [3942, 1893],
#                     [3950, 1890],
#                     [4227, 2576],
#                     [4321,   98],
#                     [4420,  497],
#                     [4420,  500],
#                     [4421,  500],
#                     [4496,  619],
#                     [4530,  814],
#                     [4721,  859],
#                     [4723,  858],
#                     [4770,  952],
#                     [4822, 3101],
#                     [4864, 3195],
#                     [4923,  155],
#                     [4941,  548],
#                     [4942,  549],
#                     [4978, 2675],
#                     [4993,  882],
#                     [5112, 3145],
#                     [5141,  625],
#                     [5149,  736],
#                     [5185,  643],
#                     [5217, 3420],
#                     [5225, 3391],
#                     [5226, 3386],
#                     [5227, 3673],
#                     [5237, 3260],
#                     [5299, 4207],
#                     [5308, 3709],
#                     [5320, 3857],
#                     [5328, 3750],
#                     [5359, 2744],
#                     [5365, 2847],
#                     [5372, 2779],
#                     [5388, 2603],
#                     [5389, 3079],
#                     [5392, 2871],
#                     [5399, 2879],
#                     [5416, 2895],
#                     [5424, 3085],
#                     [5428, 3107],
#                     [5435, 2580],
#                     [5457, 3375],
#                     [5461, 3093],
#                     [5466, 3252],
#                     [5466, 3254],
#                     [5472, 3356],
#                     [5478,  671],
#                     [5483, 3150],
#                     [5485, 3670],
#                     [5487, 3672],
#                     [5510, 3433],
#                     [5513, 3107],
#                     [5537, 3010],
#                     [5537, 3012],
#                     [5579, 2977],
#                     [5755, 1061]], dtype=int32),
#              (32,
#               27,
#               18): array([[   3, 3240],
#                     [   3, 3241],
#                     [  12, 3349],
#                     ...,
#                     [6622, 2844],
#                     [6622, 2854],
#                     [6622, 2865]], dtype=int32),
#              (24,
#               17,
#               8): array([[ 669, 1723],
#                     [ 670, 1722],
#                     [ 781, 3120],
#                     ...,
#                     [6622, 4522],
#                     [6622, 4523],
#                     [6622, 4524]], dtype=int32),
#              (53,
#               58,
#               56): array([[   2, 4523],
#                     [   2, 4524],
#                     [   4, 4522],
#                     ...,
#                     [5906, 1969],
#                     [5908, 1246],
#                     [6057, 2862]], dtype=int32),
#              (52,
#               57,
#               55): array([[   1, 4524],
#                     [  49, 4035],
#                     [  52, 4030],
#                     ...,
#                     [6040, 2868],
#                     [6049, 2868],
#                     [6058, 2863]], dtype=int32),
#              (62,
#               67,
#               65): array([[   0, 4523],
#                     [  50, 4037],
#                     [  52, 4033],
#                     ...,
#                     [5903, 1973],
#                     [5904, 1952],
#                     [6041, 2868]], dtype=int32)})

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

Built Distribution

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