Android影像處理系統1.4映像的銳利化-邊緣檢測

來源:互聯網
上載者:User

Android影像處理系統1.4映像的銳利化-邊緣檢測
映像的銳利化-邊緣檢測:(Robert Gradient、Sobel Gradient、Laplace Gradient)
@author:鄭海波
相關部落格:
1.http://blog.csdn.net/nuptboyzhb/article/details/7925994
2.http://blog.csdn.net/nuptboyzhb/article/details/7926407
1.      Robert Gradient
[圖1]

2.      Sobel Gradient
[圖2]

3.      Laplace Gradient
[圖3]

關鍵的Java代碼

 
[java] 
/*
     * Robert運算元梯度
     * 
     */ 
    public Bitmap RobertGradient(Bitmap myBitmap){ 
        // Create new array 
        int width = myBitmap.getWidth(); 
        int height = myBitmap.getHeight(); 
        int[] pix = new int[width * height]; 
        myBitmap.getPixels(pix, 0, width, 0, 0, width, height); 
        Matrix dataR=getDataR(pix, width, height); 
        Matrix dataG=getDataG(pix, width, height); 
        Matrix dataB=getDataB(pix, width, height); 
        //Matrix dataGray=getDataGray(pix, width, height); 
        ///////////////////////////////////////////////////////// 
        dataR=eachRobertGradient(dataR,width,height); 
        dataG=eachRobertGradient(dataG,width,height); 
        dataB=eachRobertGradient(dataB,width,height); 
        /////////////////////////////////////////////////////////////// 
        // Change bitmap to use new array 
        Bitmap bitmap=makeToBitmap(dataR, dataG, dataB, width, height);  
        myBitmap = null; 
        pix = null; 
        return bitmap; 
    } 
    private Matrix eachRobertGradient(Matrix tempM,int width,int height){ 
        int i,j; 
        for(i=0;i<width-1;i++){ 
            for(j=0;j<height-1;j++){ 
                int temp=Math.abs((int)tempM.get(i, j)-(int)tempM.get(i,j+1)) 
                         +Math.abs((int)tempM.get(i+1,j)-(int)tempM.get(i,j+1)); 
                tempM.set(i, j, temp); 
            } 
        } 
        return tempM; 
    } 
    /*
     *Sobel運算元銳利化 
     */ 
    public Bitmap SobelGradient(Bitmap myBitmap){ 
        // Create new array 
        int width = myBitmap.getWidth(); 
        int height = myBitmap.getHeight(); 
        int[] pix = new int[width * height]; 
        myBitmap.getPixels(pix, 0, width, 0, 0, width, height); 
        Matrix dataR=getDataR(pix, width, height); 
        Matrix dataG=getDataG(pix, width, height); 
        Matrix dataB=getDataB(pix, width, height); 
        Matrix dataGray=getDataGray(pix, width, height); 
        ///////////////////////////////////////////////////////// 
        dataGray=eachSobelGradient(dataGray, width, height); 
        dataR=dataGray.copy(); 
        dataG=dataGray.copy(); 
        dataB=dataGray.copy(); 
        /////////////////////////////////////////////////////////////// 
        // Change bitmap to use new array 
        Bitmap bitmap=makeToBitmap(dataR, dataG, dataB, width, height);  
        myBitmap = null; 
        pix = null; 
        return bitmap; 
    } 
    private Matrix eachSobelGradient(Matrix tempM,int width,int height){ 
        int i,j; 
        Matrix resultMatrix=tempM.copy(); 
        for(i=1;i<width-1;i++){ 
            for(j=1;j<height-1;j++){ 
                int temp1=Math.abs(((int)tempM.get(i+1, j-1)+2*(int)tempM.get(i+1, j)+(int)tempM.get(i+1,j+1)) 
                         -(((int)tempM.get(i-1,j-1)+2*(int)tempM.get(i-1,j)+(int)tempM.get(i-1,j-1)))); 
                int temp2=Math.abs(((int)tempM.get(i-1, j+1)+2*(int)tempM.get(i, j+1)+(int)tempM.get(i+1,j+1)) 
                         -(((int)tempM.get(i-1,j-1)+2*(int)tempM.get(i,j-1)+(int)tempM.get(i+1,j-1)))); 
                int temp=temp1+temp2; 
                resultMatrix.set(i, j, temp); 
            } 
        } 
        return resultMatrix; 
    } 
    /*
     *Laplace 銳利化 
     */ 
    public Bitmap LaplaceGradient(Bitmap myBitmap){ 
        // Create new array 
        int width = myBitmap.getWidth(); 
        int height = myBitmap.getHeight(); 
        int[] pix = new int[width * height]; 
        myBitmap.getPixels(pix, 0, width, 0, 0, width, height); 
        Matrix dataR=getDataR(pix, width, height); 
        Matrix dataG=getDataG(pix, width, height); 
        Matrix dataB=getDataB(pix, width, height); 
        Matrix dataGray=getDataGray(pix, width, height); 
        ///////////////////////////////////////////////////////// 
        dataGray=eachLaplaceGradient(dataGray,width,height); 
        dataR=dataGray.copy(); 
        dataG=dataGray.copy(); 
        dataB=dataGray.copy(); 
        /////////////////////////////////////////////////////////////// 
        // Change bitmap to use new array 
        Bitmap bitmap=makeToBitmap(dataR, dataG, dataB, width, height);  
        myBitmap = null; 
        pix = null; 
        return bitmap; 
    } 
    private Matrix eachLaplaceGradient(Matrix tempM,int width,int height){ 
        int i,j; 
        Matrix resultMatrix=tempM.copy(); 
        for(i=1;i<width-1;i++){ 
            for(j=1;j<height-1;j++){ 
                int temp=Math.abs(5*(int)tempM.get(i, j)-(int)tempM.get(i+1,j) 
                        -(int)tempM.get(i-1,j)-(int)tempM.get(i,j+1)-(int)tempM.get(i,j-1)); 
                resultMatrix.set(i, j, temp); 
            } 
        } 
        return resultMatrix; 
    } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.