毛玻璃效果(不是很好)

#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("c:\\TestImages\\1.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)
			{
				l = 0;
				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;
}

你可能感兴趣的:(毛玻璃效果(不是很好))