Tech Tips

  1. 要素技術
  2. 1080 view

[ImageProcessing]肌色で2値化

Contents

HSV表色系

V
一般的によくつかわれているRGBからHSVに変換するには以下の式を用いる。
S
H

ソースコード

require 'imlib2'
include Math

def rgb2hsv(r, g, b)
    h = s = v = 0.0

    r = r.to_f
    g = g.to_f
    b = b.to_f
    color = [r, g, b]
    cmax = color.max
    cmin = color.min
    v = cmax

    c = cmax - cmin
    if cmax == 0.0
        s = 0.0
    else
        s = c/cmax
    end

    if s != 0.0
        if r == cmax
            h = (g - b)/c
        elsif g == cmax
            h = 2 + (g - r)/c
        elsif b == cmax
            h = 4 + (r - g)/c
        end
        h *= 60.0
        if h < 0.0
            h += 360.0
        end
    end
    return h, s, v
end

# Main part
img = Imlib2::Image.load("srcimg/src.jpg")
minHue = 0.0
maxHue = 30.0

img.h.times do |y|
    img.w.times do |x|
        color = img.pixel(x, y)

        h, s, v = rgb2hsv(color.r, color.g, color.b)

        col = 255
        if h >= minHue and h <= maxHue
            col = 0
        end
        color.r = color.g = color.b = col
        img.draw_pixel(x, y, color)
    end
end

img.save("dstimg/binarize.jpg")
実行してみる。

入力画像

src

出力画像

dst

要素技術の最近記事

  1. Udacity Self-Driving Car Engineer Nanodegree …

  2. TF-Agents で独自の環境を利用する方法

  3. Amazon Rekognition Video を使って動画内の顔検出と境界ボックスの描…

  4. [触ってみた]Microsoft Quantum Development Kit

  5. Tensorboardを使ってニューラルネットワークと学習の状況を可視化する

関連記事

PAGE TOP