OpenCV编写毛玻璃效果实现(需进一步提高)

【OpenCV/Python】 同时被 2 个专栏收录
46 篇文章 0 订阅
46 篇文章 52 订阅
    #include "cv.h"  
    #include "highgui.h"  
    #include <stdio.h>  
    #include <stdlib.h>  
    #include <time.h>  
    #include <math.h>  
      
    // color image processing Frosted-glass effect  
    int main() {  
        IplImage* src = cvLoadImage("../Test.jpg", 1);  
        if(src)    {  
            int w = src->width, h = src->height, ws = src->widthStep;  
            uchar* dsrc = (uchar*)src->imageData;  
            IplImage* dst = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);  
            uchar* ddst = (uchar*)dst->imageData;  
            cvZero(dst);
     
            // random window size, n must be odd number.  
            int n = 7;  
            int rw = n * n;  
            int nf = floor(n / 2.0); 
            IplImage* arr = cvCreateImage(cvSize(1, rw), src->depth, src->nChannels);  
            uchar* darr = (uchar*)arr->imageData;  
            IplImage* tmp = cvCreateImage(cvSize(1, 1), src->depth, src->nChannels);  
            uchar* dtmp = (uchar*)tmp->imageData; 
            int i, j, k, l, m;  
            for(i = nf; i < h - nf; i += n)  
                for(j = nf; j < w - nf; j += n)   {              
                    for(k = -nf; k <= nf; k++)  
                        for(m = -nf; m <= nf; m++)     {  
                            darr[3 * l] = dsrc[(i + k) * ws + 3 * (j + m)];  
                            darr[3 * l + 1] = dsrc[(i + k) * ws + 3 * (j + m) + 1];  
                            darr[3 * l + 2] = dsrc[(i + k) * ws + 3 * (j + m) + 2];
                            l++;  
                        }  
                    srand((int)time(NULL));  
                    for(k = 0; k < rw; k++)          {  
                        m = rand() % rw;        
                
                        dtmp[0] = darr[3 * m];  
                        dtmp[1] = darr[3 * m + 1];  
                        dtmp[2] = darr[3 * m + 2];
     
                        darr[3 * m] = darr[3 * k];  
                        darr[3 * m + 1] = darr[3 * k + 1];  
                        darr[3 * m + 2] = darr[3 * k + 2]; 
                        darr[3 * k] = dtmp[0];  
                        darr[3 * k + 1] = dtmp[1];  
                        darr[3 * k + 2] = dtmp[2];  
                    }  
                    l = 0;  
                    for(k = -nf; k <= nf; k++)  
                        for(m = -nf; m <= nf; m++)   {  
                            ddst[(i + k) * ws + 3 * (j + m)] = darr[3 * l];  
                            ddst[(i + k) * ws + 3 * (j + m) + 1] = darr[3 * l + 1];  
                            ddst[(i + k) * ws + 3 * (j + m) + 2] = darr[3 * l + 2];  
                            l++;  
                        }                 
                }  
      
            cvNamedWindow("src", 1);  
            cvShowImage("src", src);  
            cvNamedWindow("dst", 1);  
            cvShowImage("dst", dst);  
      
            cvWaitKey(0);  
      
            cvDestroyAllWindows();  
            cvReleaseImage(&src);  
            cvReleaseImage(&dst);  
            cvReleaseImage(&arr);  
            cvReleaseImage(&tmp);  
                  
            return 0;  
        }  
        printf("Can't open image!/n");  
        return 1;  
    }

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值