# Detects colors in images 10 x faster than Numpy
### pip install locate-pixelcolor-c
#### Tested against Windows 10 / Python 3.10 / Anaconda
### How to use it in Python
```python
import numpy as np
import cv2
from locate_pixelcolor_c 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 search_colors(pic=pic, colors=colors0)
17.6 ms ± 245 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
# last update: 16.3 ms
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)
138 ms ± 10 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# last update: 117 ms
%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 C Code
```c
void colorsearch(unsigned char *pic, unsigned char *colors, int width, int totallengthpic, int totallengthcolor, int *outputx, int *outputy, int *lastresult)
{
int counter = 0;
for (int i = 0; i <= totallengthcolor; i += 3)
{
int r = colors[i];
int g = colors[i + 1];
int b = colors[i + 2];
for (int j = 0; j <= totallengthpic; j += 3)
{
if ((r == pic[j]) && (g == pic[j + 1]) && (b == pic[j + 2]))
{
int dividend = j / 3;
int quotient = dividend / width;
int remainder = dividend % width;
int upcounter = counter;
outputx[upcounter] = quotient;
outputy[upcounter] = remainder;
lastresult[0] = upcounter;
counter++;
}
}
}
}
// gcc -O2 -fPIC -shared -o cloop.so cloop.c
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/locate_pixelcolor_c",
"name": "locate-pixelcolor-c",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "C,image,search,rgb",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/cb/6f/edd1f7396a1101bc20411dd7b06d5ff207779de3690cce03836d818d50ab/locate_pixelcolor_c-0.12.tar.gz",
"platform": null,
"description": "\r\n# Detects colors in images 10 x faster than Numpy \r\n\r\n### pip install locate-pixelcolor-c\r\n\r\n#### Tested against Windows 10 / Python 3.10 / Anaconda\r\n\r\n\r\n\r\n### How to use it in Python \r\n\r\n```python\r\nimport numpy as np\r\nimport cv2\r\nfrom locate_pixelcolor_c import search_colors\r\n# 4525 x 6623 x 3 picture https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/\r\npicx = r\"C:\\Users\\hansc\\Downloads\\pexels-alex-andrews-2295744.jpg\"\r\npic = cv2.imread(picx)\r\ncolors0 = np.array([[255, 255, 255]],dtype=np.uint8)\r\nresus0 = search_colors(pic=pic, colors=colors0)\r\ncolors1=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)\r\nresus1 = search_colors(pic=pic, colors=colors1)\r\n####################################################################\r\n%timeit search_colors(pic=pic, colors=colors0)\r\n17.6 ms \u00b1 245 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 100 loops each)\r\n# last update: 16.3 ms\r\n\r\nb,g,r = pic[...,0],pic[...,1],pic[...,2]\r\n%timeit np.where(((b==255)&(g==255)&(r==255)))\r\n150 ms \u00b1 209 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)\r\n####################################################################\r\n%timeit resus1 = search_colors(pic=pic, colors=colors1)\r\n138 ms \u00b1 10 ms per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)\r\n# last update: 117 ms\r\n\r\n\r\n%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)))\r\n1 s \u00b1 16.1 ms per loop (mean \u00b1 std. dev. of 7 runs, 1 loop each)\r\n####################################################################\r\n```\r\n\r\n\r\n### The C Code \r\n\r\n```c\r\n\r\nvoid colorsearch(unsigned char *pic, unsigned char *colors, int width, int totallengthpic, int totallengthcolor, int *outputx, int *outputy, int *lastresult)\r\n{\r\n int counter = 0;\r\n\r\n for (int i = 0; i <= totallengthcolor; i += 3)\r\n {\r\n int r = colors[i];\r\n int g = colors[i + 1];\r\n int b = colors[i + 2];\r\n for (int j = 0; j <= totallengthpic; j += 3)\r\n {\r\n if ((r == pic[j]) && (g == pic[j + 1]) && (b == pic[j + 2]))\r\n {\r\n\r\n int dividend = j / 3;\r\n int quotient = dividend / width;\r\n int remainder = dividend % width;\r\n int upcounter = counter;\r\n outputx[upcounter] = quotient;\r\n outputy[upcounter] = remainder;\r\n lastresult[0] = upcounter;\r\n counter++;\r\n }\r\n }\r\n }\r\n}\r\n// gcc -O2 -fPIC -shared -o cloop.so cloop.c\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Detects colors in images 10 x faster than Numpy",
"version": "0.12",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/locate_pixelcolor_c"
},
"split_keywords": [
"c",
"image",
"search",
"rgb"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "83dbb9cec98546c32d86653b33bcb3b30cb9dedc54672cbf8d8c494fffb6e142",
"md5": "45c17decdb8ab9103c28df82a2d8fa8b",
"sha256": "b2a69b3765be766bfd0b15e8673e23dfe6e65de0dbae31e608e45c92535645e0"
},
"downloads": -1,
"filename": "locate_pixelcolor_c-0.12-py3-none-any.whl",
"has_sig": false,
"md5_digest": "45c17decdb8ab9103c28df82a2d8fa8b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 36245,
"upload_time": "2023-05-30T16:33:40",
"upload_time_iso_8601": "2023-05-30T16:33:40.296243Z",
"url": "https://files.pythonhosted.org/packages/83/db/b9cec98546c32d86653b33bcb3b30cb9dedc54672cbf8d8c494fffb6e142/locate_pixelcolor_c-0.12-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "cb6fedd1f7396a1101bc20411dd7b06d5ff207779de3690cce03836d818d50ab",
"md5": "b297238aaa61895ba0db0cf9aef3541e",
"sha256": "4637a0e63bd6aa650d4144b85080527641a9d410b3730d1c85ffb3bbb9bd2ee9"
},
"downloads": -1,
"filename": "locate_pixelcolor_c-0.12.tar.gz",
"has_sig": false,
"md5_digest": "b297238aaa61895ba0db0cf9aef3541e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 35004,
"upload_time": "2023-05-30T16:33:42",
"upload_time_iso_8601": "2023-05-30T16:33:42.811019Z",
"url": "https://files.pythonhosted.org/packages/cb/6f/edd1f7396a1101bc20411dd7b06d5ff207779de3690cce03836d818d50ab/locate_pixelcolor_c-0.12.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-30 16:33:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "locate_pixelcolor_c",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "locate-pixelcolor-c"
}