Upgrade to Pro — share decks privately, control downloads, hide ads and more …

画像の特徴色抽出

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 画像の特徴色抽出

Julia で画像の特徴色を抽出するお話

Avatar for Kenta Murata

Kenta Murata

July 11, 2015
Tweet

More Decks by Kenta Murata

Other Decks in Technology

Transcript

  1. $ julia typical_colors.jl --help usage: typical_colors.jl [-k K] [-h] filenames...

    This program extract typical colors from the given images positional arguments: filenames an image filenames optional arguments: -k K the number of typical colors to be extracted (type: Int64, default: 3) -h, --help show this help message and exit $ ࠷৽൛͸Φϓγϣϯ͕গ͠૿͑ͯΔ
  2. $ julia typical_colors.jl -k 6 genovese.jpg arrabbiata.jpg giraffe.jpg pandas.jpg Typical

    colors of genovese.jpg: [1] rgb(#9da33d), hsv(63.56, 0.628, 0.639) [2] rgb(#a77740), hsv(31.91, 0.614, 0.653) [3] rgb(#91a03c), hsv(68.52, 0.623, 0.626) [4] rgb(#b38a4b), hsv(36.34, 0.578, 0.700) [5] rgb(#3c855b), hsv(145.14, 0.546, 0.521) [6] rgb(#829938), hsv(74.40, 0.633, 0.601) Typical colors of arrabbiata.jpg: [1] rgb(#b81a0b), hsv(5.14, 0.940, 0.722) [2] rgb(#2896c9), hsv(199.04, 0.802, 0.787) [3] rgb(#f8972a), hsv(31.68, 0.832, 0.973) [4] rgb(#e16820), hsv(22.44, 0.859, 0.884) [5] rgb(#a11013), hsv(358.73, 0.904, 0.633) [6] rgb(#c5451d), hsv(14.19, 0.852, 0.771) Typical colors of giraffe.jpg: [1] rgb(#ac7042), hsv(26.15, 0.615, 0.674) [2] rgb(#5781bb), hsv(214.93, 0.536, 0.734) [3] rgb(#5786bd), hsv(212.22, 0.539, 0.741) [4] rgb(#ca8f50), hsv(31.18, 0.604, 0.792) [5] rgb(#9d603b), hsv(22.21, 0.624, 0.617) [6] rgb(#935337), hsv(18.17, 0.627, 0.577) Typical colors of pandas.jpg: WARNING: The objective value changes towards an opposite direction [1] rgb(#7a9a42), hsv(81.93, 0.569, 0.603) [2] rgb(#6c9a3e), hsv(89.87, 0.597, 0.605) [3] rgb(#94953d), hsv(60.58, 0.587, 0.583) [4] rgb(#5e933d), hsv(97.30, 0.584, 0.578) [5] rgb(#966e39), hsv(34.45, 0.620, 0.589) [6] rgb(#a08b44), hsv(45.91, 0.574, 0.628) $ ࣮ߦ݁Ռ
  3. function extract_typical_colors(filename, k) image = imread(filename) hsv_image = convert(Image{HSV}, float32(image))

    hsv_pixels = filter( x -> (x.s > 0.5 && x.v > 0.5), hsv_image.data ) pixel_vectors = Array(Float32, 3, length(hsv_pixels)) for i in 1:length(hsv_pixels) pixel_vectors[:, i] = hsv2vec(hsv_pixels[i]) end result = kmeans(pixel_vectors, k) for i in 1:size(result.centers, 2) hsv = vec2hsv(result.centers[:, i]) rgb = convert(RGB, hsv) rgb_vec = round(Uint8, 255*[rgb.r, rgb.g, rgb.b]) @printf("[%d] rgb(#%02x%02x%02x), hsv(%.2f, %.3f, %.3f)\n”, i, rgb_vec[1], rgb_vec[2], rgb_vec[3], hsv.h, hsv.s, hsv.v) end end
  4. function extract_typical_colors(filename, k) image = imread(filename) hsv_image = convert(Image{HSV}, float32(image))

    hsv_pixels = filter( x -> (x.s > 0.5 && x.v > 0.5), hsv_image.data ) pixel_vectors = Array(Float32, 3, length(hsv_pixels)) for i in 1:length(hsv_pixels) pixel_vectors[:, i] = hsv2vec(hsv_pixels[i]) end result = kmeans(pixel_vectors, k) for i in 1:size(result.centers, 2) hsv = vec2hsv(result.centers[:, i]) rgb = convert(RGB, hsv) rgb_vec = round(Uint8, 255*[rgb.r, rgb.g, rgb.b]) @printf("[%d] rgb(#%02x%02x%02x), hsv(%.2f, %.3f, %.3f)\n”, i, rgb_vec[1], rgb_vec[2], rgb_vec[3], hsv.h, hsv.s, hsv.v) end end ը૾ΛಡΈࠐΜͰ HSV ৭ۭؒʹม׵͠ɺS > 0.5 && V > 0.5 ͰϐΫηϧΛϑΟϧλ ͨͬͨ͜Ε͚ͩͰ HSV ʹม׵ͯ͠ϑΟϧλ΋Ͱ͖ͪΌ͏
  5. function extract_typical_colors(filename, k) image = imread(filename) hsv_image = convert(Image{HSV}, float32(image))

    hsv_pixels = filter( x -> (x.s > 0.5 && x.v > 0.5), hsv_image.data ) pixel_vectors = Array(Float32, 3, length(hsv_pixels)) for i in 1:length(hsv_pixels) pixel_vectors[:, i] = hsv2vec(hsv_pixels[i]) end result = kmeans(pixel_vectors, k) for i in 1:size(result.centers, 2) hsv = vec2hsv(result.centers[:, i]) rgb = convert(RGB, hsv) rgb_vec = round(Uint8, 255*[rgb.r, rgb.g, rgb.b]) @printf("[%d] rgb(#%02x%02x%02x), hsv(%.2f, %.3f, %.3f)\n”, i, rgb_vec[1], rgb_vec[2], rgb_vec[3], hsv.h, hsv.s, hsv.v) end end ϐΫηϧΛ3࣍ݩ࠲ඪ஋ͷߦྻʹม׵͠ɺK-means++ Ͱ ΫϥελϦϯά ͍͢͝؆୯Ͱ͍͢͝
  6. function extract_typical_colors(filename, k) image = imread(filename) hsv_image = convert(Image{HSV}, float32(image))

    hsv_pixels = filter( x -> (x.s > 0.5 && x.v > 0.5), hsv_image.data ) pixel_vectors = Array(Float32, 3, length(hsv_pixels)) for i in 1:length(hsv_pixels) pixel_vectors[:, i] = hsv2vec(hsv_pixels[i]) end result = kmeans(pixel_vectors, k) for i in 1:size(result.centers, 2) hsv = vec2hsv(result.centers[:, i]) rgb = convert(RGB, hsv) rgb_vec = round(Uint8, 255*[rgb.r, rgb.g, rgb.b]) @printf("[%d] rgb(#%02x%02x%02x), hsv(%.2f, %.3f, %.3f)\n”, i, rgb_vec[1], rgb_vec[2], rgb_vec[3], hsv.h, hsv.s, hsv.v) end end ݁ՌΛදࣔ ΄Μͱ͸ JSON Λग़͍͚ͨ͠Ͳؒʹ߹Θͳ͔ͬͨ
  7. #6e9b1b #a56d3f #94ad1d #648716 #72434c #c39d88 #6e9b1b #a56d3f #94ad1d #648716

    #72434c #c39d88 non-filtered filtered ͍͍ͱ͜औΓ͠Α͏ ϑΟϧλ͠ͳ͍৔߹ͱͷൺֱ http://cookpad.com/recipe/1749730