查找輪廓什么是輪廓:一個輪廓是由圖像中的一系列點(diǎn)組成的,,也就是圖像中的一條曲線。在OpenCV中一般用序列來存儲輪廓信息,。序列中的每個元素是曲線中每個點(diǎn)的位置,。 關(guān)于序列:序列是內(nèi)存存儲器中可以存儲的一種對象,序列是某種結(jié)構(gòu)的鏈表,。 下面是序列結(jié)構(gòu)體: typedef sturct CvSeq{
int flags;
int header_size;
CvSeq * h_prev;
CvSeq * h_next;
CvSeq * v_prev;
CvSeq * v_next;
int total;
int elem_size;
char *bolck_max;
char * ptr;
int delta_elems;
CvMemStorage * storage;
CvSeqBlock * free_blocks;
CvSeqBlock * first;
}
Freeman編碼 在Freeman鏈碼中,,多邊形被表示成一系列的位移,每一個位移都有8個方向,,這8個方向從0到7表示,。
2. 相關(guān)APIfindContours()函數(shù)來尋找圖像中物體的輪廓,并結(jié)合drawContours()函數(shù)將找到的輪廓繪制出,。 void cv::findContours ( InputOutputArray image,
OutputArrayOfArrays contours,
OutputArray hierarchy,
int mode,
int method,
Point offset = Point()
)
參數(shù)解釋: image:輸入圖像,,圖像必須為8-bit單通道圖像,圖像中的非零像素將被視為1,,0像素是0,,加載圖像后自動轉(zhuǎn)化為二值化圖像。我們同樣可以使用cv::compare,cv::inRange,cv::threshold,cv::adaptiveThreshold,cv::Canny等函數(shù)來創(chuàng)建二值圖像,。 contours 檢測到的輪廓,,每個輪廓都是以點(diǎn)向量的形式進(jìn)行存儲即使用point類型的vector表示。 hierarchy 可選的輸出向量包括了圖像的拓?fù)湫畔ⅰ?/p> mode 輪廓檢索模式 method 輪廓近似方法 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()
)
參數(shù)解釋: image 輸入Mat類型圖像 contours 檢測到的輪廓數(shù)據(jù),。 contourldx 繪制輪廓的只是變量,,如果為負(fù)值則在輪廓內(nèi)部繪制。 line type 線條類型 LINE_4 4-connected line LINE_8 8-connected line LINE_AA antialiased line hierarchy 可選層次結(jié)構(gòu) maxLevel 用于繪制輪廓的最大等級 offset 可選輪廓偏置參數(shù),,制定偏移量offset=(dx, dy)給出繪制輪廓的偏移量 3. 示例代碼:#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <stdlib.h>
using namespace std;
using namespace cv;
int main()
{
Mat src,dst,gray_img;
src = imread("1.jpg");
if(src.empty())
{
cout<<"圖像加載失敗";
waitKey(0);
return -1;
}
else
cout<<"圖像加載成功";
namedWindow("1",WINDOW_AUTOSIZE);
imshow("1",src);
cvtColor(src,gray_img,CV_BGR2GRAY);
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
gray_img = gray_img > 100;
findContours(gray_img,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
//繪制輪廓圖
dst = Mat::zeros(src.size(), CV_8UC3);
for (int i = 0; i < hierarchy.size(); i++)
{
Scalar color = Scalar(rand() % 255, rand() % 255, rand() % 255);
drawContours(dst, contours, i, color, CV_FILLED, 8, hierarchy);
}
imshow("輪廓圖", dst);
waitKey(0);
waitKey(0);
return 0;
}
————————————————
版權(quán)聲明:本文為CSDN博主「南山二毛」的原創(chuàng)文章,,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_16481211/article/details/79640703
———————————————— 版權(quán)聲明:本文為CSDN博主「南山二毛」的原創(chuàng)文章,,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,,轉(zhuǎn)載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/qq_16481211/article/details/79640703 #轉(zhuǎn)載請注明出處 www.skcircle.com 《少有人走的路》勇哥的工業(yè)自動化技術(shù)網(wǎng)站,。如果需要本貼圖片源碼等資源,,請向勇哥索取,。 掃碼加本站公眾號
|