简体   繁体   English

使用 python 查找图像中最暗的颜色

[英]Find darkest color in image with python

I was just wondering you could find the darkest colour in a greyscale image with python.我只是想知道您是否可以使用 python 在灰度图像中找到最暗的颜色。 I would prefer using pillow but OpenCV would be fine.我更喜欢使用枕头,但 OpenCV 会很好。 I found this but couldn't make sense of it.我发现了这一点,但无法理解。 If this is simple just say.如果这很简单,那就说吧。

Thanks谢谢


** edit ** ** 编辑 **

The issue that I have is that with the rest of the script the darkest colour is very unlikely to be black.我遇到的问题是,对于脚本的 rest,最暗的颜色不太可能是黑色。

So in the image, after you read using OpenCV or Pillow, and because it is grayscale, the darkest "color" should be 0, which is black.所以在图像中,当你使用 OpenCV 或 Pillow 阅读后,因为它是灰度的,所以最暗的“颜色”应该是 0,也就是黑色。 The range of pixel values is from 0 and 255. If you want to find the darkest value, you can just use the minimal function.像素值的范围是从 0 到 255。如果你想找到最暗的值,你可以使用最小的 function。

import cv2
img = cv2.imread("2.png", 0)
print(min(img.flatten()))

"For grayscale images, the pixel value is a single number that represents the brightness of the pixel. The most common pixel format is the byte image, where this number is stored as an 8-bit integer giving a range of possible values from 0 to 255. Typically zero is taken to be black, and 255 is taken to be white." “对于灰度图像,像素值是表示像素亮度的单个数字。最常见的像素格式是字节图像,其中该数字存储为 8 位 integer 给出的可能值范围从 0 到255. 通常将 0 视为黑色,将 255 视为白色。”

Reference: https://homepages.inf.ed.ac.uk/rbf/HIPR2/value.htm参考: https://homepages.inf.ed.ac.uk/rbf/HIPR2/value.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM