opencv實(shí)現(xiàn)圖形輪廓檢測
要想實(shí)現(xiàn)輪廓檢測,首先我們需要對(duì)待檢測的圖像進(jìn)行圖像處理:
圖像灰度化、高斯濾波、Canny 邊緣檢測、邊緣檢測放大處理、提取輪廓。
一、實(shí)現(xiàn)簡單的全圖型檢測
即只要將drawContours第三個(gè)參數(shù)設(shè)置為-1 既能實(shí)現(xiàn)圖像的全圖型檢測。
程序:
#include <iostream>
#include <opencv2/highgui.hpp> // 說是說gui 具體什么gui 不清楚
#include <opencv2/imgcodecs.hpp> // 圖像頭文件
#include <opencv2/imgproc.hpp> // 圖像處理頭文件
using namespace std;
using namespace cv;
/*要進(jìn)行圖像形貌檢測之前
*首先要二值化,再進(jìn)行濾波處理,再進(jìn)行Canny邊緣檢測
*最后才能檢測出圖形輪廓
*/
Mat imgGray, imgBlur, imgCanny,imgDil;
void getContours(Mat imgDil,Mat& img);
int main()
{
string path = "resources/shapes.png"; // 導(dǎo)入圖形的時(shí)候,先要在右邊點(diǎn)擊顯示所有文件?。。?
Mat img = imread(path); // 在opencv 中所有的圖像信息都使用Mat
// pre-processing image 圖像預(yù)處理
cvtColor(img, imgGray, COLOR_BGR2GRAY);
GaussianBlur(imgGray, imgBlur,Size(3,3),3,0); // 高斯濾波
Canny(imgBlur, imgCanny, 25, 75);// Canny 邊緣檢測
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); // 其中 Size 和 邊緣檢測的放大倍數(shù)有關(guān)系
dilate(imgCanny, imgDil, kernel);
getContours(imgDil,img); // 第一個(gè)參數(shù) 是尋找輪廓的參數(shù), 第二個(gè)參數(shù)是顯示圖案的參數(shù)
imshow("Image", img);
waitKey(0); // 延時(shí),0即相當(dāng)于無窮大
}
void getContours(Mat imgDil, Mat& img)
{
/* contour is a vector inside that vector there is more vector
* {{Point(20,30),Point(50,60)},{},{}} each vector like a contour and each contour have some points
*
**/
vector<vector<Point>> contours;
vector<Vec4i> hierarchy; // Vec4i 即代表該向量內(nèi)有4個(gè) int 變量typedef Vec<int, 4> Vec4i; 這四個(gè)向量每一層級(jí)代表一個(gè)輪廓
findContours(imgDil, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // CV_CHAIN_APPROX_SIMPLE - 簡單的鏈?zhǔn)浇咏?
drawContours(img, contours, -1, Scalar(255,0,255),2); // contouridx = -1 代表需要繪制所檢測的所有輪廓
}
運(yùn)行結(jié)果:

細(xì)心的讀者會(huì)發(fā)現(xiàn),該程序還將微小的瑕疵檢測到了。
二、去除輪廓瑕疵
去除瑕疵的方法很簡單,即先檢測所有圖形的面積,發(fā)現(xiàn)圖形中的最小面積,即為瑕疵面積(假設(shè)我們已知該瑕疵面積<1000),之后使用if進(jìn)行面積過濾。
程序:
#include <iostream>
#include <opencv2/highgui.hpp> // 說是說gui 具體什么gui 不清楚
#include <opencv2/imgcodecs.hpp> // 圖像頭文件
#include <opencv2/imgproc.hpp> // 圖像處理頭文件
using namespace std;
using namespace cv;
/*要進(jìn)行圖像形貌檢測之前
*首先要二值化,再進(jìn)行濾波處理,再進(jìn)行Canny邊緣檢測
*最后才能檢測出圖形輪廓
*/
Mat imgGray, imgBlur, imgCanny,imgDil;
void getContours(Mat imgDil,Mat& img);
int main()
{
string path = "resources/shapes.png"; // 導(dǎo)入圖形的時(shí)候,先要在右邊點(diǎn)擊顯示所有文件?。?!
Mat img = imread(path); // 在opencv 中所有的圖像信息都使用Mat
// pre-processing image 圖像預(yù)處理
cvtColor(img, imgGray, COLOR_BGR2GRAY);
GaussianBlur(imgGray, imgBlur,Size(3,3),3,0); // 高斯濾波
Canny(imgBlur, imgCanny, 25, 75);// Canny 邊緣檢測
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); // 其中 Size 和 邊緣檢測的放大倍數(shù)有關(guān)系
dilate(imgCanny, imgDil, kernel);
getContours(imgDil,img); // 第一個(gè)參數(shù) 是尋找輪廓的參數(shù), 第二個(gè)參數(shù)是顯示圖案的參數(shù)
imshow("Image", img);
waitKey(0); // 延時(shí),0即相當(dāng)于無窮大
}
void getContours(Mat imgDil, Mat& img)
{
/* contour is a vector inside that vector there is more vector
* {{Point(20,30),Point(50,60)},{},{}} each vector like a contour and each contour have some points
*
**/
vector<vector<Point>> contours;
vector<Vec4i> hierarchy; // Vec4i 即代表該向量內(nèi)有4個(gè) int 變量typedef Vec<int, 4> Vec4i; 這四個(gè)向量每一層級(jí)代表一個(gè)輪廓
findContours(imgDil, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // CV_CHAIN_APPROX_SIMPLE - 簡單的鏈?zhǔn)浇咏?
drawContours(img, contours, -1, Scalar(255,0,255),2); // contouridx = -1 代表需要繪制所檢測的所有輪廓
for(int i = 0;i <contours.size();i++)
{
int area = contourArea(contours[i]) ; // 計(jì)算輪廓面積函數(shù)
if(area > 1000)
{
drawContours(img,contours,i,Scalar(255, 0, 255), 2);
}
}
}
運(yùn)行結(jié)果:

可見我們已經(jīng)成功將瑕疵濾除。
三、判斷輪廓形狀
判斷輪廓之前,我們先要將圖形的拐角點(diǎn)計(jì)算出來,如三角形有三個(gè)拐角點(diǎn),矩形有四個(gè)拐角點(diǎn),圓形有多個(gè)拐角點(diǎn),所以如何得到拐角點(diǎn)是我們檢測輪廓形狀的前提。檢測拐角的核心函數(shù)為approxPolyDP(contours[i],conpoly[i],0.02* peri,true);即多邊形擬合函數(shù)。
程序:
#include <iostream>
#include <opencv2/highgui.hpp> // 說是說gui 具體什么gui 不清楚
#include <opencv2/imgcodecs.hpp> // 圖像頭文件
#include <opencv2/imgproc.hpp> // 圖像處理頭文件
using namespace std;
using namespace cv;
/*要進(jìn)行圖像形貌檢測之前
*首先要二值化,再進(jìn)行濾波處理,再進(jìn)行Canny邊緣檢測
*最后才能檢測出圖形輪廓
*/
Mat imgGray, imgBlur, imgCanny,imgDil;
void getContours(Mat imgDil,Mat& img);
int main()
{
string path = "resources/shapes.png"; // 導(dǎo)入圖形的時(shí)候,先要在右邊點(diǎn)擊顯示所有文件?。?!
Mat img = imread(path); // 在opencv 中所有的圖像信息都使用Mat
// pre-processing image 圖像預(yù)處理
cvtColor(img, imgGray, COLOR_BGR2GRAY);
GaussianBlur(imgGray, imgBlur,Size(3,3),3,0); // 高斯濾波
Canny(imgBlur, imgCanny, 25, 75);// Canny 邊緣檢測
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); // 其中 Size 和 邊緣檢測的放大倍數(shù)有關(guān)系
dilate(imgCanny, imgDil, kernel);
getContours(imgDil,img); // 第一個(gè)參數(shù) 是尋找輪廓的參數(shù), 第二個(gè)參數(shù)是顯示圖案的參數(shù)
imshow("Image", img);
//imshow("Image Gray", imgGray);
//imshow("Image Blur", imgBlur);
//imshow("Image Canny", imgCanny);
//imshow("Image Dilate", imgDil); // 圖像放大之后的邊緣檢測效果要明顯好于 Canny 邊緣檢測,這也是為什么大佬熱衷于dilation的原因
waitKey(0); // 延時(shí),0即相當(dāng)于無窮大
}
// 因?yàn)橐婚_始參數(shù)不同,所以電腦直接將其視為重載函數(shù)
void getContours(Mat imgDil, Mat& img)
{
/* contour is a vector inside that vector there is more vector
* {{Point(20,30),Point(50,60)},{},{}} each vector like a contour and each contour have some points
*
**/
vector<vector<Point>> contours;
vector<Vec4i> hierarchy; // Vec4i 即代表該向量內(nèi)有4個(gè) int 變量typedef Vec<int, 4> Vec4i; 這四個(gè)向量每一層級(jí)代表一個(gè)輪廓
findContours(imgDil, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // CV_CHAIN_APPROX_SIMPLE - 簡單的鏈?zhǔn)浇咏?
//drawContours(img, contours, -1, Scalar(255,0,255),2); // contouridx = -1 代表需要繪制所檢測的所有輪廓
vector<vector<Point>> conpoly(contours.size());// conpoly(paprameter1) ,paprameter1便代表vector對(duì)象的行數(shù),而其列數(shù)中的vector 是使用了point點(diǎn)集但其只包含圖形的拐角點(diǎn)集
vector<Rect> boundRect(contours.size());// Rect 類中x y 函數(shù)描述的是圖形左上角的坐標(biāo)
string objType; // 記錄物體形狀
// 為了濾除微小噪聲,因此計(jì)算area 的面積
for (int i = 0; i < contours.size(); i++) // 關(guān)于contours.size()為什么是返回二維數(shù)組的行,因?yàn)?vector::size()函數(shù)只接受vector 對(duì)象的調(diào)用而contours的所有行(不管列)均為其對(duì)象
{
int area = contourArea(contours[i]);
if (area > 1000)
{
float peri = arcLength(contours[i], true);// 該函數(shù)計(jì)算輪廓的長度,后面的bool值表面輪廓曲線是否閉合若為true 則輪廓曲線閉合
//尋找角點(diǎn)
// conpoly 同樣為輪廓點(diǎn)集但它第二個(gè)數(shù)組中只有1-9個(gè)參數(shù)為了描述各個(gè)輪廓的拐角點(diǎn)
approxPolyDP(contours[i],conpoly[i],0.02* peri,true); // conpoly[i]是輸出array 0.02*peri 這個(gè)參數(shù)理解不了就不要理解?。?! 最后一個(gè)參數(shù)仍然是詢問是否閉合
cout << conpoly[i].size() << endl; // 輸出圖像輪廓中的拐角點(diǎn)
boundRect[i] = boundingRect(conpoly[i]); // 針對(duì)conpoly[i] 進(jìn)行boundingRect 以
drawContours(img, conpoly, i, Scalar(255, 0, 255), 2);
//rectangle(img, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 5);
}
}
}
運(yùn)行結(jié)果:

當(dāng)檢測出所有圖形都應(yīng)具有拐角點(diǎn)數(shù)之后,在加個(gè)if 判斷圖形的點(diǎn)數(shù),之后通過puttext函數(shù)去顯示,圖形的形狀。
程序:
#include <iostream>
#include <opencv2/highgui.hpp> // 說是說gui 具體什么gui 不清楚
#include <opencv2/imgcodecs.hpp> // 圖像頭文件
#include <opencv2/imgproc.hpp> // 圖像處理頭文件
using namespace std;
using namespace cv;
/*要進(jìn)行圖像形貌檢測之前
*首先要二值化,再進(jìn)行濾波處理,再進(jìn)行Canny邊緣檢測
*最后才能檢測出圖形輪廓
*/
Mat imgGray, imgBlur, imgCanny,imgDil;
void getContours(Mat imgDil,Mat& img);
int main()
{
string path = "resources/shapes.png"; // 導(dǎo)入圖形的時(shí)候,先要在右邊點(diǎn)擊顯示所有文件?。?!
Mat img = imread(path); // 在opencv 中所有的圖像信息都使用Mat
// pre-processing image 圖像預(yù)處理
cvtColor(img, imgGray, COLOR_BGR2GRAY);
GaussianBlur(imgGray, imgBlur,Size(3,3),3,0); // 高斯濾波
Canny(imgBlur, imgCanny, 25, 75);// Canny 邊緣檢測
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); // 其中 Size 和 邊緣檢測的放大倍數(shù)有關(guān)系
dilate(imgCanny, imgDil, kernel);
getContours(imgDil,img); // 第一個(gè)參數(shù) 是尋找輪廓的參數(shù), 第二個(gè)參數(shù)是顯示圖案的參數(shù)
imshow("Image", img);
//imshow("Image Gray", imgGray);
//imshow("Image Blur", imgBlur);
//imshow("Image Canny", imgCanny);
//imshow("Image Dilate", imgDil); // 圖像放大之后的邊緣檢測效果要明顯好于 Canny 邊緣檢測,這也是為什么大佬熱衷于dilation的原因
waitKey(0); // 延時(shí),0即相當(dāng)于無窮大
}
// 因?yàn)橐婚_始參數(shù)不同,所以電腦直接將其視為重載函數(shù)
void getContours(Mat imgDil, Mat& img)
{
/* contour is a vector inside that vector there is more vector
* {{Point(20,30),Point(50,60)},{},{}} each vector like a contour and each contour have some points
*
**/
vector<vector<Point>> contours;
vector<Vec4i> hierarchy; // Vec4i 即代表該向量內(nèi)有4個(gè) int 變量typedef Vec<int, 4> Vec4i; 這四個(gè)向量每一層級(jí)代表一個(gè)輪廓
findContours(imgDil, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // CV_CHAIN_APPROX_SIMPLE - 簡單的鏈?zhǔn)浇咏?
//drawContours(img, contours, -1, Scalar(255,0,255),2); // contouridx = -1 代表需要繪制所檢測的所有輪廓
vector<vector<Point>> conpoly(contours.size());// conpoly(paprameter1) ,paprameter1便代表vector對(duì)象的行數(shù),而其列數(shù)中的vector 是使用了point點(diǎn)集但其只包含圖形的拐角點(diǎn)集
vector<Rect> boundRect(contours.size());// Rect 類中x y 函數(shù)描述的是圖形左上角的坐標(biāo)
string objType; // 記錄物體形狀
// 為了濾除微小噪聲,因此計(jì)算area 的面積
for (int i = 0; i < contours.size(); i++) // 關(guān)于contours.size()為什么是返回二維數(shù)組的行,因?yàn)?vector::size()函數(shù)只接受vector 對(duì)象的調(diào)用而contours的所有行(不管列)均為其對(duì)象
{
int area = contourArea(contours[i]);
if (area > 1000)
{
float peri = arcLength(contours[i], true);// 該函數(shù)計(jì)算輪廓的長度,后面的bool值表面輪廓曲線是否閉合若為true 則輪廓曲線閉合
//尋找角點(diǎn)
// conpoly 同樣為輪廓點(diǎn)集但它第二個(gè)數(shù)組中只有1-9個(gè)參數(shù)為了描述各個(gè)輪廓的拐角點(diǎn)
approxPolyDP(contours[i],conpoly[i],0.02* peri,true); // conpoly[i]是輸出array 0.02*peri 這個(gè)參數(shù)理解不了就不要理解?。?! 最后一個(gè)參數(shù)仍然是詢問是否閉合
//drawContours(img, contours , i, Scalar(255, 0, 255), 2);
// 通過conpoly 而繪制的輪廓中只存在程序認(rèn)為應(yīng)該存在的點(diǎn)
cout << conpoly[i].size() << endl; // 輸出圖像輪廓中的拐角點(diǎn)
boundRect[i] = boundingRect(conpoly[i]); // 針對(duì)conpoly[i] 進(jìn)行boundingRect 以便擬合相切矩形
//rectangle(img, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 5); // 使用
int objCor = (int)conpoly[i].size(); // 計(jì)算物體邊角數(shù)
if (3 == objCor) objType = "Triangle";
else
if (4 == objCor)
{ // 計(jì)算float對(duì)象,一定要記得使用 float 強(qiáng)轉(zhuǎn)符號(hào)
float aspRatio = (float)boundRect[i].width/(float)boundRect[i].height;
if(aspRatio<1.05 && aspRatio>0.95)
objType = "Square";
else objType = "Rectangle";
}
else if (objCor > 4) objType = "Circle";
putText(img, objType, Point(boundRect[i].x, boundRect[i].y-5), FONT_HERSHEY_PLAIN, 1, Scalar(0, 69, 255), 1);
drawContours(img, conpoly, i, Scalar(255, 0, 255), 2);
//rectangle(img, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 5);
}
}
}
運(yùn)行結(jié)果:

四、給被檢測圖形套上一個(gè)檢測框
核心函數(shù),使用rectangle()進(jìn)行矩形繪畫。
程序:
#include <iostream>
#include <opencv2/highgui.hpp> // 說是說gui 具體什么gui 不清楚
#include <opencv2/imgcodecs.hpp> // 圖像頭文件
#include <opencv2/imgproc.hpp> // 圖像處理頭文件
using namespace std;
using namespace cv;
/*要進(jìn)行圖像形貌檢測之前
*首先要二值化,再進(jìn)行濾波處理,再進(jìn)行Canny邊緣檢測
*最后才能檢測出圖形輪廓
*/
Mat imgGray, imgBlur, imgCanny,imgDil;
void getContours(Mat imgDil,Mat& img);
int main()
{
string path = "resources/shapes.png"; // 導(dǎo)入圖形的時(shí)候,先要在右邊點(diǎn)擊顯示所有文件?。。?
Mat img = imread(path); // 在opencv 中所有的圖像信息都使用Mat
// pre-processing image 圖像預(yù)處理
cvtColor(img, imgGray, COLOR_BGR2GRAY);
GaussianBlur(imgGray, imgBlur,Size(3,3),3,0); // 高斯濾波
Canny(imgBlur, imgCanny, 25, 75);// Canny 邊緣檢測
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); // 其中 Size 和 邊緣檢測的放大倍數(shù)有關(guān)系
dilate(imgCanny, imgDil, kernel);
getContours(imgDil,img); // 第一個(gè)參數(shù) 是尋找輪廓的參數(shù), 第二個(gè)參數(shù)是顯示圖案的參數(shù)
imshow("Image", img);
//imshow("Image Gray", imgGray);
//imshow("Image Blur", imgBlur);
//imshow("Image Canny", imgCanny);
//imshow("Image Dilate", imgDil); // 圖像放大之后的邊緣檢測效果要明顯好于 Canny 邊緣檢測,這也是為什么大佬熱衷于dilation的原因
waitKey(0); // 延時(shí),0即相當(dāng)于無窮大
}
// 因?yàn)橐婚_始參數(shù)不同,所以電腦直接將其視為重載函數(shù)
void getContours(Mat imgDil, Mat& img)
{
/* contour is a vector inside that vector there is more vector
* {{Point(20,30),Point(50,60)},{},{}} each vector like a contour and each contour have some points
*
**/
vector<vector<Point>> contours;
vector<Vec4i> hierarchy; // Vec4i 即代表該向量內(nèi)有4個(gè) int 變量typedef Vec<int, 4> Vec4i; 這四個(gè)向量每一層級(jí)代表一個(gè)輪廓
findContours(imgDil, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // CV_CHAIN_APPROX_SIMPLE - 簡單的鏈?zhǔn)浇咏?
//drawContours(img, contours, -1, Scalar(255,0,255),2); // contouridx = -1 代表需要繪制所檢測的所有輪廓
vector<vector<Point>> conpoly(contours.size());// conpoly(paprameter1) ,paprameter1便代表vector對(duì)象的行數(shù),而其列數(shù)中的vector 是使用了point點(diǎn)集但其只包含圖形的拐角點(diǎn)集
vector<Rect> boundRect(contours.size());// 記錄各圖形的擬合矩形
string objType; // 記錄物體形狀
// 為了濾除微小噪聲,因此計(jì)算area 的面積
for (int i = 0; i < contours.size(); i++) // 關(guān)于contours.size()為什么是返回二維數(shù)組的行,因?yàn)?vector::size()函數(shù)只接受vector 對(duì)象的調(diào)用而contours的所有行(不管列)均為其對(duì)象
{
int area = contourArea(contours[i]);
if (area > 1000)
{
float peri = arcLength(contours[i], true);// 該函數(shù)計(jì)算輪廓的長度,后面的bool值表面輪廓曲線是否閉合若為true 則輪廓曲線閉合
//尋找角點(diǎn)
// conpoly 同樣為輪廓點(diǎn)集但它第二個(gè)數(shù)組中只有1-9個(gè)參數(shù)為了描述各個(gè)輪廓的拐角點(diǎn)
approxPolyDP(contours[i],conpoly[i],0.02* peri,true); // conpoly[i]是輸出array 0.02*peri 這個(gè)參數(shù)理解不了就不要理解?。?! 最后一個(gè)參數(shù)仍然是詢問是否閉合
//drawContours(img, contours , i, Scalar(255, 0, 255), 2);
// 通過conpoly 而繪制的輪廓中只存在程序認(rèn)為應(yīng)該存在的點(diǎn)
cout << conpoly[i].size() << endl; // 輸出圖像輪廓中的拐角點(diǎn)
boundRect[i] = boundingRect(conpoly[i]); // 針對(duì)conpoly[i] 進(jìn)行boundingRect 以便擬合相切矩形
//rectangle(img, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 5); // 使用
int objCor = (int)conpoly[i].size(); // 計(jì)算物體邊角數(shù)
if (3 == objCor) objType = "Triangle";
else
if (4 == objCor)
{ // 計(jì)算float對(duì)象,一定要記得使用 float 強(qiáng)轉(zhuǎn)符號(hào)
float aspRatio = (float)boundRect[i].width/(float)boundRect[i].height;
if(aspRatio<1.05 && aspRatio>0.95)
objType = "Square";
else objType = "Rectangle";
}
else if (objCor > 4) objType = "Circle";
putText(img, objType, Point(boundRect[i].x, boundRect[i].y-5), FONT_HERSHEY_PLAIN, 1, Scalar(0, 69, 255), 1);
drawContours(img, conpoly, i, Scalar(255, 0, 255), 2);
rectangle(img, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 5);
}
}
}
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VSCode 使用 Code Runner 插件無法編譯運(yùn)行文件名帶空格的文件問題
這篇文章主要介紹了VSCode 使用 Code Runner 插件無法編譯運(yùn)行文件名帶空格的文件問題,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)紅黑樹應(yīng)用實(shí)例代碼
紅黑樹它一種特殊的二叉查找樹,這意味著它滿足二叉查找樹的特征,但是也有許多自己的特性,這篇文章主要給大家介紹了關(guān)于C++實(shí)現(xiàn)紅黑樹的相關(guān)資料,需要的朋友可以參考下2021-11-11
c語言程序設(shè)計(jì)文件操作方法示例(CreateFile和fopen)
c主要的文件操作函數(shù)有:CreateFile,CloseHandle,ReadFile,WriteFile,SetFilePointer,GetFileSize。其中的讀寫操作是以字符為單位,獲得文件大小也是以字符為單位。2013-12-12

