opencv 数字图像处理-图像错切

图像的错切变换实际上是平面景物在投影平面上的非垂直投影效果。图像错切变换也称为图像剪切、错位或错移变换。图像错切的原理就是保持图像上各点的某一坐标不变,将另一个坐标进行线性变换,坐标不变的轴称为依赖轴,坐标变换的轴称为方向轴。图像错切一般分为两种情况:水平方向错切和垂直方向错切。


####################################################


水平方向的错切,即沿x轴方向关于y的错切,即x=x0+c*y0, y=y0

若c>0,则沿+x轴方向错切,反之,如果c<0,则沿-x轴方向错切.


void imageTwist(IplImage *src, IplImage *res, float ratio)
{
	cvZero(res);
	int res_wid;
	int res_hei;

	unsigned char* src_data = (unsigned char*)src->imageData;
	unsigned char* res_data = (unsigned char*)res->imageData;

	for (int i=0; iheight; i++)
	{
		if (ratio >= 0)
			for (int j=0; jwidth; j++)
			{
				res_hei=i;
				res_wid=j+res_hei*ratio;
				res_data[res_hei*res->widthStep+res_wid] = src_data[i*src->widthStep+j];
			}
		else 
		{
			int offset=ratio*src->height;
			for (int j=src->width-1; j>=0; j--)
			{
				res_hei=i;
				res_wid=j+res_hei*ratio;
				res_data[res_hei*res->widthStep+res_wid+offset] = src_data[i*src->widthStep+j];
			}
		}
	}

	cvNamedWindow("src");
	cvNamedWindow("res");
	cvShowImage("src", src);
	cvShowImage("res", res);
	cvWaitKey(0);
	cvReleaseImage(&src);
	cvReleaseImage(&res);
	cvDestroyAllWindows();
}

void imageCutting(void)
{
	src=cvLoadImage("lena.jpg", 0);
	if (src == NULL)
		exit(0);

	float ratio;
	cout<<"输入水平错切比率:"<>ratio;
	
	int res_wid;
	int res_hei;
	if (ratio < 0)
	{
		res_wid=src->width+src->height*ratio*(-1);
	} else {
		res_wid=src->width+src->height*ratio;
	}

	res_hei=src->height;
	CvSize size=cvSize(res_wid, res_hei);
	res=cvCreateImage(size, src->depth, src->nChannels);
	if (res == NULL)
		exit(0);

	imageTwist(src, res, ratio);//图像扭曲
}


opencv 数字图像处理-图像错切_第1张图片




opencv 数字图像处理-图像错切_第2张图片


#########################################################


垂直方向错切,即沿y轴方向关于x的错切。与水平错切类似变换,即x=x0, y=y0+b*x0


void imageTwistV(IplImage *src, IplImage *res, float ratio)
{
	cvZero(res);
	int res_wid;
	int res_hei;

	unsigned char* src_data = (unsigned char*)src->imageData;
	unsigned char* res_data = (unsigned char*)res->imageData;

	for (int i=0; iwidth; i++)
	{
		if (ratio >= 0)
		{
			int offset=ratio*src->width;
			for (int j=0; jheight; j++)
			{
				res_wid=i;
				res_hei=j+offset-res_wid*ratio;
				res_data[res_hei*res->widthStep+res_wid] = src_data[j*src->widthStep+i];
			}
		}
		else 
		{
			//int offset=ratio*src->height*(-1);
			for (int j=src->height-1; j>=0; j--)
			{
				res_wid=i;
				res_hei=j+res_wid*ratio*(-1);
				res_data[res_hei*res->widthStep+res_wid] = src_data[j*src->widthStep+i];
			}
		}
	}

	cvNamedWindow("src");
	cvNamedWindow("res");
	cvShowImage("src", src);
	cvShowImage("res", res);
	cvWaitKey(0);
	cvReleaseImage(&src);
	cvReleaseImage(&res);
	cvDestroyAllWindows();
}

void imageCutting(void)
{
	src=cvLoadImage("lena.jpg", 0);
	if (src == NULL)
		exit(0);

	float ratio=0;
	cout<<"输入垂直错切比率:"<>ratio;
	
	int res_wid;
	int res_hei;
	if (ratio < 0)
	{
		res_hei=src->height+src->width*ratio*(-1);
		//res_wid=src->width+src->height*ratio*(-1);
	} else {
		res_hei=src->height+src->width*ratio;
		//res_wid=src->width+src->height*ratio;
	}

	res_wid=src->width;
	//res_hei=src->height;
	CvSize size=cvSize(res_wid, res_hei);
	res=cvCreateImage(size, src->depth, src->nChannels);
	if (res == NULL)
		exit(0);

//	imageTwistH(src, res, ratio);//图像扭曲
	imageTwistV(src, res, ratio);
}

opencv 数字图像处理-图像错切_第3张图片


opencv 数字图像处理-图像错切_第4张图片


你可能感兴趣的:(opencv)