fbpx
维基百科

中位切割演算法

中位切割演算法(Median cut) 是Paul Heckbert於1979年提出來的演算法。概念上很簡單,卻也是最知名、應用最為廣泛的減色演算法(Color quantization英语Color_quantization)。常見的影像處理軟體如Photoshop、GIMP...等,都使用了這個演算法或其變種。[1]

演算法

假如你有任意一張圖片,想要降低影像中的顏色數目到256色。

  1. 將圖片內的所有像素加入到同一個區域
  2. 對於所有的區域做以下的事:
    1. 計算此區域內所有像素的RGB三元素最大值與最小值的差。
    2. 選出相差最大的那個顏色(R或G或B)
    3. 根據那個顏色去排序此區域內所有像素
    4. 分割前一半與後一半的像素到二個不同的區域(這裡就是「中位切割」名字的由來)
  3. 重複第二步直到你有256個區域
  4. 將每個區域內的像素平均起來,於是你就得到了256色

演算法的限制

因為每次迴圈區域的數量都會加倍,所以最終產生的顏色數量必定為2的次方。假如有特殊需要其它數量的顏色時,可以先產生超過需求的顏色數量,再合併多餘的顏色直到符合所需的數量。

實作範例

ruby

class Color  attr_reader :R, :G, :B   def initialize(r = rand(256), g = rand(256), b = rand(256))  @R = r  @G = g  @B = b  end   def inspect  return "{R: #{@R}, G: #{@G}, B: #{@B}}"  end end  def get_colors(image_input, need_num)  regions = [image_input.flatten]   while regions.size < need_num  new_regions = []  regions.each do |region|  max_color = [:R, :G, :B].map do |color|  colors = region.map(&color)  next [colors.max - colors.min, color]  end.max[1]   new_order = region.sort_by(&max_color).reverse  middle = new_order.size / 2  new_regions.push(new_order[0, middle], new_order[middle, middle + 1])  end  regions = new_regions  end   return regions.map do |region|  Color.new(*[:R, :G, :B].map{|color| (region.map(&color).inject(&:+) / region.size.to_f).round })  end end  get_colors(Array.new(10){ Array.new(10){ Color.new } }, 16) # => [{ R: 187, G: 226, B: 184 }, { R: 185, G: 169, B: 189 }, ...] 

另見

引用

  1. ^ JavaScript图像处理(6) - 减色算法(Color Reduction) - Peihong's Infinite Dimensional Space - 博客频道 - CSDN.NET. blog.csdn.net. [2016-06-30]. (原始内容于2016-08-18). 

外部連結

中位切割演算法, 沒有或很少條目链入本條目, 2016年12月20日, 請根据格式指引, 在其他相關條目加入本條目的內部連結, 來建構維基百科內部網絡, median, 是paul, heckbert於1979年提出來的演算法, 概念上很簡單, 卻也是最知名, 應用最為廣泛的減色演算法, color, quantization, 英语, color, quantization, 常見的影像處理軟體如photoshop, gimp, 都使用了這個演算法或其變種, 目录, 演算法, 演算法的限制, 實作範例, ruby. 沒有或很少條目链入本條目 2016年12月20日 請根据格式指引 在其他相關條目加入本條目的內部連結 來建構維基百科內部網絡 中位切割演算法 Median cut 是Paul Heckbert於1979年提出來的演算法 概念上很簡單 卻也是最知名 應用最為廣泛的減色演算法 Color quantization 英语 Color quantization 常見的影像處理軟體如Photoshop GIMP 等 都使用了這個演算法或其變種 1 目录 1 演算法 1 1 演算法的限制 2 實作範例 2 1 ruby 3 另見 4 引用 5 外部連結演算法 编辑假如你有任意一張圖片 想要降低影像中的顏色數目到256色 將圖片內的所有像素加入到同一個區域 對於所有的區域做以下的事 計算此區域內所有像素的RGB三元素最大值與最小值的差 選出相差最大的那個顏色 R或G或B 根據那個顏色去排序此區域內所有像素 分割前一半與後一半的像素到二個不同的區域 這裡就是 中位切割 名字的由來 重複第二步直到你有256個區域 將每個區域內的像素平均起來 於是你就得到了256色演算法的限制 编辑 因為每次迴圈區域的數量都會加倍 所以最終產生的顏色數量必定為2的冪次方 假如有特殊需要其它數量的顏色時 可以先產生超過需求的顏色數量 再合併多餘的顏色直到符合所需的數量 實作範例 编辑ruby 编辑 class Color attr reader R G B def initialize r rand 256 g rand 256 b rand 256 R r G g B b end def inspect return R R G G B B end end def get colors image input need num regions image input flatten while regions size lt need num new regions regions each do region max color R G B map do color colors region map amp color next colors max colors min color end max 1 new order region sort by amp max color reverse middle new order size 2 new regions push new order 0 middle new order middle middle 1 end regions new regions end return regions map do region Color new R G B map color region map amp color inject amp region size to f round end end get colors Array new 10 Array new 10 Color new 16 gt R 187 G 226 B 184 R 185 G 169 B 189 另見 编辑k d樹引用 编辑 JavaScript图像处理 6 减色算法 Color Reduction Peihong s Infinite Dimensional Space 博客频道 CSDN NET blog csdn net 2016 06 30 原始内容存档于2016 08 18 外部連結 编辑Image quantization 页面存档备份 存于互联网档案馆 Median cut variations 页面存档备份 存于互联网档案馆 Image Pngslimmer Perl module at CPAN 页面存档备份 存于互联网档案馆 A Median Cut Algorithm for Light Probe Sampling 取自 https zh wikipedia org w index php title 中位切割演算法 amp oldid 68468596, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。