图像分解python_color-extraction-用于将图像分解为基本颜色的python包-Christelle Cocco, Raphael Cere, Aris Xanthos Instal...

作者:Christelle Cocco, Raphael Cere, Aris Xanthos

作者邮箱:christelle.cocco@unil.ch

首页:https://github.com/ChrisCocco/ddd_colours

文档:None

下载链接

Color-extraction

Color-extraction is an open-source python module which attributes to each element of an ndarray (RGB image) the most similar color from a palette of predefined colors.

Three functions are included, each of which takes an RGB ndarray as input and returns a dict whose keys are the names of each predefined color:

Function | Values of returned dict --- | --- get_bool_arrays | boolean ndarrays (1 per color) get_rgb_arrays | RGB ndarrays (1 per color) get_counts | integer counts of pixels (1 per color)

Installing

>>> pip install color_extraction

Usage examples

A predefined set of colors is included in the module with ten colors: red, orange, yellow, green, cyan, blue, purple, pink, achromatic (gray and black), and white. This set of colors, which can be modified, is available at https://github.com/ChrisCocco/dddcolours/blob/master/colorextraction/color_definitions.json.

To get started:

>>> import color_extraction

>>> import matplotlib

>>> import matplotlib.pyplot

>>> with matplotlib.cbook.get_sample_data('ada.png') as image_file:

... img = matplotlib.pyplot.imread(image_file)

Boolean arrays

The function get_bool_arrays returns a dictionary with a boolean ndarray for each color. Each such array has the same horizontal and vertical dimensions as the source image and can be thought of as a mask for the color in question.

>>> dict_bool_arrays = color_extraction.get_bool_arrays(img)

>>> for color in dict_bool_arrays.keys():

matplotlib.image.imsave(output_path + color + ".png", dict_bool_arrays[color], cmap='gray')

Original image| white | red | orange --- | --- | --- | ---

up-19822b995ed25540b86db1a390f5aaa1.jpg |

up-cc80f74b440ac033057894ec64a2e181.jpg|

up-a58f3ccfee48c6fdc13a898c5fcf5dad.jpg |

up-1be96290f1941d8bf73ae0a9652a22ef.jpg yellow | green | cyan | blue

up-abd49381a716f5bba7e176f32ec842f6.jpg|

up-ec240b1ed4d4be3e7f509bb4cdd2859e.jpg|

up-e82120349990669504d11251de3a25ab.jpg|

up-99dd26a423b6718dc380441a943dde12.jpg purple |pink | achromatic

up-319759b19c25eaa2fca9265da5513e00.jpg|

up-5b7476cfbd3f7d4da78662c09577813e.jpg|

up-e5a4301e073bdd342e38f06ed9bbf1a6.jpg

It is also possible to use a median filter (3 x 3) in order to reduce the amount of pixels of a given color that are isolated in the array:

>>> color_extraction.get_bool_arrays(img, median_filter=True)

It is also possible to use your own color definitions saved in a JSON file.

>>> color_extraction.get_bool_arrays(img, color_def_path=path_to_your_json_file)

RGB arrays

The function get_rgb_arrays returns a dictionary with a RGB array for each color. Each such array has the same horizontal and vertical dimensions as the source image. Positions where the color in question has been detected contain the original RGB color found in the source image; other positions have the value 0 (black), except in the case of the "achro(matic)" color, where they have the value 1 (white).

>>> dict_rgb_arrays = color_extraction.get_rgb_arrays(img)

>>> for color in dict_rgb_arrays:

... matplotlib.image.imsave(color, dict_rgb_arrays[color])

Using the following image as input:

Original image| white | red | orange --- | --- | --- | ---

up-a8803f4e117dfb0b8da26703c930cbde.jpg |

up-ddf96a05faafafd539d7d293bf5b28ce.jpg|

up-169a07facbee5d970719023406448f20.jpg |

up-bf3346fa222e45a92d7db0dab242ac92.jpg yellow | green | cyan | blue

up-0fe2da1fad13fc0c9406590ed1b0a7e5.jpg|

up-410ae0e4985f368212633f5354e2472d.jpg|

up-d2210cbd1b5c5afe42725852f1f1508b.jpg|

up-5e3a8956cf28e5322c0879ec730f9915.jpg purple |pink | achromatic

up-6064dec145ecf8d62391b8d7398bf5d2.jpg|

up-cc3f77a0ca69ac2e3e00f9927455a49c.jpg|

up-9ad7feace7680dbdf619b9be5edc3822.jpg

Similarly to get_bool_arrays, it is possible to use a median filter and/or your own color definition set, with the same parameters (median_filter and color_def_path).

Pixel counts

The function get_counts returns a dictionary with the number of pixels of each colour.

>>> color_extraction.get_counts(img)

{'purple': 25, 'blue': 6652, 'achro': 2477505, 'cyan': 764, 'white': 9567, 'green': 185, 'red': 114555, 'pink': 163, 'orange': 150263, 'yellow': 5121}

Similarly to get_bool_arrays, it is possible to use your own color definition set, with the same parameter (color_def_path). The median filter is not available for this function.

Dependencies

scipy.cluster.vq

skimage.filters

numpy

Authors

Christelle Cocco - initial implementation

Aris Xanthos - refactoring

Credits

This module was partially funded by the the Swiss National Science Foundation (SNSF), grant N° CR11I1_156383.

The current version (0.1a4) was implemented by Aris Xanthos based on the original code by Christelle Cocco available here.

License

This project is licensed under the GNU General Public License v3 - see the LICENSE file for details.

Acknowledgements

Copy from pypi.org

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值