【问题标题】:Count total number of pixels for each color计算每种颜色的像素总数
【发布时间】:2021-11-12 04:43:47
【问题描述】:

我正在尝试计算分割图像的每种颜色对应的像素数

以下是我写的代码。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-



import argparse
from ast import literal_eval

try:
    from PIL import Image, ImageDraw, ImageFont
except ImportError:
    exit("This script requires the PIL module. Install with pip install Pillow")

try:
    import webcolors
except ImportError:

    exit("This script uses webcolors for displaying named colors. Install with pip install webcolors")

def count_pixels(filename):
    """
    Returns a count of pixels per a unique color
    Args:
        filename (str): the image to count the number of pixels of
    Returns:
        a key-value pairing of the rgb color value and the number of times the color was present in the image
    """
    color_count = {}
    with Image.open(filename) as image:
        width, height = image.size
        rgb_image = image.convert('RGB')

        # iterate through each pixel in the image and keep a count per unique color
        for x in range(width):
            for y in range(height):
                rgb = rgb_image.getpixel((x, y))

                if rgb in color_count:
                    color_count[rgb] += 1
                else:
                    color_count[rgb] = 1

    return color_count

def create_legend_image(filename, colors, title, ignore_color):
    """
    Create an image of color swatches paired with a color name and number
    Args:
        filename (str): the name of the legend image file
        colors (dict): a key-value pairing of the color name and count
        title (str): a title for the legend image
        ignore_color (tuple): do not add this color to the legend image
    Returns:
        None
    """
    margin = 10
    rect_width = 25
    rect_outline_width = 2
    font_size = 20
    img_width = 250
    img_height = len(colors) * (rect_width + rect_outline_width + margin) + (0 if title is None else font_size)
    legend_img = Image.new("RGBA", (img_width, img_height), "white")
    draw = ImageDraw.Draw(legend_img)    
    font = ImageFont.truetype("arialbd.ttf", font_size)
    
    # draw title for legend if applicable
    text_height = 0
    if title is not None:
        _, text_height = draw.textsize(title, font=font)
        draw.text((0, 0), title, font=font, fill="black")

    color_index = 1
    for color, count in colors.items():
        if color == ignore_color:
            continue

        try:
            # convert RGB color to a human readable color if applicable
            color_name = webcolors.rgb_to_name(color)
        except ValueError:                
            color_name = color

        # draw square for color legend
        y0 = rect_width * (color_index - 1) + margin * color_index + text_height
        y1 = rect_width * color_index + margin * color_index + text_height
        draw.rectangle([(0, y0), (rect_width, y1)], fill=color, outline="black", width=2)

        # draw color name next and pixel count for legend colors
        draw.text((rect_width + margin, y0), "{}: {}".format(color_name, count), font=font, fill="black")

        color_index += 1

    legend_img.save(filename, mode="w")

def main():
    parser = argparse.ArgumentParser(description='Calculates the sum of pixels per a color')
    parser.add_argument('image', nargs='?', default='.', help='The image to sum the pixels per a color of')
    parser.add_argument('-i', '--ignore-color', help='Skip counting pixels of this color')
    parser.add_argument('-t', '--title', help='Title for the image legend')
    parser.add_argument('-l', '--legend-image', help='Generate an image with color swatches paired with the pixel count')

    args = parser.parse_args()
    ignore_color = literal_eval(args.ignore_color) if args.ignore_color is not None else None
    color_count = count_pixels(args.image)                   

    if args.legend_image is not None:
        create_legend_image(args.legend_image, color_count, args.title, ignore_color)
 
    # outputs pixel color count to console
    print('Pixel Count per Unique Color:')
    print('-' * 30)
    color_index = 1

    for color, count in color_count.items(): 
        if color == ignore_color:
            continue

        try:
            # convert RGB color to a human readable color if applicable
            color_name = webcolors.rgb_to_name(color)
        except ValueError:                
            color_name = color

        print('{}.) {}: {}'.format(color_index, color_name, count))

        color_index += 1

    # Display the total number of pixels not ignored
    print('-' * 30)
    print('\t{} pixels'.format(sum(color_count[color] for color in color_count if color != ignore_color)))

if __name__ == '__main__':
    main()

当我测试我的代码时,我没有得到准确的结果,它表示图片中表示的每种独特颜色(即(红色、黄色和黑色)的像素总数),您能否建议我一些替代方法来计算分割图像中的总像素数

【问题讨论】:

  • np.histogram?
  • @QuangHoang ,我之前尝试过,但我无法获得准确的结果,你可以在我的图像上使用 np.histogram 进行测试,你会得到一个奇怪的错误,它在图像中显示了几种颜色虽然我只能看到 3 种颜色和小白色像素
  • 您的眼睛可能无法检测到相似的颜色,例如(100, 100, 100)(100, 100, 99)。你可能想用箱做直方图...
  • 您确实不应该将分段、标记或分类的图像存储为 JPEG。它是一种改变像素的LOSSY 格式。您发布的图片有近 7,000 种独特的颜色。你有正确的无损PNG吗?
  • @MarkSetchell ,我有一个 .png 图片,我现在就发布它

标签: python-3.x matplotlib python-imaging-library


【解决方案1】:

这是获取颜色及其对应计数的最简单方法:

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Open image and ensure RGB
im = Image.open('UMN9c.png').convert('RGB')

# Make into Numpy array
na = np.array(im)

# Get colours and corresponding counts
colours, counts = np.unique(na.reshape(-1,3), axis=0, return_counts=1)

print(colours, counts)

样本输出

 [[ 37  36  36]
 [226  44  11]
 [228 239  80]] [255169   1059   5916]

如果您不想编写任何 Python 代码,可以在终端中使用 ImageMagick

magick -verbose UMN9c.png -format %c histogram:info:-

输出

UMN9c.png PNG 512x512 512x512+0+0 8-bit sRGB 3572B 0.020u 0:00.008
255169: (37,36,36,255) #252424FF srgba(37,36,36,1)
1059: (226,44,11,255) #E22C0BFF srgba(226,44,11,1)
5916: (228,239,80,255) #E4EF50FF srgba(228,239,80,1)
UMN9c.png=>info:- PNG 512x512 512x512+0+0 8-bit sRGB 3572B 0.320u 0:00.166

我不确定完全速度是否对您来说是个问题。如果是这样,在this 答案的末尾使用np.dot() 有一个明显更快的方法。

【讨论】:

    【解决方案2】:

    使用 OpenCV 进行导入的解决方案。

    # Load image
    img = cv2.imread("UMN9c.png")
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    
    # Flatten image into rows = number of pixels, columns = 3 (RGB)
    img = img.reshape(-1, img.shape[2])
    
    # Count number of unique colors
    unique_colors, unique_count = np.unique(img, axis=0, return_counts=True)
    
    # Print info
    print(unique_colors)
    print(unique_count)
    print("Total number of unique colors: ", unique_colors.shape[0])
    

    【讨论】:

      猜你喜欢
      • 2020-08-03
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2011-11-29
      相关资源
      最近更新 更多