【问题标题】:Find average colour of each section of an image查找图像每个部分的平均颜色
【发布时间】:2019-09-17 13:40:53
【问题描述】:

我正在寻找使用 Python 实现以下目标的最佳方法:

  1. 导入图片。
  2. 添加一个包含 n 个部分的网格(以下示例中显示了 4 个)。
  3. 为每个部分找到主要颜色。

期望的输出

输出一个数组、列表、字典或类似的捕获这些主要颜色值的东西。

甚至可能是一个显示颜色的 Matplotlib 图表(如像素艺术)。

我尝试了什么?

可以使用图像切片器对图像进行切片:

import image_slicer
image_slicer.slice('image_so_grid.png', 4)

然后我可能会使用 this 之类的东西来获得平均颜色,但我确信有更好的方法来做到这一点。

使用 Python 实现此目的的最佳方法是什么?

【问题讨论】:

标签: python opencv machine-learning scikit-image


【解决方案1】:

您可以将 scikit-image 的 view_as_blocksnumpy.mean 一起使用。您指定块大小而不是块数:

import numpy as np
from skimage import data, util
import matplotlib.pyplot as plt

astro = data.astronaut()
blocks = util.view_as_blocks(astro, (8, 8, 3))

print(astro.shape)
print(blocks.shape)

mean_color = np.mean(blocks, axis=(2, 3, 4))

fig, ax = plt.subplots()
ax.imshow(mean_color.astype(np.uint8))

输出:

(512, 512, 3)
(64, 64, 1, 8, 8, 3)

不要忘记转换为 uint8,因为 matplotlib 和 scikit-image 期望浮点图像位于 [0, 1],而不是 [0, 255]。请参阅scikit-image documentation on data types 了解更多信息。

【讨论】:

    【解决方案2】:

    这适用于 4 个部分,但您需要弄清楚如何使其适用于“n”个部分:

    import cv2
    
    img = cv2.imread('image.png')
    
    def fourSectionAvgColor(image):
        rows, cols, ch = image.shape
        colsMid = int(cols/2)
        rowsMid = int(rows/2)
    
        numSections = 4
        section0 = image[0:rowsMid, 0:colsMid]
        section1 = image[0:rowsMid, colsMid:cols]
        section2 = image[rowsMid: rows, 0:colsMid]
        section3 = image[rowsMid:rows, colsMid:cols]
        sectionsList = [section0, section1, section2, section3]
    
        sectionAvgColorList = []
        for i in sectionsList:
            pixelSum = 0
            yRows, xCols, chs = i.shape
            pixelCount = yRows*xCols
            totRed = 0
            totBlue = 0
            totGreen = 0
            for x in range(xCols):
                for y in range(yRows):
                    bgr = i[y,x]
                    b = bgr[0]
                    g = bgr[1]
                    r = bgr[2]
                    totBlue = totBlue+b
                    totGreen = totGreen+g
                    totRed = totRed+r
    
            avgBlue = int(totBlue/pixelCount)
            avgGreen = int(totGreen/pixelCount)
            avgRed = int(totRed/pixelCount)
            avgPixel = (avgBlue, avgGreen, avgRed)
            sectionAvgColorList.append(avgPixel)
        return sectionAvgColorList
    
    print(fourSectionAvgColor(img))
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    【讨论】:

      猜你喜欢
      • 2013-06-04
      • 2019-07-24
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多