OpenCV為輪廓?jiǎng)?chuàng)建邊界框和圓

2018-09-27 11:35 更新

目標(biāo)

在本教程中,您將學(xué)習(xí)如何:

Code

本教程代碼如下所示。您也可以從這里下載


#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
void thresh_callback(int, void* );
int main( int, char** argv )
{
  src = imread( argv[1], IMREAD_COLOR );
  cvtColor( src, src_gray, COLOR_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );
  const char* source_window = "Source";
  namedWindow( source_window, WINDOW_AUTOSIZE );
  imshow( source_window, src );
  createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
  thresh_callback( 0, 0 );
  waitKey(0);
  return(0);
}
void thresh_callback(int, void* )
{
  Mat threshold_output;
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;
  threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
  findContours( threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
  vector<vector<Point> > contours_poly( contours.size() );
  vector<Rect> boundRect( contours.size() );
  vector<Point2f>center( contours.size() );
  vector<float>radius( contours.size() );
  for( size_t i = 0; i < contours.size(); i++ )
  {
    approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
    boundRect[i] = boundingRect( Mat(contours_poly[i]) );
    minEnclosingCircle( contours_poly[i], center[i], radius[i] );
  }
  Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
  for( size_t i = 0; i< contours.size(); i++ )
  {
    Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
    drawContours( drawing, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
    rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
    circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
  }
  namedWindow( "Contours", WINDOW_AUTOSIZE );
  imshow( "Contours", drawing );
}

說(shuō)明

主要功能相當(dāng)簡(jiǎn)單,如下所述:

  1. 打開(kāi)圖像,將其轉(zhuǎn)換為灰度,并將其模糊以擺脫噪點(diǎn)。
      src = imread( argv[1], IMREAD_COLOR );
      cvtColor( src, src_gray, COLOR_BGR2GRAY );
      blur( src_gray, src_gray, Size(3,3) );
  2. 創(chuàng)建一個(gè)標(biāo)題為“Source”的窗口,并在其中顯示源文件。 
      const char* source_window = "Source";
      namedWindow( source_window, WINDOW_AUTOSIZE );
      imshow( source_window, src );
  3. 在source_window上創(chuàng)建一個(gè)跟蹤欄,并為其分配一個(gè)回調(diào)函數(shù)。一般來(lái)說(shuō),回調(diào)函數(shù)用于對(duì)某種信號(hào)做出反應(yīng),在我們的例子中它是跟蹤欄的狀態(tài)變化。 
      createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
    
  4. 顯式的一次性電話(huà)thresh_callback是必要的同時(shí)顯示“輪廓”窗口與“源”窗口。
      thresh_callback( 0, 0 );
    
  5. 等待用戶(hù)關(guān)閉窗口。
      waitKey(0);
    

回調(diào)函數(shù)thresh_callback執(zhí)行所有有趣的工作。

  • 寫(xiě)入threshold_output灰度圖片的閾值(您可以在這里查看閾值)。
  threshold(src_gray,threshold_output,thresh,255,THRESH_BINARY);

  • 找到輪廓并將其保存到向量contourhierarchy。

  findContours(threshold_output,contour,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point(0,0));

  • 對(duì)于每個(gè)找到的輪廓,我們現(xiàn)在將逼近逼近具有精度±3的多邊形,并表示曲線(xiàn)必須關(guān)閉。

之后,我們?yōu)槊總€(gè)多邊形找到一個(gè)邊界,并將其保存boundRect。

最后,我們發(fā)現(xiàn)每一個(gè)多邊形的最小封閉圈,并保存到center和radius載體

  for( size_t i = 0; i < contours.size(); i++ )
  {
    approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
    boundRect[i] = boundingRect( Mat(contours_poly[i]) );
    minEnclosingCircle( contours_poly[i], center[i], radius[i] );
  }

我們發(fā)現(xiàn)了我們需要的一切,我們所要做的就是繪制。

  • 創(chuàng)建新的無(wú)符號(hào)8位字符的Mat,填充零。它將包含我們要制作的所有圖紙(直角和圓圈)。

  Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
  • 對(duì)于每個(gè)輪廓:選擇隨機(jī)顏色,繪制輪廓,邊界矩形和最小包圍圓,
  for( size_t i = 0; i< contours.size(); i++ )
  {
    Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
    drawContours( drawing, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
    rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
    circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
  }

  • 顯示結(jié)果:創(chuàng)建一個(gè)新窗口“輪廓”,并顯示我們添加到圖紙上的所有內(nèi)容。

  namedWindow( "Contours", WINDOW_AUTOSIZE );
  imshow( "Contours", drawing );

結(jié)果

這里是:

OpenCV為輪廓?jiǎng)?chuàng)建邊界框和圓

OpenCV為輪廓?jiǎng)?chuàng)建邊界框和圓

以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)