PhotoShop算法实现--图像错切(二十)

PhotoShop算法实现--图像错切(二十)

[email protected]

http://blog.csdn.net/kezunhai

            图像倾斜(Slant)就是将图像按指定的方向进行错切,使图像变成平行四边形。

PhotoShop算法实现--图像错切(二十)_第1张图片

           上图示意了将举行沿上下左右四个不同方向倾斜成平行四边形的过程。图中的水平和垂直箭头表示倾斜的正方向。每幅图的三个坐标分别表示原图的左上角、右上角、左下角三个顶点经过倾斜后,映射到目标平行四边形中的新坐标。

           实现代码:

// 图像错切算法实现
// slat:错切参数(水平或垂直方向)
// flag=1,水平方向; flag = -1, 垂直方向
// (x1,y1)错切和为(x2,y2)
// 水平方向:x2 = x1-y1*tan(theta)  
//           y2 = y1             

void PhotoShop::Slant(Mat& img, Mat& dst, float angle, int flag)
{
	int height0 = img.rows;
	int width0 = img.cols;

	float ftan = fabs((float)tan(angle/180.0*PI));

	//float ftan = (float)tan(angle/180.0*PI);

	int newHeight =0;
	int newWidth = 0;

	if ( flag == 1) // 水平方向, 高度不变
	{
		newHeight = height0;
		newWidth = (int)(width0 + height0*fabs(ftan));
	}
	else  // 垂直方向,宽度不变
	{
		newHeight = (int)(height0 + width0*fabs(ftan));
		newWidth = width0;
	}	

	if( dst.empty())	
		dst.create(newHeight, newWidth, img.type());

	int chns = dst.channels();	

	int i, j, k, i0, j0;
	
	for (i=0; i=0 && i0=0 && j0
        测试效果:

PhotoShop算法实现--图像错切(二十)_第2张图片

作者:kezunhai 出处:http://blog.csdn.net/kezunhai 欢迎转载或分享,但请务必声明文章出处。


你可能感兴趣的:(PhotoShop算法实现,photoshop算法实现,图像错切,PhotoShop算法)