Opencv實現(xiàn)對象提取與測量
更新時間:2019年05月21日 10:54:17 作者:東城青年
這篇文章主要為大家詳細介紹了基于Opencv實現(xiàn)對象提取與測量,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Opencv3實現(xiàn)對象提取與測量的具體代碼,供大家參考,具體內(nèi)容如下
案例背景:下圖為一張衛(wèi)星拍攝的圖片,要獲取其中島嶼的周長和面積

方案思路:高斯模糊去噪,灰度二值化提取輪廓,閉操作填充縫隙 或小的孔洞,尋找輪廓,通過輪廓特征選擇輪廓
#include<opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main(int arc, char** argv) {
Mat src = imread("1.jpg");
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
//該高斯模糊去噪
GaussianBlur(src, src, Size(15, 15), 0, 0);
imshow("output1", src);
//灰度二值化
Mat gray,binary;
cvtColor(src, gray, CV_BGR2GRAY);
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_TRIANGLE);
imshow("output2", binary);
//閉操作
Mat kernel = getStructuringElement(MORPH_RECT, Size(4, 4));
morphologyEx(binary, binary, MORPH_CLOSE, kernel);
imshow("output3", binary);
//尋找輪廓
vector<vector<Point>>contours;
Mat draw = Mat::zeros(src.size(), CV_8UC3);
findContours(binary, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
Rect rect = boundingRect(contours[i]);
if (rect.width < src.cols / 2 || rect.height>src.rows-20)continue;//篩選輪廓
drawContours(draw, contours, i, Scalar(0, 0, 255), 1);
printf("area:%f\n", contourArea(contours[i]));
printf("length:%f\n",arcLength(contours[i],true));
}
imshow("output4", draw);
waitKey(0);
return 0;
}

原圖像

高斯模糊

二值化

閉操作

效果圖
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
c++中拷貝構造函數(shù)的參數(shù)類型必須是引用
如果拷貝構造函數(shù)中的參數(shù)不是一個引用,即形如CClass(const CClass c_class),那么就相當于采用了傳值的方式(pass-by-value),而傳值的方式會調(diào)用該類的拷貝構造函數(shù),從而造成無窮遞歸地調(diào)用拷貝構造函數(shù)。因此拷貝構造函數(shù)的參數(shù)必須是一個引用2013-07-07

