2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Mask R-CNN とK-Meansクラスタリングで画像中の対象物の色を抽出する

Posted at

GitHubリポジトリ

概要

Mask R-CNNと提供されている学習済みのモデルを使って、画像中の対象物のみをRGBデータとして抽出する。
そのRGBデータをK-Meansでクラスタリングすると対象物のドミナントカラーを抽出できる。

#例
###ピザ(pizza)


Photo by mahyar motebassem on Unsplash

###信号機(traffic light)


Photo by Aleksandr Kotlyar on Unsplash

###犬(dog)

使い方

$ git clone https://github.com/xy-gao/instance-dominant-colors.git
$ cd instance-dominant-colors
$ pip3 install -r requirements.txt
from instance2color import Instance2Color

inst = Instance2Color(image_file='sample_img/pizza.jpg', class_name='pizza', num_of_color=5)
# 指定できるclass_name:
# class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
#                 'bus', 'train', 'truck', 'boat', 'traffic light',
#                 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
#                 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
#                 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
#                 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
#                 'kite', 'baseball bat', 'baseball glove', 'skateboard',
#                 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
#                 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
#                 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
#                 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
#                 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
#                 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
#                 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
#                 'teddy bear', 'hair drier', 'toothbrush']
# 学習済みモデルは初回にダウンロードされる

print(inst.dominant_colors()) # ドミナントカラーのRGBと割合
# [([174, 77, 68], 29.52), ([223, 140, 100], 26.31), ([99, 47, 38], 20.08), ([96, 181, 108], 12.91), ([227, 206, 189], 11.18)]
inst.visualize_pie(output_file='sample_img/pizza_pie_chart.jpg')# 円グラフを描く
inst.visualize_instance(output_file='sample_img/pizza_inst.jpg')# どこが対象物にされたか確認できる
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?