设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 3015|回复: 5
打印 上一主题 下一主题

[已经解决] 【悬赏10V】双线性插值 求优化【更新2.1 @2011-11-28】

[复制链接]

Lv2.观梦者

(?????)

梦石
0
星屑
700
在线时间
1327 小时
注册时间
2011-7-18
帖子
3184

贵宾

跳转到指定楼层
1
发表于 2011-11-25 05:35:38 | 显示全部楼层 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 各种压力的猫君 于 2011-11-29 01:32 编辑

悬赏视情况可以随便追加,就这样。

好吧我还在搞双线性插值 lol
这次应该是真正的双线性插值了。




【更新记录】
 2011-11-28 2.1
 继续优化算法,提高效率(相同情况下对比2.0加快0.2秒),
 加权平均的边缘变成了另一种奇怪的样子 = = b 原因不明。
 2011-11-25 2.0
 大幅优化了算法,提高效率(对比初版(点我)同样是128*128放大3倍,加快1秒)

【测试工程】
  双线性插值缩放2.1.zip (282.07 KB, 下载次数: 36)

【脚本】
  1. #==============================================================================
  2. # ■ 双线性插值 2.1 by 各种压力的猫君
  3. #------------------------------------------------------------------------------
  4. #  双线性插值图像缩放算法
  5. #   仅供测试,当Bitmap尺寸较大时效率很差暂时不具实用性。
  6. #------------------------------------------------------------------------------
  7. #   使用方法:
  8. #   Bilinear.new(bitmap, x_rate, y_rate, type)
  9. #     bitmap :原Bitmap
  10. #     x_rate :横轴放大率
  11. #     y_rate :纵轴放大率
  12. #     type   :缩放参数(1:加权平均 2:算术平均)
  13. #
  14. #   缩放后的Bitmap会被输出到全局变量$new_bitmap。
  15. #   缩放结束后可以在全局变量$bilinear_time获取处理全程花费的时间(秒)
  16. #------------------------------------------------------------------------------
  17. #   更新记录:
  18. #   2.1 2011-11-28 优化效率
  19. #   2.0 2011-11-25 测试版(别问我1.0哪去了)
  20. #==============================================================================

  21. class Bilinear
  22.   #--------------------------------------------------------------------------
  23.   # ● 初始化对像
  24.   #--------------------------------------------------------------------------
  25.   def initialize(bitmap, x_rate, y_rate, type = 1)
  26.     # 获取原Bitmap及宽高信息
  27.     @old_bitmap = bitmap
  28.     @old_width  = @old_bitmap.width
  29.     @old_height = @old_bitmap.height
  30.     # 生成一个储存所有像素颜色的数组
  31.     @color_array = []
  32.     # 每行从左到右的顺序依次获取颜色并压入数组
  33.     for line in 1..@old_height
  34.       for row in 1..@old_width
  35.         color = @old_bitmap.get_pixel(row, line)
  36.         @color_array.push color
  37.       end
  38.     end
  39.     # 获取放大率
  40.     @x_rate = x_rate
  41.     @y_rate = y_rate
  42.     # 根据放大率计算新Bitmap尺寸
  43.     @new_width  = @x_rate * @old_width
  44.     @new_height = @y_rate * @old_height
  45.     # 生成Bitmap
  46.     @new_bitmap = Bitmap.new(@new_width, @new_height)
  47.     # 绘制Bitmap
  48.     draw(type)
  49.     # 返回Bitmap
  50.     $new_bitmap = @new_bitmap.clone
  51.     # 消除临时Bitmap
  52.     @new_bitmap.dispose
  53.   end
  54.   #--------------------------------------------------------------------------
  55.   # ● 绘制Bitmap
  56.   #--------------------------------------------------------------------------
  57.   def draw(type)
  58.     # 记录目前时间,用于统计算法用时
  59.     @time_begin = Time.now
  60.     # 一行一行绘制
  61.     for line in 1..@new_height
  62.       # 过程1:获取本行通用浮点y坐标
  63.       do_math_1(line)
  64.       for row in 1..@new_width
  65.         # 过程2:获取浮点x坐标
  66.         do_math_2(row)
  67.         # 过程3:获取颜色
  68.         do_math_3
  69.         # 过程4:计算颜色
  70.         do_math_4(type)
  71.         # 绘制颜色
  72.         draw_color(row, line)
  73.       end
  74.     end
  75.     # 计算算法用时
  76.     $bilinear_time = Time.now - @time_begin
  77.   end
  78.   #--------------------------------------------------------------------------
  79.   # ● 计算(过程1:获取浮点y坐标及y权)
  80.   #--------------------------------------------------------------------------
  81.   def do_math_1(y)
  82.     # 获取新坐标对应在原图上的浮点坐标
  83.     @y = 100 * y / @y_rate
  84.     # 计算y权
  85.     @ya = @y % 100
  86.     @yb = 100 - @ya
  87.     # 获取底数(floor)
  88.     @yf = @y /= 100
  89.   end
  90.   #--------------------------------------------------------------------------
  91.   # ● 计算(过程2:获取浮点x坐标及x权)
  92.   #--------------------------------------------------------------------------
  93.   def do_math_2(x)
  94.     # 获取新坐标对应在原图上的浮点坐标
  95.     @x = 100 * x / @x_rate
  96.     # 计算x权
  97.     @xa = @x % 100
  98.     @xb = 100 - @xa
  99.     # 获取底数(floor)
  100.     @xf = @x /= 100
  101.   end
  102.   #--------------------------------------------------------------------------
  103.   # ● 计算(过程3:获取颜色)
  104.   #--------------------------------------------------------------------------
  105.   def do_math_3
  106.     # 计算坐标颜色储存在数组中的序列
  107.     index11 = @xf + (@yf - 1) * @old_width - 1
  108.     if @ya == 0
  109.       index21 = index11
  110.     else
  111.       index21 = index11 + @old_width
  112.     end
  113.     if @xa == 0
  114.       index12 = index11
  115.       index22 = index21
  116.     else
  117.       index12 = index11 + 1
  118.       index22 = index21 + 1
  119.     end
  120.     # 从数组中获取颜色
  121.     @color11 = @color_array[index11]
  122.     @color21 = @color_array[index21]
  123.     @color12 = @color_array[index12]
  124.     @color22 = @color_array[index22]
  125.   end
  126.   #--------------------------------------------------------------------------
  127.   # ● 计算(过程4:计算颜色)
  128.   #--------------------------------------------------------------------------
  129.   def do_math_4(type)
  130.     # 分解颜色
  131.     @c11 = decompose_color(@color11)
  132.     @c21 = decompose_color(@color21)
  133.     @c12 = decompose_color(@color12)
  134.     @c22 = decompose_color(@color22)
  135.     if type == 1
  136.       # 加权
  137.       @c11 = weighted_average(@c11, @xa)
  138.       @c21 = weighted_average(@c21, @xa)
  139.       @c12 = weighted_average(@c12, @xb)
  140.       @c22 = weighted_average(@c22, @xb)
  141.       # 计算新颜色
  142.       @r = ((@c11[0] + @c12[0]) * @ya + (@c21[0] + @c22[0]) * @yb) / 10000
  143.       @g = ((@c11[1] + @c12[1]) * @ya + (@c21[1] + @c22[1]) * @yb) / 10000
  144.       @b = ((@c11[2] + @c12[2]) * @ya + (@c21[2] + @c22[2]) * @yb) / 10000
  145.       @a = ((@c11[3] + @c12[3]) * @ya + (@c21[3] + @c22[3]) * @yb) / 10000
  146.     elsif type == 2
  147.       # 计算新颜色
  148.       @r = (@c11[0] + @c12[0] + @c21[0] + @c22[0]) / 4
  149.       @g = (@c11[1] + @c12[1] + @c21[1] + @c22[1]) / 4
  150.       @b = (@c11[2] + @c12[2] + @c21[2] + @c22[2]) / 4
  151.       @a = (@c11[3] + @c12[3] + @c21[3] + @c22[3]) / 4
  152.     end
  153.   end
  154.   #--------------------------------------------------------------------------
  155.   # ○ 分解颜色
  156.   #--------------------------------------------------------------------------
  157.   def decompose_color(color)
  158.     r = color.red
  159.     g = color.green
  160.     b = color.blue
  161.     a = color.alpha
  162.     color_array = [r, g, b, a]
  163.     return color_array
  164.   end
  165.   #--------------------------------------------------------------------------
  166.   # ○ 加权运算
  167.   #--------------------------------------------------------------------------
  168.   def weighted_average(array, d)
  169.     new_array = []
  170.     array.each do |i|
  171.       new_array.push i * d
  172.     end
  173.     return new_array
  174.   end
  175.   #--------------------------------------------------------------------------
  176.   # ● 绘制颜色
  177.   #--------------------------------------------------------------------------
  178.   def draw_color(x, y)
  179.     color = Color.new(@r, @g, @b, @a)
  180.     @new_bitmap.set_pixel(x, y, color)
  181.   end
  182. end
复制代码
为什么不让我换行!!!
【历史版本】(以下链接均链接到我的6R空间日志)
 2.0版 大幅优化效率
 1.0版(某个脑残算法版本,刚发布就删除了,尸首已经找不到了)
 初版 最初的思路

【悬赏解决以下问题】
 ①效率再提高
 ②采用加权平均时图像边缘的锯齿
 ③调用方法能否改成类似@new_bitmap = Bilinear.new(@bitmap, 3, 3, 2) 这样

多多益善,就算效果不是非常好也有经验奖励,
VIP悬赏无上限追加{:nm_4:}(当然不能超过我财力范围 {:nm_7:} )

Lv2.观梦者

(?????)

梦石
0
星屑
700
在线时间
1327 小时
注册时间
2011-7-18
帖子
3184

贵宾

2
 楼主| 发表于 2011-11-26 04:16:24 | 显示全部楼层
fux2 发表于 2011-11-25 09:20
第三个问题把这玩意加个bitmap爸爸然后在自己身上画应该可以,前两个待我想想。 ...

求具体思路 = = b
直接把Bitmap指定为父类用return @new_bitmap无效啊
回复

使用道具 举报

Lv2.观梦者

(?????)

梦石
0
星屑
700
在线时间
1327 小时
注册时间
2011-7-18
帖子
3184

贵宾

3
 楼主| 发表于 2011-11-27 23:39:53 | 显示全部楼层
追加1V & 顶帖

又想到种优化方法……马上试试去

点评

benvisa的photozoomer用的算法几乎达到无损放大,不知道那算法效率如何  发表于 2011-12-31 08:13
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-21 13:09

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表