Tech Tips

  1. Uncategorized
  2. 114 view

[ImageProcessing]Binarize with flesh color

Contents

HSV

V
Following formula is used to convert famous color scheme RGB to HSV.
S
H

Source Code

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")
Try to execute.

Input Image

src

Output Image

dst

Uncategorized recent post

  1. Run Amazon FreeRTOS on M5Stack Core2 for AWS …

  2. Udacity Self-Driving Car Engineer Nanodegree …

  3. Install sbt 1.0.0 and run sample template

  4. Visualization of Neural Network and its Train…

  5. [Machine Learning]Created docker image includ…

関連記事

PAGE TOP