图像处理/opencv/C++:图像马赛克

马赛克操作就是在指定大小的范围内,图像的颜色取成相同的。

int masaike(unsigned char *srcData, int width, int height, int stride, int mode)
{
	//@ stride:每个马赛克块的大小
    //@ mode: 未使用参数
	int ret = 0;
	int i, j, gray, offset;
	//offset = stride - width * 3;//这个参数没有用到
	//有些情况下会对图像的宽度保持为4的倍数之类的,这种情况下需要这个参数
	//就是遍历像素的时候,每次换行时,指针跳过填充的部分
	offset = 4 - (width * 3) % 4;
	unsigned char* pSrc = srcData;

	int R, G, B;
	for (j = 0; j < height; j++)
	{
		for (i = 0; i < width; i++)
		{
			if (j%stride == 0)
			{
				if (i%stride == 0)
				{
					B = pSrc[0];
					G = pSrc[1];
					R = pSrc[2];
				}
				else
				{
					pSrc[0] = B;
					pSrc[1] = G;
					pSrc[2] = R;
				}
			}
			else
			{
				unsigned char* tmp = pSrc - width*3;
				pSrc[0] = tmp[0];
				pSrc[1] = tmp[1];
				pSrc[2] = tmp[2];
			}
			pSrc += 3;
		}
		//pSrc += offset;
	}
	return ret;
};
int main()
{
	Mat img_src = imread("darling.jpg", IMREAD_COLOR);
	//namedWindow("img_src", CV_WINDOW_NORMAL);
	imshow("img_src", img_src);
	cout << img_src.step << endl;
	unsigned char * data = (unsigned char*)malloc(sizeof(unsigned char) * img_src.cols * img_src.rows*img_src.channels());
	memcpy(data, img_src.data, sizeof(unsigned char) * img_src.cols * img_src.rows*img_src.channels());
	int flag = masaike(data, img_src.cols, img_src.rows, 8, 0);

	Mat img_dst(img_src.rows, img_src.cols, CV_8UC3, data);
	//namedWindow("img_dst", CV_WINDOW_NORMAL);
	imshow("img_dst", img_dst);
	waitKey();
	return 0;
}

图像处理/opencv/C++:图像马赛克_第1张图片 图像处理/opencv/C++:图像马赛克_第2张图片

 

你可能感兴趣的:(C++,图像处理,opencv,opencv,图像处理,c++)