【问题标题】:Unique Color Detection and Storing images dynamically独特的颜色检测和动态存储图像
【发布时间】:2021-05-26 19:40:17
【问题描述】:

如果给定图像,找出该图像中的唯一颜色并写入与每个唯一颜色对应的输出图像。 因为我应该将所有其他没有该独特颜色的像素标记为白色。

例如,如果图像有 3 种颜色 - 在输出文件夹中应该有三个图像,其中每种颜色是分开的。使用 Open CV 和 Python。

我已经使用我的方法创建了唯一的颜色列表。我想要的是计算 sample.png 图像中所有这些独特颜色的数量,并根据问题给出相应的图像输出。

【问题讨论】:

  • 那么你应该编写一个程序来做到这一点。
  • 对于上面的评论,我深表歉意。请不要将其视为整个 stackoverflow 社区的代表。如果您可以附上示例图片以及您目前拥有的代码,这将有助于我们理解和解决您的问题。
  • 我写了一个kmeans分类代码来检测颜色。但是在返回图像时,我只得到黑色或白色背景。虽然我自己解决了。谢谢! :)

标签: python numpy opencv machine-learning computer-vision


【解决方案1】:

Ack,他打败了我。好吧,这就是我所拥有的。

哦不,我不认为这条线

blank[img == color] = img[img == color]

按照我的想法行事。我认为它只是巧合地适用于这种情况。我将使用我更有信心适用于所有情况的解决方案来编辑​​代码。

原图

import cv2
import numpy as np

# load image
img = cv2.imread("circles.png");

# get uniques
unique_colors, counts = np.unique(img.reshape(-1, img.shape[-1]), axis=0, return_counts=True);

# split off each color
splits = [];
for a in range(len(unique_colors)):
    # get the color
    color = unique_colors[a];
    blank = np.zeros_like(img);
    mask = cv2.inRange(img, color, color); # edited line 1
    blank[mask == 255] = img[mask == color]; # edited line 2

    # show
    cv2.imshow("Blank", blank);
    cv2.waitKey(0);

    # save each color with its count
    file_str = "";
    for b in range(3):
        file_str += str(color[b]) + "_";
    file_str += str(counts[a]) + ".png";
    cv2.imwrite(file_str, blank);

【讨论】:

    【解决方案2】:

    我相信下面的代码(带有 cmets)应该可以帮助您解决这个问题!

    如果有任何代码不清楚,请随时跟进!

    import numpy as np
    import cv2 as cv
    import matplotlib.pyplot as plt
    from copy import deepcopy
    
    # Load image and convert it from BGR (opencv default) to RGB
    fpath = "dog.png"  # TODO: replace with your path
    IMG = cv.cvtColor(cv.imread(fpath), cv.COLOR_BGR2RGB)
    
    # Get dimensions and reshape into (H * W, C) vector - i.e. a long vector, where each element is a tuple corresponding to a color!
    H, W, C = IMG.shape
    IMG_FLATTENED = np.vstack([IMG[:, w, :] for w in range(W)])
    
    # Get unique colors using np.unique function, and their counts
    colors, counts = np.unique(IMG_FLATTENED, axis=0, return_counts = True)
    
    # Jointly loop through colors and counts
    for color, count in zip(colors, counts):
    
        print("COLOR: {}, COUNT: {}".format(color, count))
        # Create placeholder image and mark all pixels as white
        SINGLE_COLOR = (255 * np.ones(IMG.shape)).astype(np.uint8)  # Make sure casted to uint8
    
        # Compute binary mask of pixel locations where color is, and set color in new image
        color_idx = np.all(IMG[..., :] == color, axis=-1)
        SINGLE_COLOR[color_idx, :] = color
    
        # Write file to output with color and counts specified
        cv.imwrite("color={}_count={}.png".format(color, count), SINGLE_COLOR)
    

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 2019-03-25
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多