简体   繁体   English

如何训练python函数返回所需的结果?

[英]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: 然后,您有许多选择:

  • image is mostly black if average pixel intensity is above an empirically determined threshold; 如果平均像素强度高于经验确定的阈值,则图像大部分为黑色;
  • count the number of pixels that are more than some threshold 计算超过某个阈值的像素数
  • if you have plenty of example images, train a classifier such as an SVM using a grayscale histogram (this does seem like using a sledge hammer to crack a walnut). 如果您有很多示例图像,请使用灰度直方图训练分类器(例如SVM)(这似乎就像使用大锤打碎核桃一样)。 You will find plenty of classifiers in the scikit-learn package . 您可以在scikit-learn包中找到许多分类器。

暂无
暂无

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

相关问题 如何产生通知并从函数返回结果? (Python) - How can I yield notifications and return a result from a function? (Python) 我如何在Python中让函数返回不止1个结果(带有for循环和字典)? - How can I get my function to return more than 1 result (with for-loops and dictionaries) in Python? 如何让python函数返回异常回溯或结果? - How do I have a python function return an exception traceback, or the result? 如何使用函数的返回结果? - How can i use the return result from function? pandas function any() 不返回我想要的结果 - the pandas function any() don’t return the result what i want 如何将某些内容附加到函数的结果中并将其存储在python中的变量中 - How can I append something to a result of a function and store that in a Variable in python 如何使用PyBrain训练简单的线性函数? - How can I use PyBrain to train a simple linear function? 如何在TFLearn的一个python模块中训练多个模型? - How can I train multiple models in one python module in TFLearn? 我向一个函数发送了 3 个值,但在函数之后我只想再次测试第 3 个函数返回。 我怎样才能做到这一点? - I an sending 3 values to a function but after the function I just want to test agains the 3th function return. How can I do this? 如何预处理一个巨大的数据集并保存它以便我可以在 Python 中训练数据 - How to preprocess a huge dataset and save it such that I can train the data in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM