圖像分析之圖像銳化


本文敘述了幾種實現圖像銳化的方式,包括拉普拉斯濾波,加權均值濾波,形態學梯度和頂帽底帽變換。本文的特色在於,大致證明了這幾種方式可以實現圖像銳化的原因。

更新記錄

本文持續更新!如文中有錯誤,或你對本文有疑問或建議,歡迎留言或發郵件至quarrying#qq.com!

2016年01月08日,發布博文。

2016年04月28日,更新博文。

參考

J.S. Lee, Digital image enhancement and noise filtering by use of local statistics, IEEE Transactions on Pattern Analysis and Machine Intelligence, 2(2):165-168, 1980.

https://en.wikipedia.org/wiki/Unsharp_masking

https://en.wikipedia.org/wiki/Erosion_(morphology)

https://en.wikipedia.org/wiki/Dilation_(morphology)

https://en.wikipedia.org/wiki/Opening_(morphology)

https://en.wikipedia.org/wiki/Closing_(morphology)

https://en.wikipedia.org/wiki/Top-hat_transform

https://en.wikipedia.org/wiki/Mathematical_morphology

https://en.wikipedia.org/wiki/Morphological_Gradient

相關代碼

拉普拉斯濾波和加權均值濾波(主要是高斯濾波)實現圖像銳化的代碼比較常見,於是只給出如下代碼。

代碼一,形態學梯度實現圖像銳化

#include <cv.h>
#include <highgui.h>

// 除了kcvMorphologicalSharpenLike
// factor為正值時,會有銳化效果,為負值時,會有模糊效果。
void kcvMorphologicalSharpenInt(const IplImage* src, IplImage* dst,
    double factor, IplConvKernel* element)
{
    cvErode(src, dst, element, 1);
    cvSub(src, dst, dst);
    cvAddWeighted(src, 1, dst, factor, 0, dst);
}

void kcvMorphologicalSharpenExt(const IplImage* src, IplImage* dst,
    double factor, IplConvKernel* element)
{
    cvDilate(src, dst, element, 1);
    cvSub(dst, src, dst);
    cvAddWeighted(src, 1, dst, -factor, 0, dst);
}

void kcvMorphologicalSharpen(const IplImage* src, IplImage* dst,
    double factor1, double factor2, IplConvKernel* element)
{
    IplImage* tmp = cvCreateImage(cvGetSize(src), 8, src->nChannels);
    kcvMorphologicalSharpenInt(src, tmp, factor1, element);
    cvErode(src, dst, element, 1);
    cvSub(src, dst, dst);
    cvAddWeighted(tmp, 1, dst, factor2, 0, dst);
    cvReleaseImage(&tmp);
}

void kcvMorphologicalSharpenLike(const IplImage* src, IplImage* dst,
    double factor, IplConvKernel* element)
{
    IplImage* tmp = cvCreateImage(cvGetSize(src), 8, src->nChannels);
    cvDilate(src, tmp, element, 1);
    cvErode(src, dst, element, 1);
    cvSub(tmp, dst, dst);
    cvAddWeighted(src, 1, dst, factor, 0, dst);
    cvReleaseImage(&tmp);
}

int main()
{
    IplImage* src = cvLoadImage("lena.jpg", 1);
    IplImage* dst = cvCloneImage(src);
    IplConvKernel* element = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT, 0);
    int64 t1, t2;

    cvShowImage("src", src);
    t1 = cvGetTickCount();
    kcvMorphologicalSharpenInt(src, dst, 1, element);
    t2 = cvGetTickCount();
    printf("kcvMorphologicalSharpenInt %f ms\n", (t2 - t1) / (1000 * cvGetTickFrequency()));
    cvShowImage("kcvMorphologicalSharpenInt", dst);

    t1 = cvGetTickCount();
    kcvMorphologicalSharpenExt(src, dst, 1, element);
    t2 = cvGetTickCount();
    printf("kcvMorphologicalSharpenExt %f ms\n", (t2 - t1) / (1000 * cvGetTickFrequency()));
    cvShowImage("kcvMorphologicalSharpenExt", dst);

    t1 = cvGetTickCount();
    kcvMorphologicalSharpen(src, dst, 1, 1, element);
    t2 = cvGetTickCount();
    printf("kcvMorphologicalSharpen %f ms\n", (t2 - t1) / (1000 * cvGetTickFrequency()));
    cvShowImage("kcvMorphologicalSharpen", dst);

    t1 = cvGetTickCount();
    kcvMorphologicalSharpenLike(src, dst, 1, element);
    t2 = cvGetTickCount();
    printf("kcvMorphologicalSharpenLike %f ms\n", (t2 - t1) / (1000 * cvGetTickFrequency()));
    cvShowImage("kcvMorphologicalSharpenLike", dst);

    t1 = cvGetTickCount();
    kcvMorphologicalSharpenLike(src, dst, -1, element);
    t2 = cvGetTickCount();
    printf("kcvMorphologicalSharpenLike 2 %f ms\n", (t2 - t1) / (1000 * cvGetTickFrequency()));
    cvShowImage("kcvMorphologicalSharpenLike 2", dst);

    cvWaitKey(0);
    cvDestroyAllWindows();
    cvReleaseImage(&src);
    cvReleaseImage(&dst);
    return 0;
}

  

代碼二,頂帽底帽變換實現圖像銳化

close all
I = imread('lena.jpg');
imshow(I)
se = strel('disk', 3);
topHat = imtophat(I, se);
botHat = imbothat(I, se);
J = imadd(I, 1.5 * topHat);
figure, imshow(J);
J = imsubtract(I, 1.5 * botHat);
figure, imshow(J);
J = imsubtract(imadd(I, topHat), botHat);
figure, imshow(J)
J = imadd(imadd(I, topHat), botHat);
figure, imshow(J)
J = imsubtract(imsubtract(I, topHat), botHat);
figure, imshow(J)

正文

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM