OpenCV輪廓檢測(cè)之boundingRect繪制矩形邊框
函數(shù)原型
cv::Rect boundingRect( InputArray array );
參數(shù)說(shuō)明
輸入:InputArray類型的array,輸入灰度圖像或二維點(diǎn)集。
輸出:Rect類型的矩形信息,包括矩形尺寸和位置。
測(cè)試代碼
#include <iostream> #include <time.h> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { cv::Mat src = imread("test.png",0); cv::Mat result = src.clone(); cv::Mat th1; // 最大類間差法,也稱大津算法 threshold(result, th1, 0, 255, THRESH_OTSU); // 反相 th1 = 255 - th1; // 確定連通區(qū)輪廓 std::vector<std::vector<cv::Point> > contours; // 創(chuàng)建輪廓容器 std::vector<cv::Vec4i> hierarchy; cv::findContours(th1, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point()); // 遍歷輪廓顯示矩形框 for (int i = 0; i < contours.size(); ++i) { cv::Rect rect = cv::boundingRect(cv::Mat(contours[i])); cv::rectangle(result, rect, Scalar(255), 1); } imshow("original", src); imshow("thresh", th1); imshow("result", result); waitKey(0); return 0; }
測(cè)試效果
?
補(bǔ)充
這個(gè)函數(shù)得到的矩形框都是方正的,還有一個(gè)函數(shù)minAreaRect也可以得到最小包圍矩形框,那個(gè)是帶傾斜角度的。
函數(shù)原型
cv::RotatedRect minAreaRect( InputArray points );
參數(shù)說(shuō)明
輸入:InputArray類型的points,輸入灰度圖像或二維點(diǎn)集。
輸出:RotatedRect類型的旋轉(zhuǎn)矩形信息,即矩形四角點(diǎn)位置。
測(cè)試代碼
#include <iostream> #include <time.h> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { cv::Mat src = imread("test.png",0); cv::Mat result = src.clone(); cv::Mat th1; // 最大類間差法,也稱大津算法 threshold(result, th1, 0, 255, THRESH_OTSU); // 反相 th1 = 255 - th1; // 確定連通區(qū)輪廓 std::vector<std::vector<cv::Point> > contours; // 創(chuàng)建輪廓容器 std::vector<cv::Vec4i> hierarchy; cv::findContours(th1, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point()); // 遍歷輪廓顯示矩形框 for (int i = 0; i < contours.size(); ++i) { cv::RotatedRect rotatedrect = cv::minAreaRect(cv::Mat(contours[i])); // 存儲(chǔ)旋轉(zhuǎn)矩形的四個(gè)點(diǎn) cv::Point2f ps[4]; rotatedrect.points(ps); std::vector<std::vector<cv::Point>> tmpContours; // 創(chuàng)建一個(gè)InputArrayOfArrays 類型的點(diǎn)集 std::vector<cv::Point> contour; for (int i = 0; i != 4; ++i) { contour.emplace_back(cv::Point2i(ps[i])); } // 插入到輪廓容器中 tmpContours.insert(tmpContours.end(), contour); // 繪制輪廓,也就是繪制旋轉(zhuǎn)矩形 drawContours(result, tmpContours, -1, Scalar(0), 1, 16); // 填充mask } imshow("original", src); imshow("thresh", th1); imshow("result", result); waitKey(0); return 0; }
測(cè)試效果:
到此這篇關(guān)于OpenCV輪廓檢測(cè)之boundingRect繪制矩形邊框的文章就介紹到這了,更多相關(guān)OpenCV boundingRect繪制矩形邊框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VC實(shí)現(xiàn)的病毒專殺工具完整實(shí)例
這篇文章主要介紹了VC實(shí)現(xiàn)的病毒專殺工具完整實(shí)例,詳細(xì)講述了針對(duì)病毒的進(jìn)程終止、刪除文件及回復(fù)注冊(cè)表與啟動(dòng)項(xiàng)等,同時(shí)介紹了與之相關(guān)的系統(tǒng)函數(shù),非常具有參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10C語(yǔ)言static修飾函數(shù)詳細(xì)解析
以下是對(duì)C語(yǔ)言中的static修飾函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08C++ 復(fù)制控制之復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn)
所謂的“復(fù)制控制”即通過(guò)這三個(gè)成員函數(shù)控制對(duì)象復(fù)制的過(guò)程,本文主要介紹了C++ 復(fù)制控制之復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11C語(yǔ)言實(shí)現(xiàn)統(tǒng)計(jì)字符串單詞數(shù)
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)統(tǒng)計(jì)字符串單詞數(shù),代碼非常的簡(jiǎn)潔,有需要的小伙伴快來(lái)參考下。2015-03-03Unity3D實(shí)現(xiàn)經(jīng)典小游戲Pacman
這篇文章主要介紹了基于Unity3D制作一做個(gè)經(jīng)典小游戲Pacman,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Unity3D有一定的幫助,感興趣的小伙伴可以了解一下2021-12-12C語(yǔ)言實(shí)現(xiàn)Linux下的socket文件傳輸實(shí)例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)Linux下的socket文件傳輸?shù)姆椒?較為詳細(xì)的分析了C語(yǔ)言文件Socket文件傳輸客戶端與服務(wù)器端相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-06-06C/C++如何實(shí)現(xiàn)循環(huán)左移,循環(huán)右移
這篇文章主要介紹了C/C++如何實(shí)現(xiàn)循環(huán)左移,循環(huán)右移,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07C++和python實(shí)現(xiàn)單鏈表及其原理
這篇文章主要介紹了C++和python實(shí)現(xiàn)單鏈表及其原理,單鏈表是鏈表家族中的一員,每個(gè)節(jié)點(diǎn)依舊由數(shù)據(jù)域(data)和指針域(next)組成,鏈表的具體概念下面文章將詳細(xì)介紹,需要的小伙伴可以參考一下2022-03-03