[英]How can I train a python function to return the result I want?
The problem I'm looking at is that I want to detect with a fairly reasonable level of certainty whether an image is black or mostly black. 我要解决的问题是我想以相当合理的确定度检测图像是黑色还是大部分是黑色。 I already have the code written to get the color histogram, and the next step is to write a function that will take the
(r,g,b)
tuple and give me a bool
indicating whether it's black or close to it. 我已经编写了获取颜色直方图的代码,下一步是编写一个函数,该函数将使用
(r,g,b)
元组,并给我一个bool
指示它是黑色还是接近黑色。 It's OK for this to not be 100% accurate, but it would be better to err toward false positives. 可以做到这一点不是100%准确,但最好还是避免误报。
def average_image_color(i):
h = i.histogram()
# split into red, green, blue
r = h[0:256]
g = h[256:256*2]
b = h[256*2: 256*3]
# perform the weighted average of each channel:
# the *index* is the channel value, and the *value* is its weight
return (
sum( i*w for i, w in enumerate(r) ) / sum(r),
sum( i*w for i, w in enumerate(g) ) / sum(g),
sum( i*w for i, w in enumerate(b) ) / sum(b))
I have a set of test images that I can use as a corpus. 我有一套可以用作语料库的测试图像。 What's the best library/approach to this?
最好的图书馆/方法是什么?
The function I'd be hoping to train would be something like 我希望训练的功能会像
def is_black(r, g, b):
if magic_says_black():
return True
return False
Since you are only concerned about brightness, it would be easier if you converted the image to grayscale so you only have to work with one channel instead of three. 由于您只关心亮度,因此将图像转换为灰度会更容易,因此您只需要使用一个通道而不是三个通道。
Then you have a number of options: 然后,您有许多选择:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.