标签:
好久没有更新了,原谅自己放了个假最近又在赶进度,所以。。。更新的内容是很靠后的第八章,因为最近工作要用就先跳了,后面会更新笔记编号。。。加油加油!
在二值图像中寻找轮廓
void cv::findContours ( InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset = Point() )
绘制轮廓
void cv::drawContours ( InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar & color, int thickness = 1, int lineType = LINE_8, InputArray hierarchy = noArray(), int maxLevel = INT_MAX, Point offset = Point() )
事例程序1
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <vector> // main int main( int argc, char** argv ) { // loading image cv::Mat srcImage = cv::imread("1.jpg", 0); imshow("original image", srcImage); // initialize result image cv::Mat dstImage = cv::Mat::zeros(srcImage.rows, srcImage.cols, CV_8UC3); // thresholding image srcImage = srcImage > 119; imshow("thresholding image", srcImage); // finding contours std::vector<std::vector<cv::Point> > contours; std::vector<cv::Vec4i> hierarchy; // for opencv 2 // cv::findContours(srcImage, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // for opencv 3 cv::findContours(srcImage, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE); // iterate through all levels, and draw contours in random color int index = 0; for (; index>=0; index = hierarchy[index][0]) { cv::Scalar color(rand()&255, rand()&255, rand()&255); // for opencv 2 // cv::drawContours(dstImage, contours, index, color, CV_FILLED, 8, hierarchy); // for opencv 3 cv::drawContours(dstImage, contours, index, color, cv::FILLED, 8, hierarchy); imshow("contours", dstImage); cv::waitKey(150); } cv::imwrite("result.jpg", dstImage); return 0; }
1.jpg
result.jpg
事例程序2
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> #include <vector> #define WINDOW_NAME1 "original image" #define WINDOW_NAME2 "contours" // global variables cv::Mat g_srcImage; cv::Mat g_grayImage; cv::Mat g_cannyMat_output; int g_nThresh = 80; int g_nThresh_max = 255; cv::RNG g_rng(12345); std::vector<std::vector<cv::Point> > g_vContours; std::vector<cv::Vec4i> g_vHierarchy; // functions void on_ThreshChange(int, void*); // main int main( int argc, char** argv ) { // change the text color of console system("color 1F"); // loading image g_srcImage = cv::imread("1.jpg", 1); if (!g_srcImage.data){ std::cerr << "ERROR while loading image." << std::endl; return false; } // convert to gray-scale and blur cv::cvtColor(g_srcImage, g_grayImage, cv::COLOR_BGR2GRAY); cv::blur(g_grayImage, g_grayImage, cv::Size(3,3)); // create window cv::namedWindow(WINDOW_NAME1, cv::WINDOW_AUTOSIZE); imshow(WINDOW_NAME1, g_srcImage); // create tracker bar cv::createTrackbar("Canny Threshold", WINDOW_NAME1, &g_nThresh, g_nThresh_max, on_ThreshChange); on_ThreshChange(0, 0); cv::waitKey(0); return 0; } void on_ThreshChange(int, void*) { cv::Canny(g_grayImage, g_cannyMat_output, g_nThresh, g_nThresh*2, 3); cv::findContours(g_cannyMat_output, g_vContours, g_vHierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE); cv::Mat drawing = cv::Mat::zeros(g_cannyMat_output.size(), CV_8UC3); for (int i = 0; i<g_vContours.size(); i++) { cv::Scalar color(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)); cv::drawContours(drawing, g_vContours, i, color, 2, 8, g_vHierarchy); } imshow(WINDOW_NAME2, drawing); }
结果图:
标签:
原文地址:http://www.cnblogs.com/Xiaoyan-Li/p/5761510.html