OpenCV_7漩涡

一,原图:

二,代码:

//7漩涡
template<typename T> T sqr(T x) { return x*x; }
double Pi = 3.14;
double Para = 8;

void Swirl()
{
	Mat src = imread("D:\\test\\26.png");
	int heigh = src.rows;
	int width = src.cols;
	Point center(width / 2, heigh / 2);
	Mat img;
	src.copyTo(img);
	Mat src1u[3];
	split(src, src1u);

	for (int y = 0; y<heigh; y++)
	{
		uchar* imgP = img.ptr<uchar>(y);
		uchar* srcP = src.ptr<uchar>(y);
		for (int x = 0; x<width; x++)
		{
			int R = norm(Point(x, y) - center);
			double angle = atan2((double)(y - center.y), (double)(x - center.x));
			double delta = Pi*Para / sqrtf(R + 1);
			int newX = R*cos(angle + delta) + center.x;
			int newY = R*sin(angle + delta) + center.y;

			if (newX<0) newX = 0;
			if (newX>width - 1) newX = width - 1;
			if (newY<0) newY = 0;
			if (newY>heigh - 1) newY = heigh - 1;

			imgP[3 * x] = src1u[0].at<uchar>(newY, newX);
			imgP[3 * x + 1] = src1u[1].at<uchar>(newY, newX);
			imgP[3 * x + 2] = src1u[2].at<uchar>(newY, newX);
		}
	}
	imshow("vortex", img);
	waitKey();
	imwrite("D:/img/漩涡.jpg", img);
}

//-----开始------
void COpenCVLearningDlg::OnBnClickedStartButton()
{
	Swirl();
}

三,结果:

欢迎扫码关注我的微信公众号

原文地址:https://blog.csdn.net/sangni007/column/info/stylizefliter

猜你喜欢

转载自blog.csdn.net/sxlsxl119/article/details/84863814