在 C++ 中使用 OpenCV 對圖像中的對象進行扭曲透視
例子。
代碼:
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>
using namespace cv;
using namespace std;
string PATH = "funk.jpg"; //Image Path
int AREA_FILTER = 1000;
Mat imgOrg, imgProc, imgWarp;
vector<Point> initialPoints, docPoints;
int w = 420, h = 596;
Mat preProcessing(Mat img)
{
cvtColor(img, imgProc, COLOR_BGR2GRAY); // to gray scale
GaussianBlur(imgProc, imgProc, Size(3,3), 3, 0); // blurring for better canny performance
Canny(imgProc, imgProc, 25, 75); // edge detection
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
dilate(imgProc, imgProc, kernel);
return imgProc;
}
vector<Point> getContours(Mat imgDil){
//detects the biggest rectangle in image
vector<vector<Point>> contours; //vectors example: {{Point(20,30),Point(50,60)},{},{}}
vector<Vec4i> hierarchy;
findContours(imgDil,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE); //finding contours
vector<vector<Point>> conPoly(contours.size());
vector<Rect> boundRect(contours.size());
vector<Point> biggest;
int maxArea=0;
for (int i=0;i<contours.size();i++){
int area = contourArea(contours[i]);
string objectType;
if(area>AREA_FILTER){ //filter small rectangles
float peri = arcLength(contours[i],true);
approxPolyDP(contours[i],conPoly[i],0.02*peri,true);
if(area>maxArea && conPoly[i].size()==4){ //find biggest (4 for rectangle)
maxArea = area;
biggest = {conPoly[i][0],conPoly[i][1],conPoly[i][2],conPoly[i][3]};
}
}
}
return biggest;
}
void drawPoints(vector<Point> points, Scalar color){
for(int i=0;i<points.size();i++)
{
circle(imgOrg,points[i], 5,color,F(xiàn)ILLED);
putText(imgOrg, to_string(i),points[i],F(xiàn)ONT_HERSHEY_PLAIN,4,color,4);
}
}
vector<Point> reorder(vector<Point> points ){
vector<Point> newPoints;
vector<int> sumPoints, subPoints;
//get corners
for(int i = 0;i<4;i++){
sumPoints.push_back(points[i].x + points[i].y);
subPoints.push_back(points[i].x - points[i].y);
}
newPoints.push_back(points[min_element(sumPoints.begin(),sumPoints.end()) - sumPoints.begin()]);
newPoints.push_back(points[max_element(subPoints.begin(),subPoints.end()) - subPoints.begin()]);
newPoints.push_back(points[min_element(subPoints.begin(),subPoints.end()) - subPoints.begin()]);
newPoints.push_back(points[max_element(sumPoints.begin(),sumPoints.end()) - sumPoints.begin()]);
return newPoints;
}
Mat getWarp(Mat img, vector<Point> points, float w, float h)
{
Point2f src[4] = {points[0],points[1],points[2],points[3]};
Point2f dst[4] = {{0.0f,0.0f},{w,0.0f},{0.0f,h},{w,h}};
Mat matrix = getPerspectiveTransform(src,dst);
warpPerspective(img, imgWarp, matrix, Point(w, h));
return imgWarp;
}
void main() {
//sample
imgOrg = imread(PATH);
resize(imgOrg,imgOrg,Size(),0.5,0.5); // reduce the size of the photo in half
//preprocessing
imgProc = preProcessing(imgOrg);
//get contours
initialPoints = getContours(imgProc);
//drawPoints(initialPoints,Scalar(0,0,255));
docPoints = reorder(initialPoints);
//drawPoints(docPoints,Scalar(0,255,0));
//warp
imgWarp = getWarp(imgOrg, docPoints, w, h);
imshow("Image imgWarp",imgWarp);
waitKey(0);
}
讓我們分解代碼;
首先我們讀取圖像文件。然后我們(可選地)減小圖像的大小。
string PATH = "funk.jpg"; //Image Path
imgOrg = imread(PATH);
resize(imgOrg,imgOrg,Size(),0.5,0.5);
為了從操作中獲得更好的結果,我們首先需要對圖像進行一些預處理和轉換。我在一個稱為預處理的方法中收集了所有這些過程。
預處理功能
在使用 opencv 時,我們經常將圖像轉換為灰度。原因是:
· 它減小了尺寸。我們獲得了單個通道,而不是 RGB 的三個通道。
· 我們得到更低的復雜性。RGB:10x10x3 像素 = 300 個數(shù)據(jù);灰度:我們只有10x10x1 = 100 個輸入。
· 許多 Opencv 方法只能在灰度下工作。因此,有必要提前進行轉換。
cvtColor(img, imgProc, COLOR_BGR2GRAY);
我們將使用 Canny Edge Detector 來檢測角點。它在圖像模糊的情況下獲得了更好的效果。這個過程稱為平滑。邊緣檢測器內核對噪聲非常敏感。因此,始終有必要應用平滑。
Size(3,3):高斯核的大小。
GaussianBlur(imgProc, imgProc, Size(3,3), 3, 0);
Canny 函數(shù)從圖像中提取邊緣。25 和 75 值是保留在該過程中提取的邊緣的閾值。
Canny(imgProc, imgProc, 25, 75);
為形態(tài)學操作創(chuàng)建一個矩形內核。
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
我們將使用膨脹作為形態(tài)學操作。膨脹增加了對象的面積,它增加了圖像中的白色區(qū)域。
dilate(imgProc, imgProc, kernel);
現(xiàn)在讓我們獲取對象的輪廓;
initialPoints = getContours(imgProc);
在getContour方法中,我們檢測將扭曲其透視圖并提取其輪廓的對象。
獲取輪廓
findContours方法將返回我們的輪廓點。我們需要保留所有找到的點嗎?
如果我們傳遞 CHAIN_APPROX_NONE 參數(shù),那么所有的點都會被保留。但是,我們可以通過消除冗余點來獲得存儲空間。為此,我們也可以傳遞 CHAIN_APPROX_SIMPLE。
為了獲得外部輪廓,我們通過了 RETR_EXTERNAL
findContours(imgDil,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE); //finding contours
然后,在 for 循環(huán)中,我們去除噪聲并獲取對象。
我們計算每個輪廓的面積。面積必須大于過濾常數(shù);
int area = contourArea(contours[i]);
...
if(area>AREA_FILTER){ //filter small rectangles
我們將在對象周圍找到邊界框。true表示對象已關閉。
float peri = arcLength(contours[i],true);
我們將找到矩形。
approxPolyDP(contours[i],conPoly[i],0.02*peri,true); if(area>maxArea && conPoly[i].size()==4){ //find biggest (4 for rectangle) maxArea = area; biggest = {conPoly[i][0],conPoly[i][1],conPoly[i][2],conPoly[i][3]}; }
我們得到對象的點;
docPoints = reorder(initialPoints);
扭曲:
imgWarp = getWarp(imgOrg, docPoints, w, h);
參考
原文標題 : 在 C++ 中使用 OpenCV 對圖像中的對象進行扭曲透視

請輸入評論內容...
請輸入評論/評論長度6~500個字
最新活動更多
推薦專題
- 1 UALink規(guī)范發(fā)布:挑戰(zhàn)英偉達AI統(tǒng)治的開始
- 2 北電數(shù)智主辦酒仙橋論壇,探索AI產業(yè)發(fā)展新路徑
- 3 降薪、加班、裁員三重暴擊,“AI四小龍”已折戟兩家
- 4 “AI寒武紀”爆發(fā)至今,五類新物種登上歷史舞臺
- 5 國產智駕迎戰(zhàn)特斯拉FSD,AI含量差幾何?
- 6 光計算迎來商業(yè)化突破,但落地仍需時間
- 7 東陽光:2024年扭虧、一季度凈利大增,液冷疊加具身智能打開成長空間
- 8 地平線自動駕駛方案解讀
- 9 封殺AI“照騙”,“淘寶們”終于不忍了?
- 10 優(yōu)必選:營收大增主靠小件,虧損繼續(xù)又逢關稅,能否乘機器人東風翻身?