Save 37% off PRO during our Black Friday Sale! »

画像の特徴色抽出

 画像の特徴色抽出

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

7cca11c5257fda526eeb4b1ada28f904?s=128

Kenta Murata

July 11, 2015
Tweet

Transcript

  1. ը૾ͷಛ௃৭நग़ Kenta Murata (mrkn) Cookpad Inc. 2015.07.11 JuliaTokyo #4

  2. ϨγϐΛ৭ͰݕࡧͰ͖ΔΑ͏ʹ͍ͨ͠ (ελοϑ޲͚ػೳ) ͖͔͚ͬ

  3. https://github.com/mrkn/typical_colors

  4. $ 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 $ ࠷৽൛͸Φϓγϣϯ͕গ͠૿͑ͯΔ
  5. $ 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) $ ࣮ߦ݁Ռ
  6. How to extract these colors?

  7. 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
  8. 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 ʹม׵ͯ͠ϑΟϧλ΋Ͱ͖ͪΌ͏
  9. 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++ Ͱ ΫϥελϦϯά ͍͢͝؆୯Ͱ͍͢͝
  10. 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 Λग़͍͚ͨ͠Ͳؒʹ߹Θͳ͔ͬͨ
  11. #6e9b1b #a56d3f #94ad1d #648716 #72434c #c39d88 http://goldengreen.lolipop.jp/wp-content/uploads/DSC_0027.jpg ύελͷ৭ɺࡼͷ৭ɺഎܠͷ৭ͱ͔औΕͯΔ

  12. #6e9b1b #a56d3f #94ad1d #648716 #72434c #c39d88 http://winegohan.up.n.seesaa.net/winegohan/image/P7132862.jpg?d=a3

  13. #6e9b1b #a56d3f #94ad1d #648716 #72434c #c39d88 http://pds.exblog.jp/pds/1/201203/19/25/d0100125_18262669.jpg k=6 ΋͍Β͍ͳ͍͔΋

  14. #6e9b1b #a56d3f #94ad1d #648716 #72434c #c39d88 http://www.cnn.co.jp/storage/2013/10/16/69c702ba10d511b7b558bdfb348c5a12/animal-panda1.jpg നͱࠇ͕ແ͍

  15. #6e9b1b #a56d3f #94ad1d #648716 #72434c #c39d88 #6e9b1b #a56d3f #94ad1d #648716

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

  17. #6e9b1b #a56d3f #94ad1d #648716 #72434c #c39d88 #648716 ຊ౰ʹཉ͍͠৭͕औΕͳ͍৔߹΋ http://cookpad.com/recipe/704436

  18. Demo

  19. None