您好,登錄后才能下訂單哦!
OpenCV(開源計(jì)算機(jī)視覺庫(kù))是一個(gè)用于處理實(shí)時(shí)圖像和視頻的開源庫(kù)。它包含了許多用于圖像處理、計(jì)算機(jī)視覺和機(jī)器學(xué)習(xí)的優(yōu)化算法。在C++中,我們可以使用OpenCV庫(kù)來(lái)加速圖像變換方法。
以下是一些常見的OpenCV圖像變換方法:
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat src = imread("input.jpg");
Mat dst;
resize(src, dst, Size(), 0.5, 0.5); // 縮小到原來(lái)的一半
imwrite("output.jpg", dst);
return 0;
}
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat src = imread("input.jpg");
Mat dst;
Point2f center(src.cols / 2.0, src.rows / 2.0);
double angle = 30; // 旋轉(zhuǎn)角度
double scale = 1.0; // 縮放比例
Mat rotationMatrix = getRotationMatrix2D(center, angle, scale);
warpAffine(src, dst, rotationMatrix, src.size());
imwrite("output.jpg", dst);
return 0;
}
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat src = imread("input.jpg");
Mat dst;
int x_shift = 100;
int y_shift = 50;
Mat translationMatrix = (Mat_<double>(2, 3) << 1, 0, x_shift, 0, 1, y_shift);
warpAffine(src, dst, translationMatrix, src.size());
imwrite("output.jpg", dst);
return 0;
}
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat src = imread("input.jpg");
Mat dst;
Point2f srcPoints[3] = {Point2f(0, 0), Point2f(src.cols - 1, 0), Point2f(0, src.rows - 1)};
Point2f dstPoints[3] = {Point2f(50, 50), Point2f(src.cols - 100, 0), Point2f(0, src.rows - 150)};
Mat affineMatrix = getAffineTransform(srcPoints, dstPoints);
warpAffine(src, dst, affineMatrix, src.size());
imwrite("output.jpg", dst);
return 0;
}
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat src = imread("input.jpg");
Mat dst;
Point2f srcPoints[4] = {Point2f(0, 0), Point2f(src.cols - 1, 0), Point2f(src.cols - 1, src.rows - 1), Point2f(0, src.rows - 1)};
Point2f dstPoints[4] = {Point2f(50, 50), Point2f(src.cols - 100, 0), Point2f(src.cols - 1, src.rows - 150), Point2f(0, src.rows - 100)};
Mat perspectiveMatrix = getPerspectiveTransform(srcPoints, dstPoints);
warpPerspective(src, dst, perspectiveMatrix, src.size());
imwrite("output.jpg", dst);
return 0;
}
這些示例展示了如何使用OpenCV庫(kù)進(jìn)行圖像變換。在實(shí)際應(yīng)用中,你可能需要根據(jù)需求調(diào)整參數(shù)以獲得所需的變換效果。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。