圖像的高斯金字塔,用java編寫的-10頁(yè)文檔資料_第1頁(yè)
圖像的高斯金字塔,用java編寫的-10頁(yè)文檔資料_第2頁(yè)
圖像的高斯金字塔,用java編寫的-10頁(yè)文檔資料_第3頁(yè)
圖像的高斯金字塔,用java編寫的-10頁(yè)文檔資料_第4頁(yè)
圖像的高斯金字塔,用java編寫的-10頁(yè)文檔資料_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、一:圖像金字塔基本操作對(duì)一張圖像不斷的模糊之后向下采樣,得到不同分辨率的圖像,同時(shí)每次得到的新的圖像寬與高是原來(lái)圖像的1/2,最常見就是基于高斯的模糊之后采 樣,得到的一系列圖像稱為高斯金字塔。高斯金字塔不同(DoG)又稱為拉普拉斯金字塔,其計(jì)算公式如下:L(i) = G(i) - expand(G(i+1)第i層拉普拉斯金字塔是由第i層高斯金字塔減去第i+1層高斯金字塔 expand之后得到。本文得到的DoG(Difference of Gaussian) 結(jié)果如下:二:關(guān)鍵代碼解析金字塔reduce操作實(shí)現(xiàn)代碼如下: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頁(yè)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過(guò)程都是卷積采樣實(shí)現(xiàn)。特別注意的是expand操作不是reduce的可逆操作。關(guān)于什么是卷積,高斯濾波請(qǐng)參見博客上的其它相關(guān)文章。第7頁(yè)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國(guó)inRow=0;應(yīng)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;特別注意:我沒(méi)有處理像素的寬與高,如果寬與高不是偶數(shù)可能會(huì)有問(wèn) 題,使用時(shí)請(qǐng)自己處理吧。UI實(shí)現(xiàn)源代碼如下:package com.gloomyfish.image.pyramid;加 238 239 Y) Xl 兩 的*卻 XS 緲 黝 251 妁 國(guó) 255 工6 方7 尊 的 251 檢 的 儂 施 簸 W 翹 密 比 271 272 加2

35、75 2把 277 278 2內(nèi)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(); 三個(gè) 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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論