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

下载本文档

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

文档简介

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

2、ufferedImage src) 二 int width = src.getWidth();?int height = src.getHeight();B BufferedImage dest = createSubCompatibleDestImage(src,null );5int inPixels =new int width*height;6int ow = width/2;7int oh = height/2;Sint outPixels =new int ow*oh;第3页getRGB(src,0, 0, width, height, inPixels );int inRow=

3、0, inCol =0, index =0, oudex = 0, ta =0;float keneralData = this .getHVGaussianKeneral();for (int row= 0; row<oh; row+) for (intcol=0; col<ow; col+) inRow =2* row;inCol =2* col;if(inRow >= height)inRow =0;if (inCol >= width) inCol =0;float sumRed = 0, sumGreen =0, sumBlue =0;for (int sub

4、Row = -2; subRow <=2; subRow+) int inRowOff = inRow + subRow;if (inRowOff >= height | inRowOff <0) inRowOff =0;for (int subCol = -2; subCol <=2; subCol+) int inColOff = inCol + subCol;if (inColOff >= width | inColOff <0) inColOff =0;index = inRowOff * width + inColOff;ta = (inPixel

5、sindex >>24) & 0xff ;int red = (inPixelsindex >>16) & 0xff ;int green = (inPixelsindex >>8) & 0xff ;int blue = inPixelsindex &0xff ;sumRed += keneralDatasubRow +2subCol +sumGreen += keneralDatasubRow +2subCol +sumBlue += keneralDatasubRow +2subCol +oudex = row * ow

6、+ col;outPixelsoudex8) | clamp(sumBlue);setRGB( dest, return dest;=(ta << 24) | (clamp(sumRed) << 16) |0, 0, ow, oh, outPixels );public Bufferedlmage pyramidExpand(BufferedImage src) int width = src.getWidth();int height = src.getHeight();int 口 inPixels = new int width*height;getRGB(src,

7、 0, 0, width, height, inPixels );int ow = 2*width;int oh = 2*height;int 口 outPixels = new int ow * oh;int index =0, outdex =0, ta =0;2 * red;2 * green;2 * blue;(clamp(sumGreen)9101112 1314151617IS 19 2)2171 23 X 否 X 272S29303132 33 四3536 37 38<<39 省4142 43 期 有 书47 铝505152 53 54 55 免 575S 61 包

8、记8 6565 67 能为 71 H W 盟 75 峦 77 78 为 SDfloat keneralData =this .getHVGaussianKeneral();Bufferedlmage dest = createTwiceCompatibleDestImage(src,null );for (int row= 0; row<oh; row+) for (int col= 0; col<ow; col+) float sumRed = 0, sumGreen =0, sumBlue =0;for (int subRow = -2; subRow <=double

9、srcRow = (row + subRow)/ double j = Math.f100r(srcRow); double t = srcRow - j; if (t >0) continue ;if (srcRow >= height | srcRow < srcRow =0;for (int subCol = -2; subCol <=double srcColOff = (col + subCol)/ j = Math.f100r(srcColOff);t = srcColOff - j;if (t >0) continue ;if (srcColOff

10、>= width | srcColOff < srcColOff = index =( ta = (inPixelsindex >>int red = (inPixelsindex >> int green = (inPixelsindex >> int blue = inPixelsindex & sumRed += keneralDatasubRow + sumGreen += keneralDatasubRow + sumBlue += keneralDatasubRow +outdex = row * ow + col;outPi

11、xelsoutdex = (ta <<2; subRow+) 2.0 ;0) 2; subCol+) 2.0 ;0) 0;int )(srcRow * width + srcColOff);24) & 0xff ;16) & 0xff ;8) & 0xff ;0xff ;2subCol +2 * red;2subCol +2 * green;2subCol +2 * blue;24) | (clamp( 4.0f * sumRed) <<16) |(clamp( 4.0f * sumGreen) <<8) | clamp( 4.0f

12、* sumBlue);'.I/ outPixelsoutdex = (ta << 24) | (clamp(sumRed) << 16) |(clamp(sumGreen) << 8) | clamp(sumBlue);setRGB( dest, 0, 0, ow, oh, outPixels ); return dest;图像金字塔的reduce与expand过程都是卷积采样实现。特别注意的是expand操作不是reduce的可逆操作。关于什么是卷积,高斯滤波请参见博客上的其它相关文章。第7页85 £6 87 88<09192 免%

13、 於 笫97 翦 劣 1G01011G2 1CB 1G* 1651C61071CS 1C9 110111-1; 11211311411511611711S119高斯金字塔全部算法源代码如下:package com.gloomyfish.image.pyramid;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;public class PyramidAlgorithm extends GaussianFilter private float a;public PyramidAlgorithm() a =0

14、.4f;public void setParameter( float p) this .a = p;public BufferedImage口 pyramidDown(BufferedImage src,int level) BufferedImage口 imagePyramids =new BufferedImagelevel +1 ;imagePyramids 0 = src;for (int i= 1; i<imagePyramids.length; i+) imagePyramidsi = pyramidReduce(imagePyramidsi-1);return image

15、Pyramids;public BufferedImage口 pyramidUp(BufferedImage口 srcImage) BufferedImage imagePyramids =new BufferedImagesrcImage.length;for (int i= 0; i<srcImage.length; i+) imagePyramidsi = pyramidExpand(srcImagei);return imagePyramids;* l1 = g1 - expand(g2)* l2 = g2 - expand(g3)* l0 = g0 - expand(g1)*

16、param reducelmages* param expandlmages* return public BufferedImage getLaplacianPyramid(BufferedImagereducelmages) BufferedImage laplacilmages =new BufferedImagereduceImages.lengthfor (int i= 1; i<reduceImages.length; i+) BufferedImage expandlmage = pyramidExpand(reduceImagesi);laplaciImagesi-1 =

17、 createCompatibleDestImage(expandImage,null );int width = reduceImagesi- 1.getWidth();int height = reduceImagesi- 1 .getHeight();int ewidth = expandImage.getWidth();width = (width > ewidth) ? ewidth : width;height = (height > expandImage.getHeight() ?expandImage.getHeight():height;121System.ou

18、t.println("width="+ width + " expand width = " + ewidth);int reducePixels =newint width*height;int expandPixels = new int width*height;I' ';int laPixels = new int width*height;I' g getRGB( reduceImagesi-1, 0, 0, width, height, reducePixels);Ji getRGB( expandImage,0, 0

19、, width, height, expandPixels );:6int index =0;I' /int er =0, eg =0, eb =0;飞.for (int row= 0; row<height; row+) 0int ta =0, tr =0, tg =0, tb =0;面for (int col= 0; col<width; col+) 131index = row * width + col;132ta = (reducePixelsindex >>24) & 0xff133tr = (reducePixelsindex >&g

20、t;16) & 0xff1tg = (reducePixelsindex >>8) & 0xff135tb = reducePixelsindex &0xff ;126ta = (expandPixelsindex >>24) & 0xff137er = (expandPixelsindex >>16) & 0xff138eg = (expandPixelsindex >>8) & 0xff139eb = expandPixelsindex &0xff ;ttr = tr - er;HItg

21、 = tg - eg;,1 t -tb = tb - eb;HilaPixelsindex= (ta << 24) | (clamp(tr) << 16) | (clamp(tg) << 8) | clamp(tb);setRGB( laplaciImagesi-1, 0, 0, width, height, laPixels );,1T:return laplaciImages;private BufferedImage pyramidReduce(BufferedImage src) JL,int width = src.getWidth();null

22、);.l-Eint height = src.getHeight();BufferedImage dest = createSubCompatibleDestImage(src,.1" .,int inPixels = new int width*height;.Iint ow = width/ 2;I 乙int oh = height/ 2;:3int outPixels = new int ow*oh;getRGB(src, 0, 0, width, height, inPixels );int inRow= 0, inCol =0, index =0, oudex =0, ta

23、 =0;.二bfloat keneralData = this .getHVGaussianKeneral();157for (introw= 0; row<oh; row+) 1雏for(int col= 0; col<ow; col+) IS*inRow =2* row;ieoinCol =2* col;161if (inRow >= height) 1国inRow=0;应if (inCol >= width) 164inCol =:0;底float sumRed = 0, sumGreen =0, sumBlue =0;;:;、for (int subRow =

24、-2; subRow <=2; subRow+) :int inRowOff = inRow + subRow;:fif (inRowOff >= height | inRowOff <0) .1 :9inRowOff =0;,.Ifor (int subCol = -2; subCol <=2; subCol+) I /|int inColOff = inCol + subCol;J /if (inColOff >= width | inColOff <0) ,入inColOff =0;Iindex = inRowOff * width + inColOf

25、f;?-ta = (inPixelsindex >>24) & 0xff ;:飞int red = (inPixelsindex >>16) & 0xff ;int green = (inPixelsindex >>8) & 0xff ;int blue = inPixelsindex &0xff ;J 为sumRed += keneralDatasubRow +2subCol +2 * red;:sumGreen += keneralDatasubRow +2subCol +2 * green;.3.sumBlue += k

26、eneralDatasubRow +2subCol +2 * blue;oudex = row * ow + col;.1 二 8outPixelsoudex = (ta <<24) | (clamp(sumRed) <<16) |(clamp(sumGreen) <<8) | clamp(sumBlue);::s setRGB( dest,0, 0, ow, oh, outPixels );return dest;public Bufferedlmage createSubCompatibleDestImage(BufferedImage src, Col

27、orModel dstCM) 二,if ( dstCM = null )dstCM = src.getColorModel();return new BufferedImage(dstCM,dstCM.createCompatibleWritableRaster(src.getWidth()/2, src.getHeight()/ 2),dstCM.isAlphaPremultiplied(),null );,1;- p public Bufferedlmage createTwiceCompatibleDestImage(BufferedImage src, ColorModel dstCM

28、) ,l>.lif ( dstCM = null ),l> - dstCM = src.getColorModel();.IS";return new BufferedImage(dstCM,dstCM.createCompatibleWritableRaster(src.getWidth()*2, src.getHeight()* 2),dstCM.isAlphaPremultiplied(),null );public Bufferedlmage pyramidExpand(BufferedImage src) int width = src.getWidth();上

29、;。int height = src.getHeight();切int inPixels = new int width*height;上;二getRGB(src, 0, 0, width, height, inPixels );.十int ow = 2*width;int oh = 2*height;二int outPixels = new int ow * oh;犯intindex =0, outdex =0, ta =0;float keneralData =this .getHVGaussianKeneral();和Bufferedlmage dest = createTwiceCom

30、patibleDestImage(src,加for(int row= 0; row<oh; row+) 为6for (int col= 0; col<ow; col+) 加7float sumRed = 0, sumGreen =0, sumBlue =0;X®for (int subRow = -2; subRow <=2; subRow+) 2Pdouble srcRow = (row + subRow)/2.0 ;210double j = Math.floor(srcRow);211double t = srcRow - j;212if (t >0)

31、213continue ;214if (srcRow >= height | srcRow <0) 215srcRow =0;216for (int subCol = -2; subCol <=2; subCol+) 217double srcColOff = (col + subCol)/2.0 ;21Sj = Math.f100r(srcColOff);219t = srcColOff - j;220if (t >0) 221continue ;上上上if (srcColOff >= width | srcColOff <0) 23srcColOff =

32、0;224index = (int )(srcRow * width + srcColOff);加ta = (inPixelsindex >>24) & 0xff ;235int red = (inPixelsindex >>16) & 0xff ;227int green = (inPixelsindex >>8) & 0xff侬int blue = inPixelsindex &0xff ;sumRed += keneralDatasubRow +2subCol +加sumGreen += keneralDatasubRo

33、w +2subCol +231sumBlue += keneralDatasubRow +2subCol +null );2 * red;2 * green;2 * blue;J;;工outdex = row * ow + col;?outPixelsoutdex = (ta <<24) | (clamp( 4.0f * sumRed) <<16) |(clamp( 4.0f * sumGreen) <<8) | clamp( 4.0f * sumBlue);-M/ outPixelsoutdex = (ta << 24) | (clamp(su

34、mRed) << 16) |(clamp(sumGreen) << 8) | clamp(sumBlue);二;' setRGB( dest,0, 0, ow, oh, outPixels );二return dest;特别注意:我没有处理像素的宽与高,如果宽与高不是偶数可能会有问 题,使用时请自己处理吧。UI实现源代码如下:package com.gloomyfish.image.pyramid;加 238 239 Y) Xl 两 的*却 XS 缈 黝 251 妁 国 255 工6 方7 尊 的 251 检 的 侬 施 簸 W 翘 密 比 271 272 加2

35、75 2把 277 278 2内import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.MediaTracker;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.Bufferedlmage;import java.io.File;import java.io.IOExce

36、ption;import javax.imageio.ImageIO;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JPanel;ActionListener public class PyramidDemoUI extends JComponent implements private static final long serialVersionUID = 1L;priv

37、ateJButton upButton;privateJButton downButton;privateBufferedImage口 reduceImages;privateBufferedImage口 expandImages;private BufferedImage sourceImage;privateDimension mySize;privateMediaTracker tracker;public PyramidDemoUI(File f) initComponents(f);private void initComponents(File f)/ TODO Auto-gene

38、rated method stub try sourceImage = ImageIO.read(f); catch (IOException e1) e1.printStackTrace();tracker =new MediaTracker( this );tracker.addImage(sourceImage,1);/ blocked 10 seconds to load the image data try if (!tracker.waitForID(1, 10000 )System.out.println("Load error." );System.exit

39、(1);/ end if catch (InterruptedException e) e.p.e.printStackTrace(); -".ISystem.exit(1);.±、/ end catch总: JPanel btnPanel =new JPanel();二 btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT);upButton = new JButton( "Laplacian Pyramid" ); downButton = new JButton( "Pyramid Down

40、" );飞. upButton.addActionListener(this );downButton.addActionListener(this );飞:btnPanel.add(upButton);,btnPanel.add(downButton);. 9.1 mySize = new Dimension( 800, 800);XJFrame mainFrame =new JFrame( "Pyramid Demo - Gloomyfish" );mainFrame.getContentPane().setLayout(new BorderLayout();

41、:,-! mainFrame.getContentPane().add(this , BorderLayout.CENTER);工; mainFrame.getContentPane().add(btnPanel, BorderLayout.SOUTH);mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.pack(); 三个 mainFrame.setVisible(true );:了.Overridem. public Dimension getPreferredSize() .: ?.|returnmyS

42、ize;工.:OverrideV.protectedvoid paintComponent(Graphics g): / g.drawImage(sourceImage, 10, 10, sourceImage.getWidth(), sourceImage.getHeight(), null); int width =10;/ if(reduceImages != null) / for(int i=1; i<reduceImages.length; i+) width += (10 + reduceImagesi-1.getWidth();/g.drawlmage(reducelmagesi, width, 10,reduceImagesi.getWidth(

温馨提示

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

评论

0/150

提交评论