• 图像处理--------应用卷积– 轧花与边缘检测 分类: 视频图像处理 2015-07-24 09:50 24人阅读 评论(0) 收藏


    一:轧花

    轧花算子(embossfilter)

    对一幅数字图像一阶微分结果即可得到轧花效果,根据不同的算子,轧花又

    可以分为凹效果与凸效果两种。两个个最简单的轧花算子为:


    轧花算子又称为双极性算子,1对图像的贡献意味着平滑,-1对图像的贡献

    意味着突出细节,于是最终就得出了双极性的轧花效果。

     

    处理过程:

    a.      读取图像像素

    b.      使用轧花算子完成对像素数组的卷积操作

    c.      整体亮度提升效果– 高斯亮度/基于阈值/直接常量提升

     

    轧花滤镜效果:左边为原图, 右边为轧花处理以后效果

     

    二:边缘提取

    Edge detection是图像处理中非常重要而且也是十分常用的图像处理手段之一,边缘提取是

    图像二值化的基本步骤之一。边缘提取从本质上来说是高通滤波,从数字信号的角度看,就

    是要保留高频信号,去掉低频信号,因此边缘提取有很多频率域算子,将图像完成FFT之后

    在频率域完成高通滤波再转到空间域。显然计算量比较大,空间域最经典的边缘提取算法之

    一Candy Edge Detection有着非常好的效果。

     

    这里只是抛砖引玉,完成一个最简单基于卷积的空间域边缘提取算子,算子为:

     

    完成卷积以后的效果如下:


    对于灰度图完成边缘提取以后效果如下:

     

    基于卷积还可以完成图像的锐化(Sharp Filter),让图像上的差异更加明显。

    一个简单的Sharp Filter可以为

     

    得到的效果如下:

     

    完成轧花卷积的代码如下:

    1. @Override  
    2. public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
    3.     int width = src.getWidth();  
    4.        int height = src.getHeight();  
    5.   
    6.        if ( dest == null )  
    7.            dest = createCompatibleDestImage( src, null );  
    8.   
    9.        int[] inPixels = new int[width*height];  
    10.        int[] outPixels = new int[width*height];  
    11.        src.getRGB( 00, width, height, inPixels, 0, width );  
    12.     int index = 0;  
    13.     int index2 = 0;  
    14.     int r=0, g=0, b=0;  
    15.     for ( int y = 0; y < height; y++ ) {  
    16.         for ( int x = 0; x < width; x++ ) {  
    17.             int ta = 255, tr = 0, tg = 0, tb = 0;  
    18.             for(int fr = 0; fr < filterRow; fr++) {  
    19.                 int rowoffset = y + fr;  
    20.                 if(rowoffset < 0 || rowoffset >=height) {  
    21.                     rowoffset = y;  
    22.                 }  
    23.                 for(int fc = 0; fc < filterCol; fc++) {  
    24.                     int coloffset = fc + x;  
    25.                     if(coloffset < 0 || coloffset >= width) {  
    26.                         coloffset = x;  
    27.                     }  
    28.                     index2 = rowoffset * width + coloffset;  
    29.                     int rgb1 = inPixels[index2];  
    30.                     int r1 = (rgb1 >> 16) & 0xff;  
    31.                     int g1 = (rgb1 >> 8) & 0xff;  
    32.                     int b1 = rgb1 & 0xff;  
    33.                     if(isOUT) {  
    34.                         tr += r1 * outfilter[fr][fc];  
    35.                         tg += g1 * outfilter[fr][fc];  
    36.                         tb += b1 * outfilter[fr][fc];  
    37.                     } else {  
    38.                         tr += r1 * infilter[fr][fc];  
    39.                         tg += g1 * infilter[fr][fc];  
    40.                         tb += b1 * infilter[fr][fc];  
    41.                     }  
    42.                 }  
    43.             }  
    44.               
    45.             tr += COLORCONSTANTS;  
    46.             tg += COLORCONSTANTS;  
    47.             tb += COLORCONSTANTS;  
    48.             r = PixelUtils.clamp(tr);  
    49.             g = PixelUtils.clamp(tg);  
    50.             b = PixelUtils.clamp(tb);  
    51.             outPixels[index] = (ta << 24) | (r << 16) | (g << 8) | b;  
    52.             index++;  
    53.         }  
    54.     }  
    55.        dest.setRGB( 00, width, height, outPixels, 0, width );  
    56.        return dest;  
    57. }  

    完成简单边缘检测的代码如下:

    1. private void filter(int[] inPixels, int[] outPixels, int height, int width, double[][] filterKernel) {  
    2.     int index = 0;  
    3.     int index2 = 0;  
    4.     int r=0, g=0, b=0;  
    5.     int semiColumn = filterKernel.length/2;  
    6.     int semiRow = filterKernel[0].length/2;  
    7.     for ( int y = 0; y < height; y++ ) {  
    8.         for ( int x = 0; x < width; x++ ) {  
    9.             int ta = 255, tr = 0, tg = 0, tb = 0;  
    10.             for(int fr = -semiRow; fr <= semiRow; fr++) {  
    11.                 int rowoffset = y + fr;  
    12.                 if(rowoffset < 0 || rowoffset >=height) {  
    13.                     rowoffset = y;  
    14.                 }  
    15.                 for(int fc = -semiColumn; fc <= semiColumn; fc++) {  
    16.                     int coloffset = fc + x;  
    17.                     if(coloffset < 0 || coloffset >= width) {  
    18.                         coloffset = x;  
    19.                     }  
    20.                     index2 = rowoffset * width + coloffset;  
    21.                     int rgb1 = inPixels[index2];  
    22.                     int r1 = (rgb1 >> 16) & 0xff;  
    23.                     int g1 = (rgb1 >> 8) & 0xff;  
    24.                     int b1 = rgb1 & 0xff;  
    25.                     tr += ((double)r1 * filterKernel[fr + semiRow][fc + semiColumn]);  
    26.                     tg += ((double)g1 * filterKernel[fr + semiRow][fc + semiColumn]);  
    27.                     tb += ((double)b1 * filterKernel[fr + semiRow][fc + semiColumn]);  
    28.                 }  
    29.             }  
    30.               
    31.             if(enhanceBrightness) {  
    32.                 tr += COLORCONSTANTS;  
    33.                 tg += COLORCONSTANTS;  
    34.                 tb += COLORCONSTANTS;  
    35.             }  
    36.             r = PixelUtils.clamp(tr);  
    37.             g = PixelUtils.clamp(tg);  
    38.             b = PixelUtils.clamp(tb);  
    39.             outPixels[index] = (ta << 24) | (r << 16) | (g << 8) | b;  
    40.             index++;  
    41.         }  
    42.     }  
    43. }  

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)
    LeetCode 349. Intersection of Two Arrays (两个数组的相交)
    LeetCode 290. Word Pattern (词语模式)
    LeetCode 266. Palindrome Permutation (回文排列)$
    34.Search for a Range
    spark连接mongodb
    NLPIR中文分词器的使用
    scala和maven整合实践
    Spark中的键值对操作-scala
    301.Remove Invalid Parentheses
  • 原文地址:https://www.cnblogs.com/mao0504/p/4706369.html
Copyright © 2020-2023  润新知