• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            coreBugZJ

            此 blog 已棄。

            數字圖像處理上機之五:圖像平滑和銳化


            在圖像中,通過鄰接點的相互平均可以去掉一些突然變化的點,從而過濾掉一定的噪聲,達到平滑的目的,但圖像有一定程度上的模糊。
            本實驗使用模板
                    1  1  1
                    1  1  1
                    1  1  1
            即每一個像素的灰度都是自身和周圍8個像素的平均值。


            圖像銳化處理的目的是使模糊的圖像變得更加清晰起來,而圖像模糊的實質就是圖像受到平均或積分運算造成的,因此可以對圖像進行逆運算(如微分)來使圖像清晰化。梯度銳化是一種常用的微分銳化方法。


            Sobel 初步嘗試,其卷積計算核有兩個,

            Sx =
                    -1  0  1
                    -2  0  2
                    -1  0  1

            Sy =
                    1   2   1
                    0   0   0
                   -1  -2  -1

            一個對垂直邊緣影響最大,一個對水平邊緣影響最大。圖像中每點均分別用這兩個算子作卷積,取兩個卷積絕對值的和。


            圖像中的邊緣及急劇變化部分與圖像高頻分量有關,利用低通濾波減弱高頻部分能實現圖像平滑,利用高通濾波減弱低頻部分能實現圖像銳化。


            圖像FFT 后注意原點。










            原圖



            FFT 后



            FFT 后高通濾波



            高通濾波



            FFT 后低通濾波



            低通濾波



            平滑



            Sobel






             

            /*
            ProcessFilterZ.h

            Copyright (C) 2011, coreBugZJ, all rights reserved.

            濾波。
            */


            #ifndef  __PROCESSFILTER_Z_H_INCLUDED__
            #define  __PROCESSFILTER_Z_H_INCLUDED__


            #include "TypeZ.h"
            #include "ClassImageZ.h"


                    /* 高通 低通 濾波器 */
                    /* 對寬為 (1<<lgw) 高為 (1<<lgh) 且 2d FFT 后的矩陣 */
                    /* 點的定位同 2d FFT */
                    /* 會修改矩陣 */
                    /* if 參數highPass then 高通 else 低通 */
                    /* == radius 也通過 */
                    /* 成功返回 ROK */
            PublicFuncZ  R32  highLowPassFilterZ( CF64 *mat, U32 lgw, U32 lgh, F64 radius, B32 highPass );
                    /* 在圖像上使用 n * n 的模板 mat,然后 乘以 mulFactor 除以 divFactor */
                    /* 假設 n 為奇數 */
                    /* 會修改圖像 */
                    /* 模板 mat 中第 y 行第 x 列的點(x,y)為 mat[ y * n + x ] */
                    /* 圖像邊緣無法使用模板處,保持不變 */
                    /* 成功返回 ROK */
            PublicFuncZ  R32  templateFilterZ( ImageZ img, I32 *mat, U32 n, I32 mulFactor, I32 divFactor );


                    /* 平滑 */
                    /* 會修改圖像 */
                    /* 成功返回 ROK */
            PublicFuncZ  R32  smoothImageZ( ImageZ img );
                    /* 銳化 sobel */
                    /* 會修改圖像 */
                    /* 成功返回 ROK */
            PublicFuncZ  R32  sharpImageZ( ImageZ img );


            #endif /* __PROCESSFILTER_Z_H_INCLUDED__ */








             

            /*
            ProcessFilterZ.c

            Copyright (C) 2011, coreBugZJ, all rights reserved.

            濾波。
            */


            #include "stdafx.h"
            #include "ProcessFilterZ.h"
            #include "ProcessGrayZ.h"

            #include <malloc.h>


            PublicFuncZ  R32  highLowPassFilterZ( CF64 *mat, U32 lgw, U32 lgh, F64 radius, B32 highPass ) {
                    U32  x, y, w, h, xc, yc;
                    F64  r2m4, d2m4, f0, f1;
                    I32  cmp;

                    if ( (NULL == mat) ) {
                            return RERR;
                    }

                    w = PWR2_U32_Z( lgw );
                    h = PWR2_U32_Z( lgh );
                    MUL_F64_Z( r2m4, radius, radius );
                    MUL_F64_U32_Z( r2m4, r2m4, 4 );

                    for ( y = 0; y < h; ++y ) {
                            yc = ( y + (h>>1) ) % h;
                            for ( x = 0; x < w; ++x ) {
                                    xc = ( x + (w>>1) ) % w;

                                    MOV_F64_U32_Z( f0, xc );
                                    ADD_F64_U32_Z( f0, f0, xc );
                                    SUB_F64_U32_Z( f0, f0, w );
                                    MUL_F64_Z( f0, f0, f0 );

                                    MOV_F64_U32_Z( f1, yc );
                                    ADD_F64_U32_Z( f1, f1, yc );
                                    SUB_F64_U32_Z( f1, f1, h );
                                    MUL_F64_Z( f1, f1, f1 );

                                    ADD_F64_Z( d2m4, f0, f1 );

                                    CMP_F64_Z( cmp, d2m4, r2m4 );
                                    if ( highPass ) {
                                            if ( cmp < 0 ) {
                                                    MOV_CF64_U32_Z( mat[ y * w + x ], 0, 0 );
                                            }
                                    }
                                    else {
                                            if ( cmp > 0 ) {
                                                    MOV_CF64_U32_Z( mat[ y * w + x ], 0, 0 );
                                            }
                                    }
                            }
                    }

                    return ROK;
            }


            PublicFuncZ  R32  templateFilterZ( ImageZ img, I32 *mat, U32 n, I32 mulFactor, I32 divFactor ) {
                    U32  w, h, x, y, ix, iy, nha;
                    I32  pix, *ptrmat;
                    U08  *ptrimg, *ptrpix;

                    if ( (!isImageValidZ(img)) || (NULL == mat) || (0 == divFactor) || (3 > n) ) {
                            return RERR;
                    }

                    if ( GRAY_NUM_Z != img->colorNum ) {
                            return RERR; /* 目前只支持 256 灰度 */
                    }

                    nha = n / 2 + 1;
                    w   = img->width;
                    h   = img->height;

                    for ( y = n; y <= h; ++y ) {
                            ptrpix = img->pPixel + (y - nha) * img->linePitch + (n - nha);
                            for ( x = n; x <= w; ++x ) {
                                    pix = 0;
                                    ptrmat = mat;
                                    for ( iy = y-n; iy < y; ++iy ) {
                                            ptrimg = img->pPixel + iy * img->linePitch + (x - n);
                                            for ( ix = x-n; ix < x; ++ix ) {
                                                    pix += (*ptrimg) * (*ptrmat);
                                                    ++ptrimg;
                                                    ++ptrmat;
                                            }
                                    }
                                    pix = pix * mulFactor / divFactor;
                                    if ( pix >= GRAY_NUM_Z ) {
                                            pix = GRAY_NUM_Z - 1;
                                    }
                                    *ptrpix = (U08)(pix);
                                    ++ptrpix;
                            }
                    }

                    return ROK;
            }


            PublicFuncZ  R32  smoothImageZ( ImageZ img ) {
                    I32 mat[] = {
                            1, 1, 1,
                            1, 1, 1,
                            1, 1, 1
                    };
                    return templateFilterZ( img, mat, 3, 1, 9 );
            }

            PublicFuncZ  R32  sharpImageZ( ImageZ img ) {
                    I32    *mat = NULL;
                    U32    width, height, x, y;
                    I32    v0, v1;
                    R32    res;

                    if ( !isImageValidZ(img) ) {
                            return RERR;
                    }

                    if ( GRAY_NUM_Z != img->colorNum ) {
                            return RERR; /* 256 級灰度 */
                    }

                    width  = img->width;
                    height = img->height;

                    mat = (I32*)malloc( width * height * sizeof(I32) );
                    if ( NULL == mat ) {
                            return RERR;
                    }

            #define  PIX(ix,iy)  ((I32)(img->pPixel[(iy) * img->linePitch + (ix)]))
                    for ( y = 0; y < height; ++y ) {
                            for ( x = 0; x < width; ++x ) {
                                    v0 = ( PIX(x+1, y-1) + 2*PIX(x+1, y) + PIX(x+1, y+1) ) -
                                         ( PIX(x-1, y-1) + 2*PIX(x-1, y) + PIX(x-1, y+1) );
                                    if ( 0 > v0 ) v0 = -v0;

                                    v1 = ( PIX(x-1, y+1) + 2*PIX(x, y+1) + PIX(x+1, y+1) ) -
                                         ( PIX(x-1, y+1) + 2*PIX(x, y+1) + PIX(x+1, y-1) );
                                    if ( 0 > v1 ) v1 = -v1;

                                    mat[ y * width + x ] = v0 + v1;
                            }
                    }
            #undef  PIX

                    res = scaleImageZ( img, mat, width, height );

                    free( mat );
                    mat = NULL;

                    return res;
            }





             

            PublicFuncZ  ImageZ  createImageScaleZ( I32 *mat, U32 width, U32 height ) {
                    ImageZ  img = NULL;

                    if ( (NULL == mat) || (1 > width) || (1 > height) ) {
                            return NULL;
                    }

                    img = createImageZ( width, height, GRAY_NUM_Z );
                    if ( NULL == img ) {
                            return NULL;
                    }

                    if ( ROK == scaleImageZ(img, mat, width, height) ) {
                            return img;
                    }

                    destroyImageZ( img );
                    img = NULL;

                    return NULL;
            }

            PublicFuncZ  R32  scaleImageZ( ImageZ img, I32 *mat, U32 width, U32 height ) {
                    U32     x, y, i;
                    I32     matMin, matMax, mv;

                    if ( (NULL == mat) || (1 > width) || (1 > height) ||
                         (!isImageValidZ(img)) || (GRAY_NUM_Z != img->colorNum) ||
                         (width != img->width) || (height != img->height)
                       ) {
                            return RERR;
                    }

                    for ( i = 0; i < GRAY_NUM_Z; ++i ) {
                            img->pPalette[ i * IMAGEZ_COLOR_SIZE_Z + IMAGEZ_OFFSET_BLUE_Z  ] =
                            img->pPalette[ i * IMAGEZ_COLOR_SIZE_Z + IMAGEZ_OFFSET_GREEN_Z ] =
                            img->pPalette[ i * IMAGEZ_COLOR_SIZE_Z + IMAGEZ_OFFSET_RED_Z   ] = (U08)(i);
                            img->pPalette[ i * IMAGEZ_COLOR_SIZE_Z + IMAGEZ_OFFSET_ALPHA_Z ] = 255;
                    }

                    matMin = matMax = mat[ 0 ];
                    for ( y = 0; y < height; ++y ) {
                            for ( x = 0; x < width; ++x ) {
                                    mv = mat[ y * width + x ];
                                    if ( matMin > mv ) {
                                            matMin = mv;
                                    }
                                    if ( matMax < mv ) {
                                            matMax = mv;
                                    }
                            }
                    }

                    mv = matMax - matMin;
                    if ( 0 == mv ) {
                            mv = 1;
                    }
                    for ( y = 0; y < height; ++y ) {
                            for ( x = 0; x < width; ++x ) {
                                    img->pPixel[ y * img->linePitch + x ] = (U08)(
                                            (mat[ y * width + x ] - matMin) * (GRAY_NUM_Z-1) / mv );
                            }
                    }

                    return ROK;
            }






             

            PublicFuncZ  ImageZ  createImageFromFftDataZ( CF64 *mat, U32 lgw, U32 lgh ) {
                    ImageZ  img = NULL;
                    U32     w, h, x, y, i, tu;
                    F64     tf255, tf;
                    U08     *ptrimg;
                    CF64    *ptrmat;

                    if ( NULL == mat ) {
                            return NULL;
                    }

                    w   = PWR2_U32_Z( lgw );
                    h   = PWR2_U32_Z( lgh );
                    img = createImageZ( w, h, GRAY_NUM_Z );
                    if ( NULL == img ) {
                            return NULL;
                    }

                    ptrimg = img->pPalette;
                    for ( i = 0; i < GRAY_NUM_Z; ++i ) {
                            ptrimg[ IMAGEZ_OFFSET_BLUE_Z  ] =
                                    ptrimg[ IMAGEZ_OFFSET_GREEN_Z ] =
                                    ptrimg[ IMAGEZ_OFFSET_RED_Z   ] = (U08)(i);
                            ptrimg[ IMAGEZ_OFFSET_ALPHA_Z ] = 255;
                            ptrimg += IMAGEZ_COLOR_SIZE_Z;
                    }

                    MOV_F64_U32_Z( tf255, 255 );
                    for ( y = 0; y < h; ++y ) {
                            ptrimg = img->pPixel + ((y+(h>>1))%h) * img->linePitch;
                            ptrmat = mat + y * w;
                            for ( x = 0; x < w; ++x ) {
                                    M_CF64_Z( tf, *ptrmat );
                                    ++ptrmat;
                                    DIV_F64_U32_Z( tf, tf, 100 );
                                    MIN_F64_Z( tf, tf, tf255 );
                                    MOV_U32_F64_Z( tu, tf );
                                    *(ptrimg + (x+(w>>1))%w ) = (U08)(tu);
                            }
                    }

                    return img;
            }

             


            posted on 2011-12-14 20:21 coreBugZJ 閱讀(3002) 評論(0)  編輯 收藏 引用 所屬分類: VideoImageAlgorithm課內作業

            久久久久久精品免费看SSS | 久久国产精品免费| 亚洲精品WWW久久久久久| 性欧美大战久久久久久久久 | 综合久久给合久久狠狠狠97色| 国产精品99久久不卡| 一本色道久久99一综合| 国产成人久久精品二区三区| 久久综合亚洲色HEZYO国产| 久久亚洲精品中文字幕三区| 久久久噜噜噜久久中文字幕色伊伊 | 亚洲伊人久久精品影院| 精品国产乱码久久久久久郑州公司 | 激情五月综合综合久久69| 精品久久亚洲中文无码| 国产—久久香蕉国产线看观看| 中文无码久久精品| 久久久久久无码国产精品中文字幕 | 久久久青草青青国产亚洲免观| 精品国产青草久久久久福利| 久久福利片| 亚洲国产成人久久精品动漫| 九九久久99综合一区二区| 精品久久久久久国产91| 无码国内精品久久人妻| 久久久久久av无码免费看大片| 99久久久国产精品免费无卡顿 | 国产精品久久久久久久午夜片 | 国产69精品久久久久9999APGF| 精品无码人妻久久久久久| 99热热久久这里只有精品68| 久久精品国产99久久无毒不卡 | 国产精品无码久久综合网| 日产精品久久久一区二区| 国产精品久久久久蜜芽| 狠狠色丁香久久婷婷综合_中| 久久婷婷综合中文字幕| 91精品国产综合久久精品| 久久精品国产亚洲网站| 国产精品久久99| 久久国产精品-久久精品|