【OpenCV】透视变换 Perspective Transformation(续)

OpenCV透视变换公式求解及函数介绍
本文继续探讨OpenCV中的透视变换,介绍了使用getPerspectiveTransform()和findHomography()函数进行变换的方法。getPerspectiveTransform()可能会导致图像中有未填充的点,可以采用差值或反向变换解决。findHomography()则适用于通过特征点直接寻找变换矩阵,常在特征匹配中应用。

透视变换的原理和矩阵求解请参见前一篇《透视变换 Perspective Transformation》。在OpenCV中也实现了透视变换的公式求解和变换函数。

求解变换公式的函数:

Mat getPerspectiveTransform(const Point2f src[], const Point2f dst[])
输入原始图像和变换之后的图像的对应4个点,便可以得到变换矩阵。之后用求解得到的矩阵输入perspectiveTransform便可以对一组点进行变换:

void perspectiveTransform(InputArray src, OutputArray dst, InputArray m)
注意这里src和dst的输入并不是图像,而是图像对应的坐标。应用前一篇的例子,做个相反的变换:

int main( )
{
	Mat img=imread("boy.png");
	int img_height = img.rows;
	int img_width = img.cols;
	vector<Point2f> corners(4);
	corners[0] = Point2f(0,0);
	corners[1] = Point2f(img_width-1,0);
	corners[2] = Point2f(0,img_height-1);
	corners[3] = Point2f(img_width-1,img_height-1);
	vector<Point2f> corners_trans(4);
	corners_trans[0] = Point2f(150,250);
	corners_trans[1] = Point2f(771,0);
	corners_trans[2] = Point2f(0,img_height-1);
	corners_trans[3] = Point2f(650,img_height-1);

	Mat transform = getPerspectiveTransform(corners,corners_trans);
	cout<<transform<<endl;
	vector<Point2f> ponits, points_trans;
	for(int i=0;i<img_height;i++){
		for(int j=0;j<img_width;j++){
			ponits.push_back(Point2f(j,i));
		}
	}

	perspectiveTransform( ponits, points_trans, transform);
	Mat img_trans = Mat::zeros(img_height,img_width,CV_8UC3);
	int count = 0;
	for(int i=0;i<img_height;i++){
		uchar* p = img.ptr<uchar>(i);
		for(int j=0;j<img_width;j++){
			int y = points_trans[count].y;
			int x = points_trans[count].x;
			uchar* t = img_trans.ptr<uchar>(y);
			t[x*3]  = p[j*3];
			t[x*3+1]  = p[j*3+1];
			t[x*3+2]  = p[j*3+2];
			count++;
		}
	}
	imwrite("boy_trans.png",img_trans);

	return 0;
}

得到变换之后的图片:


注意这种将原图变换到对应图像上的方式会有一些没有被填充的点,也就是右图中黑色的小点。解决这种问题一是用差值的方式,再一种比较简单就是不用原图的点变换后对应找新图的坐标,而是直接在新图上找反向变换原图的点。说起来有点绕口,具体见前一篇《透视变换 Perspective Transformation》的代码应该就能懂啦。

除了getPerspectiveTransform()函数,OpenCV还提供了findHomography()的函数,不是用点来找,而是直接用透视平面来找变换公式。这个函数在特征匹配的经典例子中有用到,也非常直观:

int main( int argc, char** argv )
{
	Mat img_object = imread( argv[1], IMREAD_GRAYSCALE );
	Mat img_scene = imread( argv[2], IMREAD_GRAYSCALE );
	if( !img_object.data || !img_scene.data )
	{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }

	//-- Step 1: Detect the keypoints using SURF Detector
	int minHessian = 400;
	SurfFeatureDetector detector( minHessian );
	std::vector<KeyPoint> keypoints_object, keypoints_scene;
	detector.detect( img_object, keypoints_object );
	detector.detect( img_scene, keypoints_scene );

	//-- Step 2: Calculate descriptors (feature vectors)
	SurfDescriptorExtractor extractor;
	Mat descriptors_object, descriptors_scene;
	extractor.compute( img_object, keypoints_object, descriptors_object );
	extractor.compute( img_scene, keypoints_scene, descriptors_scene );

	//-- Step 3: Matching descriptor vectors using FLANN matcher
	FlannBasedMatcher matcher;
	std::vector< DMatch > matches;
	matcher.match( descriptors_object, descriptors_scene, matches );
	double max_dist = 0; double min_dist = 100;

	//-- Quick calculation of max and min distances between keypoints
	for( int i = 0; i < descriptors_object.rows; i++ )
	{ double dist = matches[i].distance;
	if( dist < min_dist ) min_dist = dist;
	if( dist > max_dist ) max_dist = dist;
	}

	printf("-- Max dist : %f \n", max_dist );
	printf("-- Min dist : %f \n", min_dist );

	//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
	std::vector< DMatch > good_matches;

	for( int i = 0; i < descriptors_object.rows; i++ )
	{ if( matches[i].distance < 3*min_dist )
	{ good_matches.push_back( matches[i]); }
	}

	Mat img_matches;
	drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
		good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
		vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

	//-- Localize the object from img_1 in img_2
	std::vector<Point2f> obj;
	std::vector<Point2f> scene;

	for( size_t i = 0; i < good_matches.size(); i++ )
	{
		//-- Get the keypoints from the good matches
		obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
		scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
	}

	Mat H = findHomography( obj, scene, RANSAC );

	//-- Get the corners from the image_1 ( the object to be "detected" )
	std::vector<Point2f> obj_corners(4);
	obj_corners[0] = Point(0,0); obj_corners[1] = Point( img_object.cols, 0 );
	obj_corners[2] = Point( img_object.cols, img_object.rows ); obj_corners[3] = Point( 0, img_object.rows );
	std::vector<Point2f> scene_corners(4);
	perspectiveTransform( obj_corners, scene_corners, H);
	//-- Draw lines between the corners (the mapped object in the scene - image_2 )
	Point2f offset( (float)img_object.cols, 0);
	line( img_matches, scene_corners[0] + offset, scene_corners[1] + offset, Scalar(0, 255, 0), 4 );
	line( img_matches, scene_corners[1] + offset, scene_corners[2] + offset, Scalar( 0, 255, 0), 4 );
	line( img_matches, scene_corners[2] + offset, scene_corners[3] + offset, Scalar( 0, 255, 0), 4 );
	line( img_matches, scene_corners[3] + offset, scene_corners[0] + offset, Scalar( 0, 255, 0), 4 );

	//-- Show detected matches
	imshow( "Good Matches & Object detection", img_matches );
	waitKey(0);
	return 0;
}

代码运行效果:



findHomography()函数直接通过两个平面上相匹配的特征点求出变换公式,之后代码又对原图的四个边缘点进行变换,在右图上画出对应的矩形。这个图也很好地解释了所谓透视变换的“Viewing Plane”。


(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)



透视变换是计算机视觉中常用的一种图像处理技术,它可以将图像从一个视角转换到另一个视角。OpenCV是一个开源的计算机视觉库,提供了丰富的函数来实现透视变换透视变换通常用于校正图像的视角,例如将一张倾斜的照片校正为正面视角。透视变换可以通过以下步骤实现: 1. **选择源点和目标点**:首先需要选择图像中的四个点作为源点,然后确定这四个点变换后的目标位置。 2. **计算变换矩阵**:使用源点和目标点计算透视变换矩阵。 3. **应用变换**:使用计算得到的变换矩阵对图像进行透视变换。 下面是一个使用OpenCV进行透视变换的示例代码: ```python import cv2 import numpy as np # 读取图像 image = cv2.imread('input.jpg') cv2.imshow('Original Image', image) # 定义源点 src_points = np.float32([[0, 0], [image.shape[1] - 1, 0], [image.shape[1] - 1, image.shape[0] - 1], [0, image.shape[0] - 1]]) # 定义目标点 dst_points = np.float32([[0, 0], [image.shape[1] - 1, 0], [image.shape[1] - 1, image.shape[0] - 1], [0, image.shape[0] - 1]]) # 计算透视变换矩阵 matrix = cv2.getPerspectiveTransform(src_points, dst_points) # 应用透视变换 result = cv2.warpPerspective(image, matrix, (image.shape[1], image.shape[0])) # 显示结果 cv2.imshow('Perspective Transformation', result) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在这个示例中,我们首先读取了一张图像,然后定义源点和目标点。由于源点和目标点相同,变换后的图像与原图相同。你可以根据需要调整目标点的位置来实现不同的变换效果。
评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值