14
12

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 5 years have passed since last update.

【Python】OpenCVで疑似カラー(Pseudo color)

Last updated at Posted at 2017-11-29

グレースケール画像に疑似的な色を付けることを疑似カラーと言います。
以下のようなデータがあった場合に、視覚的に分かりやすい表現が出来ます
 ・深度センサー(KinectやRealSenseやXtion等のDepthカメラ)
 ・サーモグラフィー
 ・照明ムラ
  など

OpenCVに疑似カラーを付けるAPI(applyColorMap())があったため、本投稿にメモ。

動画は以下。
https://www.youtube.com/watch?v=iOeULBjR7Jk
【Python】OpenCVで疑似カラー(Pseudo color)

ソースコードは以下。
本来ならサーモグラフィー等の画像を取り込みたいところですが、
Webカメラしかなかったので、グレースケール画像を取り込んで疑似カラーを付けてます。
色々なパターンがあるため、N押下で切り替えれるようにサンプルを作成。


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

import numpy as np
import cv2
import copy

colormap_table_count = 0
colormap_table = [
    ['COLORMAP_AUTUMN',  cv2.COLORMAP_AUTUMN ],
    ['COLORMAP_JET',     cv2.COLORMAP_JET    ],
    ['COLORMAP_WINTER',  cv2.COLORMAP_WINTER ],
    ['COLORMAP_RAINBOW', cv2.COLORMAP_RAINBOW],
    ['COLORMAP_OCEAN',   cv2.COLORMAP_OCEAN  ],
    ['COLORMAP_SUMMER',  cv2.COLORMAP_SUMMER ],
    ['COLORMAP_SPRING',  cv2.COLORMAP_SPRING ],
    ['COLORMAP_COOL',    cv2.COLORMAP_COOL   ],
    ['COLORMAP_HSV',     cv2.COLORMAP_HSV    ],
    ['COLORMAP_PINK',    cv2.COLORMAP_PINK   ],
    ['COLORMAP_HOT',     cv2.COLORMAP_HOT    ]
]

video_input = cv2.VideoCapture(0)

while(1):
    ret, frame = video_input.read()

    # グレースケール化
    grayscale_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    # 疑似カラーを付与
    apply_color_map_image = cv2.applyColorMap(grayscale_image, colormap_table[colormap_table_count % len(colormap_table)][1])

    cv2.putText(apply_color_map_image, colormap_table[colormap_table_count % len(colormap_table)][0], (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0,0,0),2);
    cv2.imshow('apply_color_map_image',apply_color_map_image)

    k = cv2.waitKey(50) & 0xff
    if k == 110: # N
        colormap_table_count = colormap_table_count + 1
    if k == 27:  # ESC
        break

video_input.release()
cv2.destroyAllWindows()

以上。

14
12
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
14
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?