OpenCV透视变换

用Iplimage存储:

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

using namespace cv;
int main(int argc, char **argv)
{
    IplImage *img = cvLoadImage("lena.jpg");
	CvSize img_size = cvGetSize(img);
	IplImage *new_img = cvCreateImage(img_size,IPL_DEPTH_8U,3);
	CvScalar pixel;

	while (1)
	{
		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]);
				double xx = (0.5*i);
				double yy = (0.5*j);
				double ww = i*0.001+1; //ww = a*i + b*j + 1;
				xx/=ww;
				yy/=ww;
				cvSet2D(new_img, (int)xx, (int)yy, pixel);
			}
		}
    cvNamedWindow("image", 1);
    cvShowImage("image", new_img);
    cvWaitKey(0);
    cvDestroyWindow("image");
    cvReleaseImage(&img);
	cvReleaseImage(&new_img);
    return 0;
}


用Mat存储:

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

using namespace cv;
int main(int argc, char **argv)
{
    Mat img_original = imread("lena.jpg");
	Mat img_new(512,512,CV_8UC3,Scalar(255,255,255));
	imshow("Lena Original",img_original);

	for (int row = 0; row < img_new.rows; row++)
		for (int col =0; col < img_new.cols; col++)
		{
			double xx = row;
			double yy = col;
			double ww = 0.001*xx + 1;
			xx/=ww;
			yy/=ww;
			img_new.at<Vec3b>((int)xx,(int)yy)=img_original.at<Vec3b>(row,col);
			printf("---RUNNING---\n");
			printf("ROW:%d COL:%d\n",row,col);
		}
    imshow("Lena Modified",img_new);
    cvWaitKey(0);
    return 0;
}


sdwer控制:

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

using namespace cv;

CvSize img_size;

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

int main(int argc, char **argv)
{
    IplImage *img = cvLoadImage("lena.jpg");
	
	img_size.height=768;
	img_size.width=1024;
	CvScalar pixel;
	double factori=0;
	double factorj=0;
	while (1)
	{
		IplImage *new_img = cvCreateImage(img_size,IPL_DEPTH_8U,3);
		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]);
				double xx = (0.5*i)+(img_size.height/2-img->height/2);
				double yy = (0.5*j)+(img_size.width/2-img->width/2);
				double ww = i*factori + j*factorj + 1; //ww = a*i + b*j + 1;
				xx/=ww;
				yy/=ww;
				if (vaild(xx,yy))
					cvSet2D(new_img, (int)xx, (int)yy, pixel);
			}
		}
		cvNamedWindow("image", 1);
		cvShowImage("image", new_img);
		char c;
		c=cvvWaitKey(0);
		if (c=='s')
			factori+=0.0005;
		else if (c=='d')
			factori-=0.0005;
		else if (c=='w')
			factorj+=0.0005;
		else if (c=='e')
			factorj-=0.0005;
		else if (c=='r')
			factori=factorj=0;
		cvReleaseImage(&new_img);
	}
	cvDestroyWindow("image");
	cvReleaseImage(&img);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值