溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言中OpenCV怎樣實現(xiàn)柱面投影

發(fā)布時間:2021-12-27 09:30:00 來源:億速云 閱讀:229 作者:柒染 欄目:開發(fā)技術

C語言中OpenCV怎樣實現(xiàn)柱面投影,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

前言

在做全景拼接的時候,為了保持圖片中的空間約束與視覺的一致性,需要進行柱面投影,否則離中心圖像距離越遠的圖像拼接后變形越大。

柱面投影公式為

C語言中OpenCV怎樣實現(xiàn)柱面投影

實現(xiàn)代碼

針對彩色圖像

int main()
{
	cv::Mat image1 = cv::imread("images/1.jpg", 1);
	if (!image1.data)
		return 0;
	imshow("image1", image1);
 
	Mat imgOut = Mat(image1.rows, image1.cols, CV_8UC3);
	float w = image1.cols;
	float h = image1.rows;
	float f = (w / 2) / atan(PI / 8);
 
	for (int i = 0; i < image1.rows; i++)
	{
		for (int j = 0; j < image1.cols; j++)
		{
			float x = j;
			float y = i;
			float x1 = f * atan((x - w / 2) / f) + f * atan(w / (2.0f * f));
			float y1 = f * (y - h / 2.0f) / sqrt((x - w / 2.0f) * (x - w / 2.0f) + f * f) + h / 2.0f;
 
			int col = (int)(x1 + 0.5f);//加0.5是為了四舍五入
			int row = (int)(y1 + 0.5f);//加0.5是為了四舍五入
 
			if (col < image1.cols && row < image1.rows)
			{
				imgOut.at<Vec3b>(row, col)[0] = image1.at<Vec3b>(i, j)[0];
				imgOut.at<Vec3b>(row, col)[1] = image1.at<Vec3b>(i, j)[1];
				imgOut.at<Vec3b>(row, col)[2] = image1.at<Vec3b>(i, j)[2];
			}
		}
	}
 
	imshow("imgOut", imgOut);
 
	waitKey(0);
	return 0;
}

實現(xiàn)效果

C語言中OpenCV怎樣實現(xiàn)柱面投影

C語言中OpenCV怎樣實現(xiàn)柱面投影

針對灰度圖像

cv::Mat image1 = cv::imread("E:\\zcb_work\\2113\\pic2\\k.jpg", 0);
	if (!image1.data)
		return 0;
	imshow("image1", image1);

	cv::Mat image2 = cv::imread("E:\\zcb_work\\2113\\pic2\\j.jpg", 0);
	if (!image2.data)
		return 0;
	imshow("image2", image2);

 	Mat imgOut1 = Mat(image1.rows, image1.cols, CV_8UC1);
	imgOut1.setTo(0);
	Mat imgOut2 = Mat(image2.rows, image2.cols, CV_8UC1);
	imgOut2.setTo(0);
	
	float w = image1.cols;
	float h = image1.rows;
	float f = (w / 2) / atan(PI / 8);
 
	for (int i = 0; i < image1.rows; i++)
	{
		for (int j = 0; j < image1.cols; j++)
		{
			float x = j;
			float y = i;
			float x1 = f * atan((x - w / 2) / f) + f * atan(w / (2.0f * f));
			float y1 = f * (y - h / 2.0f) / sqrt((x - w / 2.0f) * (x - w / 2.0f) + f * f) + h / 2.0f;
 
			int col = (int)(x1 + 0.5f);//加0.5是為了四舍五入
			int row = (int)(y1 + 0.5f);//加0.5是為了四舍五入
 
			if (col < image1.cols && row < image1.rows)
			{
				imgOut1.at<uchar>(row, col) = image1.at<uchar>(i, j);
				imgOut2.at<uchar>(row, col) = image2.at<uchar>(i, j);
				//imgOut.at<Vec3b>(row, col)[1] = image1.at<Vec3b>(i, j)[1];
				//imgOut.at<Vec3b>(row, col)[2] = image1.at<Vec3b>(i, j)[2];
			}
		}
	}
 
	imshow("imgOut1", imgOut1);
	imshow("imgOut2", imgOut2);

實現(xiàn)效果

原圖

C語言中OpenCV怎樣實現(xiàn)柱面投影

柱面投影

C語言中OpenCV怎樣實現(xiàn)柱面投影

用surf算法,特征檢測,

C語言中OpenCV怎樣實現(xiàn)柱面投影

合成這樣,呵呵呵,

C語言中OpenCV怎樣實現(xiàn)柱面投影 

看完上述內容,你們掌握C語言中OpenCV怎樣實現(xiàn)柱面投影的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI