您好,登錄后才能下訂單哦!
輪廓提取
findContours發(fā)現(xiàn)輪廓
findContours( InputOutputArray binImg, //輸入8bit圖像,0值像素值不變,非0的像素看成1;(變?yōu)槎祱D像) OutputArrayOfArrays contours,//輸出找到的輪廓對(duì)象 OutputArray, hierachy// 圖像的拓?fù)浣Y(jié)構(gòu) int mode, //輪廓返回的模式(RETR_TREE等) int method,//發(fā)現(xiàn)方法(CHAIN_APPROX_SIMPLE等) Point offset=Point()//輪廓像素的位移(默認(rèn)沒有位移(0, 0)) )
【報(bào)錯(cuò)問題】
findContours()有時(shí)會(huì)報(bào)告“已觸發(fā)了一個(gè)斷點(diǎn)”等錯(cuò)誤,嘗試過y有效的解決方法有:
1.為vector提前申請(qǐng)一定的空間,如
std::vector<std::vector<Point>> contours(500)
2.Debug版切換為Release版;
drawContours繪制輪廓
drawContours( InputOutputArray binImg, // 輸出圖像 OutputArrayOfArrays contours,//找到的全部輪廓對(duì)象 Int contourIdx//輪廓索引號(hào) const Scalar & color,//繪制顏色 int thickness,//繪制線寬 int lineType ,//線的類型(默認(rèn)8) InputArray hierarchy,//拓?fù)浣Y(jié)構(gòu)圖 int maxlevel,//最大層數(shù)(0只繪制當(dāng)前的,1表示繪制繪制當(dāng)前及其內(nèi)嵌的輪廓) 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時(shí)(測(cè)試用opencv3.1.0)發(fā)現(xiàn),cv命名空間下沒有了vector,而在opencv2中(測(cè)試用opencv2.4.10)還存在。后查看各自的頭文件發(fā)現(xiàn):
opencv.hpp頭文件中包含著core.hpp(#include “opencv2/core.hpp”);
而在opencv2的core.hpp中包含有
........ #include <map> #include <new> #include <string> #include <vector> .......
等頭文件,但在opencv3的core.hpp中刪去這些包含項(xiàng)。
因此在使用opencv3時(shí)cv命名空間下沒有了vector。
使用opencv2.4.10時(shí)可以寫:
#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; }
繪制輪廓外矩形框
常用繪制輪廓外形狀的函數(shù):
cv::boundingRect(InputArray points)繪制一個(gè)矩形(輪廓周圍最小矩形左上角點(diǎn)和右下角點(diǎn))
cv::minAreaRect(InputArray points)繪制輪廓周圍最小旋轉(zhuǎn)矩形
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,//輸入數(shù)據(jù) bool binaryImage=false //是否為二值圖像 ) contourArea( InputArray contour,//輸入輪廓數(shù)據(jù) bool oriented//返回絕對(duì)值(默認(rèn)false) ) arcLength( InputArray curve,//輸入輪廓 bool closed// 輪廓否是封閉曲線 )
輪廓篩選示例:
使用輪廓的面積和長(zhǎng)度特征對(duì)輪廓進(jìn)行篩選后用外接矩形將篩選后的輪廓框選出來。
#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++輪廓的提取與篩選方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(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)容。