Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以检测文本颜色&;python中opencv直方图的位置?_Python_Opencv_Text_Colors_Histogram - Fatal编程技术网

是否可以检测文本颜色&;python中opencv直方图的位置?

是否可以检测文本颜色&;python中opencv直方图的位置?,python,opencv,text,colors,histogram,Python,Opencv,Text,Colors,Histogram,我需要在opencv中检测图像上文本的颜色,并使用直方图获得平均颜色 有可能这样做吗 我现在有这个代码: color = ('b','g','r') for i,col in enumerate(color): histr = cv2.calcHist([img],[i],None,[256],[0,256]) plt.plot(histr,color = col) plt.xlim([0,256]) plt.show() 图像背景是透明的 在您发布的示例图像中,可以通

我需要在opencv中检测图像上文本的颜色,并使用直方图获得平均颜色

有可能这样做吗

我现在有这个代码:

color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()
图像背景是透明的


在您发布的示例图像中,可以通过给定的直方图近似文本的平均颜色

在一般情况下,您需要将文本与背景分离,并仅收集文本像素的直方图

在您发布的图像中,我们可以假设背景为白色(RGB颜色约为[255,255,255]),而文本为黑色(文本的所有RGB颜色分量值均较低)

您可以使用以下阶段:

  • 收集红色、绿色和蓝色通道的直方图
  • 从直方图中删除所有高值(将直方图值设置为零)。
    假设高值来自背景像素
  • 计算直方图的总和。
    总和表示原始图像中的像素计数
  • 根据直方图计算原始图像中的像素总和。
    例子: 如果
    h[100]=10

    然后图像中有10个值为100的像素。
    10个像素的总和为100*10。
    原始图像中的像素总和为:
    h[0]*0+h[1]*1+h[2]*2…
  • 计算平均值-除以总数
代码如下:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('img.png')  # Read input image

h_red = cv2.calcHist([img], [2], None, [256], [0,256])
h_green = cv2.calcHist([img], [1], None, [256], [0,256])
h_blue = cv2.calcHist([img], [0], None, [256], [0,256])

#h_red.sum() must be img.shape[0]*img.shape[1]

# Remove background pixels from the histograms.
# Set histogram bins above 230 with zero 
# assume all text has lower values of red, green and blue.
h_red[230:] = 0
h_green[230:] = 0
h_blue[230:] = 0

# Compute number of elements in histogram, after removing background
count_red = h_red.sum()
count_green = h_green.sum()
count_blue = h_blue.sum()

# Compute the sum of pixels in the original image according to histogram.
# Example:
# If h[100] = 10
# Then there are 10 pixels with value 100 in the image.
# The sum of the 10 pixels is 100*10.
# The sum of an pixels in the original image is: h[0]*0 + h[1]*1 + h[2]*2...
sum_red = np.sum(h_red * np.c_[0:256])
sum_green = np.sum(h_green * np.c_[0:256])
sum_blue = np.sum(h_blue * np.c_[0:256])

# Compute the average - divide sum by count.
avg_red = sum_red / count_red
avg_green = sum_green / count_green
avg_blue = sum_blue / count_blue

print('Text RGB average is about: {}, {}, {}'.format(avg_red, avg_green, avg_blue))
注:
我故意使代码简单,没有for循环。

我认为您最好修改代码,并使用for循环

在一般情况下,这是不可能的。在可能的情况下,有一些具体的情况。你能把图片贴出来吗?试着发布一个可执行代码示例。您的问题没有很好地定义。请澄清我们是否假设背景始终为白色。如果我们假设图像中除了文本之外没有其他对象/形状/图形,请澄清。