【发布时间】:2017-10-22 05:59:08
【问题描述】:
我有一组图像,根据它们的类型,它们位于 3 个单独的文件夹中。我想遍历每个类型并计算每个图像的红色像素值。我为红色设置了一个限制,范围从 200 到 256。我想为每种类型创建直方图,然后对直方图进行聚类并区分 3 个类。我在 Python 方面的经验非常有限,我被困在如何隔离和计算红色像素值上。我附上了我的代码和类型 1 的结果直方图,它是一条直线。有人可以帮忙吗?
import numpy as np
import cv2
import os.path
import glob
import matplotlib.pyplot as plt
## take the image, compute sum of all row colors and return the percentage
#iterate through every Type
for t in [1]:
#load_files
files = glob.glob(os.path.join("..", "data", "train", "Type_{}".format(t), "*.jpg"))
no_files = len(files)
#iterate and read
for n, file in enumerate(files):
try:
image = cv2.imread(file)
hist = cv2.calcHist([img], [0], None, [56], [200, 256])
print(file, t, "-files left", no_files - n)
except Exception as e:
print(e)
print(file)
plt.plot(hist)
plt.show()
【问题讨论】:
-
为什么要转换
BGR2RGB?如果你只关心红色,那你为什么要在所有 3 个频道上都做inRange?为什么不直接抓取红色通道(使用cv2.split或仅使用 numpy 索引)并使用它? -
我尝试做
image = cv2.imread(file)img= cv2.split(image),但它返回 img 不是 numpy 数组,也不是标量 -
numpy 索引是什么意思?
-
是的,它不是一个 numpy 数组,它是一个 python 列表,每个原始通道包含一个单通道 numpy 数组(例如,一个 BGR 图像将被分成 3 个单独的数组)。 Numpy array indexing
-
所以,为了获得红色通道,我应该添加一行
im = img[0, :, :]?
标签: python opencv numpy image-processing scikit-image