360°旋转透视变换


#include <opencv2/opencv.hpp>
#include <stdio.h>

using namespace cv;

CvSize img_size;

bool valid(int xx,int yy)
{
	return (xx>0 && xx<img_size.height && yy>0 && yy<img_size.width);
}

bool pixel_isused(CvScalar *pixel)
{
	int R=pixel->val[0];
	int G=pixel->val[1];
	int B=pixel->val[2];
	return !(R==205 && G==205 && B==205);
}

void fill(int xx,int yy,IplImage *img)
{
	int x_lower = xx-1;
	int x_upper = xx+1;
	int y_lower = yy-1;
	int y_upper = yy+1;
	CvScalar pixel_set, pixel_detect;
	pixel_set = cvGet2D(img, xx, yy);
	pixel_detect = cvGet2D(img, xx, yy+1);
	for (int i = 1; i <= 3; i++)
		if (valid(xx,yy+i))
		{
			if (!pixel_isused(&pixel_detect))
			{
				cvSet2D(img, xx, yy+i, pixel_set);
				//printf("%d %d DONE\n",xx,yy+1);
			}
		}
	/*for (int row = x_lower; row <= x_upper; row++)
		for (int col = y_lower; col <= y_upper; col++)
		{
			pixel_detect = cvGet2D(img, row, col);
			if (valid(row,col))
			{
				if (!pixel_isused(&pixel_detect))
				{
					cvSet2D(img, row, col, pixel_set);
					printf("%d %d DONE\n",row,col);
				}
			}
		}*/
}

int main(int argc, char **argv)
{
    IplImage *img = cvLoadImage("Test2.png");
	
	img_size.height=768;
	img_size.width=1024;
	CvScalar pixel;
	double factori=0;
	double factorj=0;
	int mode=0;
	while (1)
	{
		IplImage *new_img = cvCreateImage(img_size,IPL_DEPTH_8U,3);
		IplImage *new_img2 = cvCreateImage(img_size,IPL_DEPTH_8U,3);
		//printf("%d %d\n",img->height,img->width);
		pixel = cvGet2D(new_img, 245, 245);
		//printf("B=%f,G=%f,R=%f\t", pixel.val[0], pixel.val[1], pixel.val[2]);
		
		for (int i = 0; i < img->height; ++i)
		{
			for (int j = 0; j < img->width; ++j)
			{
				pixel = cvGet2D(img, i, j);
				//printf("B=%f,G=%f,R=%f\t", pixel.val[0], pixel.val[1], pixel.val[2]);
				int width=img->width/2;
				int height=img->height/2;
				int axis=(img_size.width/2-width/2);

				double xx = 0.5*i+(img_size.height/2-height/2);
				double yy = j*0.5+(img_size.width/2-width/2);//(img_size.width/2-img->width/2);
				double yy2=2*axis-(j*0.5+(img_size.width/2-width/2));//2*axis-j*0.5+(img_size.width/2-width/2);
				//double yy = (2.0*j-j)/j*0.5+(img_size.width/2-img->width/2);
				double ww = i*factori + j*factorj + 1; //ww = a*i + b*j + 1;
				xx/=ww;
				yy/=ww;
				yy2/=ww;
				if (valid(xx,yy))
				{
					cvSet2D(new_img, (int)xx, (int)yy, pixel);
					fill((int)xx, (int)yy, new_img);
				}
				if (valid(xx,yy2))
				{
					cvSet2D(new_img2,(int)xx,(int)yy2,pixel);
					fill((int)xx, (int)yy2, new_img2);
				}
			}
		}
		if (!mode)
		{
			cvNamedWindow("image");
			cvShowImage("image", new_img);
		}
		else
		{
			cvNamedWindow("image");
			cvShowImage("image",new_img2);
		}
		char c;
		c=cvWaitKey(0);
		if (!mode)
		{//factorj+=0.0005;
			if (c=='s')//下
				factori+=0.0005;
			else if (c=='w')//上
				factori-=0.0005;
			else if (c=='d')//右
				factorj+=0.0005;
			else if (c=='a')//左
				factorj-=0.0005;
			else if (c=='r')//恢复
				factori=factorj=0;
		}
		else
		{//factorj-=0.0005;
			if (c=='s')//下
				factori+=0.0005;
			else if (c=='w')//上
				factori-=0.0005;
			else if (c=='d')//右
				factorj-=0.0005;
			else if (c=='a')//左
				factorj+=0.0005;
			else if (c=='r')//恢复
				factori=factorj=0;
		}
		printf("out:factor:%f mode:%d\n",factorj,mode);
		if (factorj>0.0005*6 || factorj<-0.0005*4)
		{
			mode^=1;
			printf("in:factor:%f mode:%d\n",factorj,mode);
		}
		cvReleaseImage(&new_img);
		cvReleaseImage(&new_img2);
	}
	cvDestroyWindow("image");
	cvReleaseImage(&img);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值