![](/img/trans.png)
[英]How can I yield notifications and return a result from a function? (Python)
[英]How can I train a python function to return the result I want?
我要解決的問題是我想以相當合理的確定度檢測圖像是黑色還是大部分是黑色。 我已經編寫了獲取顏色直方圖的代碼,下一步是編寫一個函數,該函數將使用(r,g,b)
元組,並給我一個bool
指示它是黑色還是接近黑色。 可以做到這一點不是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))
我有一套可以用作語料庫的測試圖像。 最好的圖書館/方法是什么?
我希望訓練的功能會像
def is_black(r, g, b):
if magic_says_black():
return True
return False
由於您只關心亮度,因此將圖像轉換為灰度會更容易,因此您只需要使用一個通道而不是三個通道。
然后,您有許多選擇:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.