プログラミング + アカデミック + 何か面白いこと

  1. Academic
  2. 14 view

[ImageProcessing]Binarize with flesh color

HSV

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

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

Academic recent post

  1. [Just Touch]Microsoft Quantum Development Kit…

  2. Visualization of Neural Network and its Train…

  3. [Machine Learning]Created docker image includ…

  4. [OpenCV][Ruby]Auto check web page design corr…

  5. [MachineLearning]How to introduce to real pro…

関連記事

PAGE TOP