# Detect colors in images - 20 x faster than Numpy
### pip install locate-pixelcolor-cpppragma
#### Tested against Windows 10 / Python 3.10 / Anaconda
### Important!
The module imports a function from a compiled .dll (C++).
If you get any import errors, install: https://download.visualstudio.microsoft.com/download/pr/8b92f460-7e03-4c75-a139-e264a770758d/26C2C72FBA6438F5E29AF8EBC4826A1E424581B3C446F8C735361F1DB7BEFF72/VC_redist.x64.exe
### How to use it in Python
```python
import numpy as np
import cv2
from locate_pixelcolor_cpppragma 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, cpus=5)
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, cpus=4)
####################################################################
%timeit resus0 = search_colors(pic=pic, colors=colors0, cpus=5)
23.4 ms ± 40.6 µ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, cpus=4)
46.6 ms ± 988 µs per loop (mean ± std. dev. of 7 runs, 10 loops 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 C++ Code
```cpp
#include <omp.h>
#include <atomic>
std::atomic<int> value(0);
int create_id() {
return value++;
}
extern "C" __declspec(dllexport) void colorsearch(char *pic, char *colors, int width, int totallengthpic, int totallengthcolor, int *outputx, int *outputy, int *lastresult)
{
value=0;
int counter = 0;
#pragma omp parallel reduction(+ \
: counter)
{
#pragma omp for schedule(static)
for (int i = 0; i <= totallengthcolor; i += 3)
{
int r = i;
int g = i + 1;
int b = i + 2;
for (int j = 0; j <= totallengthpic; j += 3)
{
if ((colors[r] == pic[j]) && (colors[g] == pic[j + 1]) && (colors[b] == pic[j + 2]))
{
#pragma omp critical
{
int dividend = j / 3;
int quotient = dividend / width;
int remainder = dividend % width;
int upcounter = create_id();
outputx[upcounter] = quotient;
outputy[upcounter] = remainder;
lastresult[0] = upcounter;
}
}
}
}
}
}
// Compile:
// cl.exe /std:c++20 /fp:fast /EHsc /MT /Og /Oi /Ot /Oy /Ob3 /GF /Gy /MD /openmp /LD clooppra.cpp /Fe:clooppra.dll
// or
// cl.exe /std:c++20 /fp:fast /EHsc /O2 /MD /openmp /LD clooppra.cpp /Fe:clooppra.dll
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/locate_pixelcolor_cpppragma",
"name": "locate-pixelcolor-cpppragma",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "cpp,c++,image,search,parallel,rgb",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e1/f8/684783fd4bd5f39a5569016d2f14c7e1d119c8136e29b7894840a10604f7/locate_pixelcolor_cpppragma-0.11.tar.gz",
"platform": null,
"description": "# Detect colors in images - 20 x faster than Numpy \r\n\r\n### pip install locate-pixelcolor-cpppragma\r\n\r\n#### Tested against Windows 10 / Python 3.10 / Anaconda\r\n\r\n### Important!\r\nThe module imports a function from a compiled .dll (C++). \r\nIf you get any import errors, install: https://download.visualstudio.microsoft.com/download/pr/8b92f460-7e03-4c75-a139-e264a770758d/26C2C72FBA6438F5E29AF8EBC4826A1E424581B3C446F8C735361F1DB7BEFF72/VC_redist.x64.exe\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_cpppragma 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, cpus=5)\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, cpus=4)\r\n\r\n####################################################################\r\n%timeit resus0 = search_colors(pic=pic, colors=colors0, cpus=5)\r\n23.4 ms \u00c2\u00b1 40.6 \u00c2\u00b5s per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 10 loops each)\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 \u00c2\u00b1 209 \u00c2\u00b5s per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 10 loops each)\r\n####################################################################\r\n%timeit resus1 = search_colors(pic=pic, colors=colors1, cpus=4)\r\n46.6 ms \u00c2\u00b1 988 \u00c2\u00b5s per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 10 loops each)\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 \u00c2\u00b1 16.1 ms per loop (mean \u00c2\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```cpp\r\n#include <omp.h>\r\n#include <atomic> \r\nstd::atomic<int> value(0);\r\nint create_id() {\r\n return value++;\r\n }\r\n\r\nextern \"C\" __declspec(dllexport) void colorsearch(char *pic, char *colors, int width, int totallengthpic, int totallengthcolor, int *outputx, int *outputy, int *lastresult)\r\n{\r\n value=0;\r\n int counter = 0;\r\n\r\n#pragma omp parallel reduction(+ \\\r\n : counter)\r\n {\r\n\r\n#pragma omp for schedule(static)\r\n for (int i = 0; i <= totallengthcolor; i += 3)\r\n {\r\n int r = i;\r\n int g = i + 1;\r\n int b = i + 2;\r\n for (int j = 0; j <= totallengthpic; j += 3)\r\n {\r\n if ((colors[r] == pic[j]) && (colors[g] == pic[j + 1]) && (colors[b] == pic[j + 2]))\r\n {\r\n\r\n#pragma omp critical\r\n {\r\n int dividend = j / 3;\r\n int quotient = dividend / width;\r\n int remainder = dividend % width;\r\n int upcounter = create_id();\r\n outputx[upcounter] = quotient;\r\n outputy[upcounter] = remainder;\r\n lastresult[0] = upcounter;\r\n }\r\n }\r\n }\r\n }\r\n }\r\n}\r\n// Compile:\r\n// cl.exe /std:c++20 /fp:fast /EHsc /MT /Og /Oi /Ot /Oy /Ob3 /GF /Gy /MD /openmp /LD clooppra.cpp /Fe:clooppra.dll\r\n// or\r\n// cl.exe /std:c++20 /fp:fast /EHsc /O2 /MD /openmp /LD clooppra.cpp /Fe:clooppra.dll\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Detect colors in images - 20x faster than Numpy",
"version": "0.11",
"split_keywords": [
"cpp",
"c++",
"image",
"search",
"parallel",
"rgb"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cd698f19e7b56473ef8dfe7dfea16eed9725fa57a4730bb98eea8af64ae1b627",
"md5": "f667b099a536f01aca0cb91f241af2b9",
"sha256": "7e6dcf0fce18cfb5b22c45315c3d14ab06b0753ed1f1877ba9dd07dd7f60317d"
},
"downloads": -1,
"filename": "locate_pixelcolor_cpppragma-0.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f667b099a536f01aca0cb91f241af2b9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 13567,
"upload_time": "2023-04-15T00:38:35",
"upload_time_iso_8601": "2023-04-15T00:38:35.426107Z",
"url": "https://files.pythonhosted.org/packages/cd/69/8f19e7b56473ef8dfe7dfea16eed9725fa57a4730bb98eea8af64ae1b627/locate_pixelcolor_cpppragma-0.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e1f8684783fd4bd5f39a5569016d2f14c7e1d119c8136e29b7894840a10604f7",
"md5": "9f212e484d2c975ff93029fb8f8ee47c",
"sha256": "daf80fac2dc7362e3b15a3688cecb7d3f7d9512996e5bf742b61d874ab4bdd59"
},
"downloads": -1,
"filename": "locate_pixelcolor_cpppragma-0.11.tar.gz",
"has_sig": false,
"md5_digest": "9f212e484d2c975ff93029fb8f8ee47c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11130,
"upload_time": "2023-04-15T00:38:37",
"upload_time_iso_8601": "2023-04-15T00:38:37.157739Z",
"url": "https://files.pythonhosted.org/packages/e1/f8/684783fd4bd5f39a5569016d2f14c7e1d119c8136e29b7894840a10604f7/locate_pixelcolor_cpppragma-0.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-15 00:38:37",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "hansalemaos",
"github_project": "locate_pixelcolor_cpppragma",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "numpy",
"specs": []
}
],
"lcname": "locate-pixelcolor-cpppragma"
}