欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

opencv 查找連通區(qū)域 最大面積實(shí)例

 更新時間:2020年06月04日 15:04:55   作者:楊宗衛(wèi)的爸爸  
這篇文章主要介紹了opencv 查找連通區(qū)域 最大面積實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

今天在弄一個查找連通的最大面積的問題。

要把圖像弄成黑底,白字,這樣才可以正確找到。

然后調(diào)用下邊的方法:

RETR_CCOMP:提取所有輪廓,并將輪廓組織成雙層結(jié)構(gòu)(two-level hierarchy),頂層為連通域的外圍邊界,次層位內(nèi)層邊界

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
 
using namespace cv;
using namespace std;
 
int main( int argc, char** argv )
{
  Mat src = imread( argv[1] );
 
  int largest_area=0;
  int largest_contour_index=0;
  Rect bounding_rect;
 
  Mat thr;
  cvtColor( src, thr, COLOR_BGR2GRAY ); //Convert to gray
  threshold( thr, thr, 125, 255, THRESH_BINARY ); //Threshold the gray
  bitwise_not(thr,thr); //這里先變反轉(zhuǎn)顏色
 
  vector<vector<Point> > contours; // Vector for storing contours
 
  findContours( thr, contours, RETR_CCOMP, CHAIN_APPROX_SIMPLE ); // Find the contours in the image
 
  for( size_t i = 0; i< contours.size(); i++ ) // iterate through each contour.
  {
    double area = contourArea( contours[i] ); // Find the area of contour
 
    if( area > largest_area )
    {
      largest_area = area;
      largest_contour_index = i;        //Store the index of largest contour
      bounding_rect = boundingRect( contours[i] ); // Find the bounding rectangle for biggest contour
    }
  }
 
  drawContours( src, contours,largest_contour_index, Scalar( 0, 255, 0 ), 2 ); // Draw the largest contour using previously stored index.
 
  imshow( "result", src );
  waitKey();
  return 0;
}

方法二: connectedComponentsWithStats

 std::pair< int , int > MaxAreaFromSource(Mat srcImage, Mat &dstImage, int index)
{
  /*
  vector<vector<cv::Point> > contours; // Vector for storing contours
  
  int largest_area=0;
  size_t largest_contour_index=0;
  Rect bounding_rect;
  
  findContours( srcImage, contours, RETR_CCOMP, CHAIN_APPROX_SIMPLE ); // Find the contours in the image
  
  for( size_t i = 0; i< contours.size(); i++ ) // iterate through each contour.
  {
    double area = contourArea( contours[i] ); // Find the area of contour
    
    if( area > largest_area )
    {
      largest_area = area;
      largest_contour_index = i;        //Store the index of largest contour
      bounding_rect = boundingRect( contours[i] ); // Find the bounding rectangle for biggest contour
    }
  }
  
  Mat dst;
  cvtColor(srcImage, dst, CV_GRAY2RGB);
  drawContours( dst, contours,largest_contour_index, Scalar( 0, 255, 0 ), 2 ); // Draw the largest contour using previously stored index.
  imshow( "result", dst );
  waitKey();
  
  printf("%%%%%%%%%%%max area:%d\n", largest_area);
  return make_pair( largest_area, index);
  */
  
  cv::Mat img_bool, labels, stats, centroids, img_color, img_gray;
  
  //連通域計算
  int nccomps = cv::connectedComponentsWithStats (
                          srcImage, //二值圖像
                          labels,   //和原圖一樣大的標(biāo)記圖
                          stats, //nccomps×5的矩陣 表示每個連通區(qū)域的外接矩形和面積(pixel)
                          centroids //nccomps×2的矩陣 表示每個連通區(qū)域的質(zhì)心
                          );
  //cv::imshow("labels", labels);
  //cv::waitKey();
  
  vector<cv::Vec3b> colors(nccomps);
  colors[0] = cv::Vec3b(0,0,0); // background pixels remain black.
  
   printf( "index:%d==================\n",index );
  
  vector< int >vec_width,vec_area,vec_height;
  
  for(int label = 1; label < nccomps; ++label)
  {
    colors[label] = cv::Vec3b( (std::rand()&255), (std::rand()&255), (std::rand()&255) );
    std::cout << "Component "<< label << std::endl;
    std::cout << "CC_STAT_LEFT  = " << stats.at<int>(label,cv::CC_STAT_LEFT) << std::endl;
    std::cout << "CC_STAT_TOP  = " << stats.at<int>(label,cv::CC_STAT_TOP) << std::endl;
    std::cout << "CC_STAT_WIDTH = " << stats.at<int>(label,cv::CC_STAT_WIDTH) << std::endl;
    std::cout << "CC_STAT_HEIGHT = " << stats.at<int>(label,cv::CC_STAT_HEIGHT) << std::endl;
    std::cout << "CC_STAT_AREA  = " << stats.at<int>(label,cv::CC_STAT_AREA) << std::endl;
    std::cout << "CENTER  = (" << centroids.at<double>(label, 0) <<","<< centroids.at<double>(label, 1) << ")"<< std::endl << std::endl;
    
    int area = stats.at<int>(label,cv::CC_STAT_AREA);
    int left = stats.at<int>(label,cv::CC_STAT_LEFT);
    int top = stats.at<int>(label,cv::CC_STAT_TOP);
    int width = stats.at<int>(label,cv::CC_STAT_WIDTH);
    int height = stats.at<int>(label,cv::CC_STAT_HEIGHT);
    
    vec_area.push_back(area);
    vec_width.push_back(width);
    vec_height.push_back(height);
  }
  
  vector<int>::iterator bigwidth = std::max_element(std::begin(vec_width), std::end(vec_width));
  vector<int>::iterator bigheight = std::max_element(std::begin(vec_height), std::end(vec_height));
  vector<int>::iterator bigarea = std::max_element(std::begin(vec_area), std::end(vec_area));
  
  //printf( "area:%d------------width:%d height:%d \n", *bigarea, *bigwidth, *bigheight );
  
  //按照label值,對不同的連通域進(jìn)行著色
  img_color = cv::Mat::zeros(srcImage.size(), CV_8UC3);
  for( int y = 0; y < img_color.rows; y++ )
    for( int x = 0; x < img_color.cols; x++ )
    {
      int label = labels.at<int>(y, x);
      CV_Assert(0 <= label && label <= nccomps);
      img_color.at<cv::Vec3b>(y, x) = colors[label];
    }
  
  cv::imshow("color", img_color);
  cv::waitKey();
   
  return make_pair( *bigarea , index );
}

我先用這個函數(shù)實(shí)現(xiàn)了一下,效果正確,還是opencv demo 是正確的,網(wǎng)上找了個例子,害死我了。

說明一下:方法一 比 第二種方法 運(yùn)行速度快很多哦! 這一點(diǎn)很重要。

以上這篇opencv 查找連通區(qū)域 最大面積實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python微醫(yī)掛號網(wǎng)醫(yī)生數(shù)據(jù)抓取

    Python微醫(yī)掛號網(wǎng)醫(yī)生數(shù)據(jù)抓取

    今天小編就為大家分享一篇關(guān)于Python微醫(yī)掛號網(wǎng)醫(yī)生數(shù)據(jù)抓取,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • python遍歷文件夾找出文件夾后綴為py的文件方法

    python遍歷文件夾找出文件夾后綴為py的文件方法

    今天小編就為大家分享一篇python遍歷文件夾找出文件夾后綴為py的文件方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 使用Pandas實(shí)現(xiàn)清洗客戶編碼異常數(shù)據(jù)

    使用Pandas實(shí)現(xiàn)清洗客戶編碼異常數(shù)據(jù)

    在不同行業(yè)中,我們經(jīng)常會遇到一個麻煩的問題:數(shù)據(jù)清洗,尤其是當(dāng)我們需要處理客戶編碼異常數(shù)據(jù)時,下面小編就來和大家分享一下常用的解決辦法吧
    2023-07-07
  • python3中利用filter函數(shù)輸出小于某個數(shù)的所有回文數(shù)實(shí)例

    python3中利用filter函數(shù)輸出小于某個數(shù)的所有回文數(shù)實(shí)例

    今天小編就為大家分享一篇 python3中利用filter函數(shù)輸出小于某個數(shù)的所有回文數(shù)實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python中start和run方法的區(qū)別

    python中start和run方法的區(qū)別

    大家好,本篇文章主要講的是python中start和run方法的區(qū)別,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Python中的迭代器詳解

    Python中的迭代器詳解

    這篇文章主要介紹迭代器,看完文章你可以了解到什么是可迭代對象、啥是迭代器、如何自定義迭代器、使用迭代器的優(yōu)勢,文中有詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-08-08
  • python爬蟲開發(fā)之使用Python爬蟲庫requests多線程抓取貓眼電影TOP100實(shí)例

    python爬蟲開發(fā)之使用Python爬蟲庫requests多線程抓取貓眼電影TOP100實(shí)例

    這篇文章主要介紹了python爬蟲開發(fā)之使用Python爬蟲庫requests多線程抓取貓眼電影TOP100實(shí)例,需要的朋友可以參考下
    2020-03-03
  • python解釋器spython使用及原理解析

    python解釋器spython使用及原理解析

    這篇文章主要介紹了python解釋器spython使用及原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • 跟老齊學(xué)Python之讓人歡喜讓人憂的迭代

    跟老齊學(xué)Python之讓人歡喜讓人憂的迭代

    跟一些比較牛X的程序員交流,經(jīng)常聽到他們嘴里冒出一個不標(biāo)準(zhǔn)的英文單詞,而loop、iterate、traversal和recursion如果不在其內(nèi),總覺得他還不夠牛X。當(dāng)讓,真正牛X的絕對不會這么說的,他們只是說“循環(huán)、迭代、遍歷、遞歸”,然后再問“這個你懂嗎?”。
    2014-10-10
  • 如何在Python項(xiàng)目中引入日志

    如何在Python項(xiàng)目中引入日志

    在開發(fā)一些大型項(xiàng)目的時候,都會使用日志來記錄項(xiàng)目運(yùn)行時產(chǎn)生的信息,以備出錯時定位分析和從日志信息中提取數(shù)據(jù)統(tǒng)計分析等。在 Python 中使用 logging 內(nèi)置模塊即可對項(xiàng)目進(jìn)行日志的配置。
    2021-05-05

最新評論