图像的高斯金字塔用java编写的_第1页
图像的高斯金字塔用java编写的_第2页
图像的高斯金字塔用java编写的_第3页
图像的高斯金字塔用java编写的_第4页
图像的高斯金字塔用java编写的_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一:图像金字塔基本操作对一张图像不断的模糊之后向下采样,得到不同分辨率的图像,同时每次得到的新的图像宽与高是原来图像的1/2, 最常见就是基于高斯的模糊之后采样,得到的一系列图像称为高斯金字塔。高斯金字塔不同(DoG)又称为拉普拉斯金字塔,其计算公式如下:L(i) = G(i) expand(G(i+1)第i层拉普拉斯金字塔是由第i层高斯金字塔减去第i+1层高斯金字塔expand之后得到。本文得到的DoG(Difference of Gaussian)结果如下:二:关键代码解析金字塔reduce操作实现代码如下:java view plaincopy1 private BufferedImag

2、e pyramidReduce(BufferedImage src) 2 int width = src.getWidth(); 3 int height = src.getHeight(); 4 BufferedImage dest = createSubCompatibleDestImage(src, null); 5 int inPixels = new intwidth*height; 6 int ow = width/2; 7 int oh = height/2; 8 int outPixels = new intow*oh; 9 getRGB(src, 0, 0, width, h

3、eight, inPixels ); 10 int inRow=0, inCol = 0, index = 0, oudex =0, ta = 0; 11 float keneralData = this.getHVGaussianKeneral(); 12 for(int row=0; row<oh; row+) 13 for(int col=0; col<ow; col+) 14 inRow = 2* row; 15 inCol = 2* col; 16 if(inRow >= height) 17 inRow = 0; 18 19 if(inCol >= widt

4、h) 20 inCol = 0; 21 22 float sumRed = 0, sumGreen = 0, sumBlue = 0; 23 for(int subRow = -2; subRow <= 2; subRow+) 24 int inRowOff = inRow + subRow; 25 if(inRowOff >= height | inRowOff < 0) 26 inRowOff = 0; 27 28 for(int subCol = -2; subCol <= 2; subCol+) 29 int inColOff = inCol + subCol;

5、 30 if(inColOff >= width | inColOff < 0) 31 inColOff = 0; 32 33 index = inRowOff * width + inColOff; 34 ta = (inPixelsindex >> 24) & 0xff; 35 int red = (inPixelsindex >> 16) & 0xff; 36 int green = (inPixelsindex >> 8) & 0xff; 37 int blue = inPixelsindex & 0xff

6、; 38 sumRed += keneralDatasubRow + 2subCol + 2 * red; 39 sumGreen += keneralDatasubRow + 2subCol + 2 * green; 40 sumBlue += keneralDatasubRow + 2subCol + 2 * blue; 41 42 43 44 oudex = row * ow + col; 45 outPixelsoudex = (ta << 24) | (clamp(sumRed) << 16) | (clamp(sumGreen) << 8) |

7、clamp(sumBlue); 46 47 48 setRGB( dest, 0, 0, ow, oh, outPixels ); 49 return dest; 50 金字塔expand实现代码如下:java view plaincopy51 public BufferedImage pyramidExpand(BufferedImage src) 52 int width = src.getWidth(); 53 int height = src.getHeight(); 54 int inPixels = new intwidth*height; 55 getRGB(src, 0, 0,

8、 width, height, inPixels ); 56 int ow = 2*width; 57 int oh =2*height; 58 int outPixels = new intow * oh; 59 int index = 0, outdex = 0, ta = 0; 60 float keneralData = this.getHVGaussianKeneral(); 61 BufferedImage dest = createTwiceCompatibleDestImage(src, null); 62 for(int row=0; row<oh; row+) 63

9、for(int col=0; col<ow; col+) 64 float sumRed = 0, sumGreen = 0, sumBlue = 0; 65 for(int subRow = -2; subRow <= 2; subRow+) 66 double srcRow = (row + subRow)/2.0; 67 double j = Math.floor(srcRow); 68 double t = srcRow - j; 69 if(t > 0) 70 continue; 71 72 if(srcRow >= height | srcRow <

10、0) 73 srcRow = 0; 74 75 for(int subCol = -2; subCol <= 2; subCol+) 76 double srcColOff = (col + subCol)/2.0; 77 j = Math.floor(srcColOff); 78 t = srcColOff - j; 79 if(t > 0) 80 continue; 81 82 if(srcColOff >= width | srcColOff < 0) 83 srcColOff = 0; 84 85 index = (int)(srcRow * width + s

11、rcColOff); 86 ta = (inPixelsindex >> 24) & 0xff; 87 int red = (inPixelsindex >> 16) & 0xff; 88 int green = (inPixelsindex >> 8) & 0xff; 89 int blue = inPixelsindex & 0xff; 90 sumRed += keneralDatasubRow + 2subCol + 2 * red; 91 sumGreen += keneralDatasubRow + 2subCol

12、 + 2 * green; 92 sumBlue += keneralDatasubRow + 2subCol + 2 * blue; 93 94 95 outdex = row * ow + col; 96 outPixelsoutdex = (ta << 24) | (clamp(4.0f * sumRed) << 16) | (clamp(4.0f * sumGreen) << 8) | clamp(4.0f * sumBlue); 97 / outPixelsoutdex = (ta << 24) | (clamp(sumRed) <

13、;< 16) | (clamp(sumGreen) << 8) | clamp(sumBlue); 98 99 100 setRGB( dest, 0, 0, ow, oh, outPixels ); 101 return dest; 102 图像金字塔的reduce与expand过程都是卷积采样实现。特别注意的是expand操作不是reduce的可逆操作。关于什么是卷积,高斯滤波请参见博客上的其它相关文章。高斯金字塔全部算法源代码如下:java view plaincopy103 package com.gloomyfish.image.pyramid; 104 105 i

14、mport java.awt.image.BufferedImage; 106 import java.awt.image.ColorModel; 107 108 public class PyramidAlgorithm extends GaussianFilter 109 private float a; 110 111 public PyramidAlgorithm() 112 a = 0.4f; 113 114 115 public void setParameter(float p) 116 this.a = p; 117 118 119 public BufferedImage p

15、yramidDown(BufferedImage src, int level) 120 BufferedImage imagePyramids = new BufferedImagelevel + 1; 121 imagePyramids0 = src; 122 for(int i=1; i<imagePyramids.length; i+) 123 imagePyramidsi = pyramidReduce(imagePyramidsi-1); 124 125 return imagePyramids; 126 127 128 public BufferedImage pyrami

16、dUp(BufferedImage srcImage) 129 BufferedImage imagePyramids = new BufferedImagesrcImage.length; 130 for(int i=0; i<srcImage.length; i+) 131 imagePyramidsi = pyramidExpand(srcImagei); 132 133 return imagePyramids; 134 135 136 /* 137 * l1 = g1 - expand(g2) 138 * l2 = g2 - expand(g3) 139 * l0 = g0 -

17、 expand(g1) 140 * param reduceImages 141 * param expandImages 142 * return 143 */ 144 public BufferedImage getLaplacianPyramid(BufferedImage reduceImages) 145 BufferedImage laplaciImages = new BufferedImagereduceImages.length -1; 146 for(int i=1; i<reduceImages.length; i+) 147 BufferedImage expan

18、dImage = pyramidExpand(reduceImagesi); 148 laplaciImagesi-1 = createCompatibleDestImage(expandImage, null); 149 int width = reduceImagesi-1.getWidth(); 150 int height = reduceImagesi-1.getHeight(); 151 152 int ewidth = expandImage.getWidth(); 153 width = (width > ewidth) ? ewidth : width; 154 hei

19、ght = (height > expandImage.getHeight() ? expandImage.getHeight():height; 155 System.out.println(" width = " + width + " expand width = " + ewidth); 156 157 int reducePixels = new intwidth*height; 158 int expandPixels = new intwidth*height; 159 int laPixels = new intwidth*heig

20、ht; 160 getRGB( reduceImagesi-1, 0, 0, width, height, reducePixels); 161 getRGB( expandImage, 0, 0, width, height, expandPixels ); 162 int index = 0; 163 int er = 0, eg = 0, eb = 0; 164 for(int row=0; row<height; row+) 165 int ta = 0, tr = 0, tg = 0, tb = 0; 166 for(int col=0; col<width; col+)

21、 167 index = row * width + col; 168 ta = (reducePixelsindex >> 24) & 0xff; 169 tr = (reducePixelsindex >> 16) & 0xff; 170 tg = (reducePixelsindex >> 8) & 0xff; 171 tb = reducePixelsindex & 0xff; 172 173 ta = (expandPixelsindex >> 24) & 0xff; 174 er = (expa

22、ndPixelsindex >> 16) & 0xff; 175 eg = (expandPixelsindex >> 8) & 0xff; 176 eb = expandPixelsindex & 0xff; 177 178 tr = tr - er; 179 tg = tg - eg; 180 tb = tb - eb; 181 182 laPixelsindex = (ta << 24) | (clamp(tr) << 16) | (clamp(tg) << 8) | clamp(tb); 183 184

23、 185 setRGB( laplaciImagesi-1, 0, 0, width, height, laPixels ); 186 187 188 return laplaciImages; 189 190 191 private BufferedImage pyramidReduce(BufferedImage src) 192 int width = src.getWidth(); 193 int height = src.getHeight(); 194 BufferedImage dest = createSubCompatibleDestImage(src, null); 195

24、 int inPixels = new intwidth*height; 196 int ow = width/2; 197 int oh = height/2; 198 int outPixels = new intow*oh; 199 getRGB(src, 0, 0, width, height, inPixels ); 200 int inRow=0, inCol = 0, index = 0, oudex =0, ta = 0; 201 float keneralData = this.getHVGaussianKeneral(); 202 for(int row=0; row<

25、;oh; row+) 203 for(int col=0; col<ow; col+) 204 inRow = 2* row; 205 inCol = 2* col; 206 if(inRow >= height) 207 inRow = 0; 208 209 if(inCol >= width) 210 inCol = 0; 211 212 float sumRed = 0, sumGreen = 0, sumBlue = 0; 213 for(int subRow = -2; subRow <= 2; subRow+) 214 int inRowOff = inRo

26、w + subRow; 215 if(inRowOff >= height | inRowOff < 0) 216 inRowOff = 0; 217 218 for(int subCol = -2; subCol <= 2; subCol+) 219 int inColOff = inCol + subCol; 220 if(inColOff >= width | inColOff < 0) 221 inColOff = 0; 222 223 index = inRowOff * width + inColOff; 224 ta = (inPixelsindex

27、 >> 24) & 0xff; 225 int red = (inPixelsindex >> 16) & 0xff; 226 int green = (inPixelsindex >> 8) & 0xff; 227 int blue = inPixelsindex & 0xff; 228 sumRed += keneralDatasubRow + 2subCol + 2 * red; 229 sumGreen += keneralDatasubRow + 2subCol + 2 * green; 230 sumBlue +=

28、 keneralDatasubRow + 2subCol + 2 * blue; 231 232 233 234 oudex = row * ow + col; 235 outPixelsoudex = (ta << 24) | (clamp(sumRed) << 16) | (clamp(sumGreen) << 8) | clamp(sumBlue); 236 237 238 setRGB( dest, 0, 0, ow, oh, outPixels ); 239 return dest; 240 241 242 public BufferedImage

29、 createSubCompatibleDestImage(BufferedImage src, ColorModel dstCM) 243 if ( dstCM = null ) 244 dstCM = src.getColorModel(); 245 return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth()/2, src.getHeight()/2), dstCM.isAlphaPremultiplied(), null); 246 247 248 public BufferedI

30、mage createTwiceCompatibleDestImage(BufferedImage src, ColorModel dstCM) 249 if ( dstCM = null ) 250 dstCM = src.getColorModel(); 251 return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth()*2, src.getHeight()*2), dstCM.isAlphaPremultiplied(), null); 252 253 254 public Buf

31、feredImage pyramidExpand(BufferedImage src) 255 int width = src.getWidth(); 256 int height = src.getHeight(); 257 int inPixels = new intwidth*height; 258 getRGB(src, 0, 0, width, height, inPixels ); 259 int ow = 2*width; 260 int oh =2*height; 261 int outPixels = new intow * oh; 262 int index = 0, ou

32、tdex = 0, ta = 0; 263 float keneralData = this.getHVGaussianKeneral(); 264 BufferedImage dest = createTwiceCompatibleDestImage(src, null); 265 for(int row=0; row<oh; row+) 266 for(int col=0; col<ow; col+) 267 float sumRed = 0, sumGreen = 0, sumBlue = 0; 268 for(int subRow = -2; subRow <= 2;

33、 subRow+) 269 double srcRow = (row + subRow)/2.0; 270 double j = Math.floor(srcRow); 271 double t = srcRow - j; 272 if(t > 0) 273 continue; 274 275 if(srcRow >= height | srcRow < 0) 276 srcRow = 0; 277 278 for(int subCol = -2; subCol <= 2; subCol+) 279 double srcColOff = (col + subCol)/2

34、.0; 280 j = Math.floor(srcColOff); 281 t = srcColOff - j; 282 if(t > 0) 283 continue; 284 285 if(srcColOff >= width | srcColOff < 0) 286 srcColOff = 0; 287 288 index = (int)(srcRow * width + srcColOff); 289 ta = (inPixelsindex >> 24) & 0xff; 290 int red = (inPixelsindex >> 1

35、6) & 0xff; 291 int green = (inPixelsindex >> 8) & 0xff; 292 int blue = inPixelsindex & 0xff; 293 sumRed += keneralDatasubRow + 2subCol + 2 * red; 294 sumGreen += keneralDatasubRow + 2subCol + 2 * green; 295 sumBlue += keneralDatasubRow + 2subCol + 2 * blue; 296 297 298 outdex = row

36、 * ow + col; 299 outPixelsoutdex = (ta << 24) | (clamp(4.0f * sumRed) << 16) | (clamp(4.0f * sumGreen) << 8) | clamp(4.0f * sumBlue); 300 / outPixelsoutdex = (ta << 24) | (clamp(sumRed) << 16) | (clamp(sumGreen) << 8) | clamp(sumBlue); 301 302 303 setRGB( dest, 0,

37、 0, ow, oh, outPixels ); 304 return dest; 305 306 307 特别注意:我没有处理像素的宽与高,如果宽与高不是偶数可能会有问题,使用时请自己处理吧。UI实现源代码如下:java view plaincopy308 package com.gloomyfish.image.pyramid; 309 310 import java.awt.BorderLayout; 311 import java.awt.Dimension; 312 import java.awt.FlowLayout; 313 import java.awt.Graphics; 3

38、14 import java.awt.MediaTracker; 315 import java.awt.event.ActionEvent; 316 import java.awt.event.ActionListener; 317 import java.awt.image.BufferedImage; 318 import java.io.File; 319 import java.io.IOException; 320 321 import javax.imageio.ImageIO; 322 import javax.swing.JButton; 323 import javax.s

39、wing.JComponent; 324 import javax.swing.JFileChooser; 325 import javax.swing.JFrame; 326 import javax.swing.JPanel; 327 328 public class PyramidDemoUI extends JComponent implements ActionListener 329 330 /* 331 * 332 */ 333 private static final long serialVersionUID = 1L; 334 private JButton upButto

40、n; 335 private JButton downButton; 336 private BufferedImage reduceImages; 337 private BufferedImage expandImages; 338 private BufferedImage sourceImage; 339 private Dimension mySize; 340 private MediaTracker tracker; 341 342 public PyramidDemoUI(File f) 343 344 initComponents(f); 345 346 347 privat

41、e void initComponents(File f) 348 349 / TODO Auto-generated method stub 350 try 351 sourceImage = ImageIO.read(f); 352 catch (IOException e1) 353 e1.printStackTrace(); 354 355 356 tracker = new MediaTracker(this); 357 tracker.addImage(sourceImage, 1); 358 359 / blocked 10 seconds to load the image d

42、ata 360 try 361 if (!tracker.waitForID(1, 10000) 362 System.out.println("Load error."); 363 System.exit(1); 364 / end if 365 catch (InterruptedException e) 366 e.printStackTrace(); 367 System.exit(1); 368 / end catch 369 370 JPanel btnPanel = new JPanel(); 371 btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT); 372 upButton = new JButton("Laplacian Pyramid"); 373 downButton = new JButton("

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论