您好,登錄后才能下訂單哦!
輪廓提取
findContours發現輪廓
findContours( InputOutputArray binImg, //輸入8bit圖像,0值像素值不變,非0的像素看成1;(變為二值圖像) OutputArrayOfArrays contours,//輸出找到的輪廓對象 OutputArray, hierachy// 圖像的拓撲結構 int mode, //輪廓返回的模式(RETR_TREE等) int method,//發現方法(CHAIN_APPROX_SIMPLE等) Point offset=Point()//輪廓像素的位移(默認沒有位移(0, 0)) )
【報錯問題】
findContours()有時會報告“已觸發了一個斷點”等錯誤,嘗試過y有效的解決方法有:
1.為vector提前申請一定的空間,如
std::vector<std::vector<Point>> contours(500)
2.Debug版切換為Release版;
drawContours繪制輪廓
drawContours( InputOutputArray binImg, // 輸出圖像 OutputArrayOfArrays contours,//找到的全部輪廓對象 Int contourIdx//輪廓索引號 const Scalar & color,//繪制顏色 int thickness,//繪制線寬 int lineType ,//線的類型(默認8) InputArray hierarchy,//拓撲結構圖 int maxlevel,//最大層數(0只繪制當前的,1表示繪制繪制當前及其內嵌的輪廓) Point offset=Point()//輪廓位移 )
示例:
#include<opencv2/opencv.hpp> using namespace cv; int main() { Mat src,dst; src = imread("E:/image/image/shape.jpg"); if(src.empty()) { printf("can not load image \n"); return -1; } namedWindow("input", CV_WINDOW_AUTOSIZE); imshow("input", src); dst = Mat::zeros(src.size(), CV_8UC3); blur(src,src,Size(3,3)); cvtColor(src,src,COLOR_BGR2GRAY); Canny(src, src, 20, 80, 3, false); std::vector<std::vector<Point>> contours; std::vector<Vec4i> hierarchy; findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); RNG rng(0); for(int i = 0; i < contours.size(); i++) { Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255)); drawContours(dst, contours, i, color, 2, 8, hierarchy, 0, Point(0,0)); } namedWindow("output", CV_WINDOW_AUTOSIZE); imshow("output",dst); waitKey(); return 0; }
使用opencv3時(測試用opencv3.1.0)發現,cv命名空間下沒有了vector,而在opencv2中(測試用opencv2.4.10)還存在。后查看各自的頭文件發現:
opencv.hpp頭文件中包含著core.hpp(#include “opencv2/core.hpp”);
而在opencv2的core.hpp中包含有
........ #include <map> #include <new> #include <string> #include <vector> .......
等頭文件,但在opencv3的core.hpp中刪去這些包含項。
因此在使用opencv3時cv命名空間下沒有了vector。
使用opencv2.4.10時可以寫:
#include<opencv2/opencv.hpp> using namespace cv; int main() { Mat src,dst; src = imread("E:/image/image/shape.jpg"); if(src.empty()) { printf("can not load image \n"); return -1; } namedWindow("input", CV_WINDOW_AUTOSIZE); imshow("input", src); dst = Mat::zeros(src.size(), CV_8UC3); blur(src,src,Size(5,5)); Canny(src, src, 20, 80, 3, false); vector<vector<Point>>contours; vector<Vec4i> hierarchy; findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); RNG rng(0); for(int i = 0; i < contours.size(); i++) { Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255)); drawContours(dst, contours, i, color, 2, 8, hierarchy, 0, Point(0,0)); } namedWindow("output", CV_WINDOW_AUTOSIZE); imshow("output",dst); waitKey(); return 0; }
繪制輪廓外矩形框
常用繪制輪廓外形狀的函數:
cv::boundingRect(InputArray points)繪制一個矩形(輪廓周圍最小矩形左上角點和右下角點)
cv::minAreaRect(InputArray points)繪制輪廓周圍最小旋轉矩形
cv::minEnclosingCircle(InputArray points, Point2f& center, float& radius)//繪制輪廓周圍最小圓形
cv::fitEllipse(InputArray points)繪制輪廓周圍最小橢圓
繪制輪廓外矩形框:
#include<opencv2/opencv.hpp> using namespace cv; int main() { Mat src,dst; src = imread("E:/image/shape.jpg"); if(src.empty()) { printf("can not load image \n"); return -1; } namedWindow("input", CV_WINDOW_AUTOSIZE); imshow("input", src); dst = Mat::zeros(src.size(), CV_8UC3); std::vector<std::vector<Point>>contours; std::vector<Vec4i> hierarchy; blur(src,src,Size(3,3)); cvtColor(src,src,COLOR_BGR2GRAY); Canny(src, src, 20, 80, 3, false); findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); RNG rng(0); std::vector<std::vector<Point>> contoursPloy(contours.size()); std::vector<RotatedRect> minRects(contours.size()); for(int i = 0; i < contours.size(); i++) { minRects[i] = minAreaRect(Mat(contours[i])); Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255)); drawContours(dst, contoursPloy, i, color, 1,8,std::vector<Vec4i>(), 0, Point(0, 0)); Point2f rectPoints[4]; minRects[i].points(rectPoints); for (int j = 0; j < 4; j++) { line(dst, rectPoints[j], rectPoints[(j+1)%4], color, 1, 8, 0); } } namedWindow("output", CV_WINDOW_AUTOSIZE); imshow("output",dst); waitKey(); return 0; }
輪廓篩選
moments( InputArray array,//輸入數據 bool binaryImage=false //是否為二值圖像 ) contourArea( InputArray contour,//輸入輪廓數據 bool oriented//返回絕對值(默認false) ) arcLength( InputArray curve,//輸入輪廓 bool closed// 輪廓否是封閉曲線 )
輪廓篩選示例:
使用輪廓的面積和長度特征對輪廓進行篩選后用外接矩形將篩選后的輪廓框選出來。
#include<opencv2/opencv.hpp> using namespace cv; void trackBar(int,void*); Mat src,dst; std::vector<std::vector<Point>>contours; int area = 0, length = 0; int main() { src = imread("E:/image/shape.jpg"); if(src.empty()) { printf("can not load image \n"); return -1; } namedWindow("input", CV_WINDOW_AUTOSIZE); imshow("input", src); dst = Mat::zeros(src.size(), CV_8UC3); std::vector<Vec4i> hierarchy; blur(src,dst,Size(3,3)); cvtColor(dst,dst,COLOR_BGR2GRAY); Canny(dst, dst, 20, 80, 3, false); findContours(dst, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); namedWindow("output", CV_WINDOW_AUTOSIZE); createTrackbar("area:", "output", &area,150,trackBar); createTrackbar("length:", "output", &length,150,trackBar); waitKey(); return 0; } void trackBar(int,void*) { Mat src1 = src.clone(); RNG rng(0); std::vector<std::vector<Point>> contoursPloy(contours.size()); std::vector<RotatedRect> minRects(contours.size()); for(int i = 0; i < contours.size(); i++) { if(contourArea(contours[i]) > area && arcLength(contours[i], false) > length) { minRects[i] = minAreaRect(Mat(contours[i])); Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255)); //drawContours(dst, contoursPloy, i, color, 1,8,vector<Vec4i>(), 0, Point(0, 0)); Point2f rectPoints[4]; minRects[i].points(rectPoints); for (int j = 0; j < 4; j++) { line(src1, rectPoints[j], rectPoints[(j+1)%4], color, 2, 8, 0); } } } imshow("output",src1); src1 = src; }
以上這篇opencv3/C++輪廓的提取與篩選方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。