Production Level Knowledge & Tips

  1. 未分類
  2. 8 view

[ImageProcessing]Binarize with flesh color

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

未分類 recent post

  1. Install sbt 1.0.0 and run sample template

  2. Visualization of Neural Network and its Train…

  3. [Machine Learning]Created docker image includ…

  4. [Node.js]How to write batch script with Node.…

  5. [Hive]Get top n items for each category

関連記事

PAGE TOP