我解决了我的问题,现在我可以计算来自语义分割数据集的图像中的颜色(图像必须是 .png,因为它是无损格式)。
下面我尝试解释我在解决方案的过程中发现了什么以及我使用的应该可以使用的代码(您只需更改要分析的图像的路径)。
我有两个主要问题。
颜色计数的第一个问题是图像的格式。我正在使用(用于某些测试)压缩图像的 .jpeg 图像。
因此从这样的事情
如果我放大玻璃的左上角(标记为绿色),我会看到类似这样的东西
这显然不好,因为它会引入比“人眼可见”更多的颜色
相反,对于带注释的图像,我有如下内容
如果我放大自行车的鞍座(标记为绿色),我会看到类似的东西
第二个问题是我没有将图像转换为 RGB 图像。
这在代码中的代码中得到了注意:
img = Image.open(filename).convert('RGB')
代码如下。可以肯定的是,它不是最有效的,但对我来说它可以完成这项工作。任何提高其性能的建议表示赞赏
import numpy as np
from PIL import Image
import argparse
import os
debug = False
def main(data_dir):
print("This small script allows you to count the number of different colors in an image")
print("This code has been written to count the number of classes in images from a semantic segmentation dataset")
print("Therefore, it is highly recommended to run this code on lossless images (such as .png ones)")
print("Images are being loaded from: {}".format(data_dir))
directory = os.fsencode(data_dir)
interesting_image_format = ".png"
# I will put in the variable filenames all the paths to the images to be analyzed
filenames = []
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(interesting_image_format):
if debug:
print(os.path.join(directory, filename))
print("Analyzing image: {}".format(filename))
filenames.append(os.path.join(data_dir, filename))
else:
if debug:
print("I am not doing much here...")
continue
# Sort the filenames in an alphabetical order
filenames.sort()
# Analyze the images (i.e., count the different number of colors in the images)
number_of_colors_in_images = []
for filename in filenames:
img = Image.open(filename).convert('RGB')
if debug:
print(img.format)
print(img.size)
print(img.mode)
data_img = np.asarray(img)
if debug:
print(data_img.shape)
uniques = np.unique(data_img.reshape(-1, data_img.shape[-1]), axis=0)
# uncomment the following line if you want information for each analyzed image
print("The number of different colors in image ({}) {} is: {}".format(interesting_image_format, filename, len(uniques)))
# print("uniques.shape[0] for image {} is: {}".format(filename, uniques.shape[0]))
# Put the number of colors of each image into an array
number_of_colors_in_images.append(len(uniques))
print(number_of_colors_in_images)
# Print the maximum number of colors (classes) of all the analyzed images
print(np.max(number_of_colors_in_images))
# Print the average number of colors (classes) of all the analyzed images
print(np.average(number_of_colors_in_images))
def args_preprocess():
# Command line arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"--data_dir", default="default_path_to_images", type=str, help='Specify the directory path from where to take the images of which we want to count the classes')
args = parser.parse_args()
main(args.data_dir)
if __name__ == '__main__':
args_preprocess()