OpenCV >> C/C++

contours


參考資訊:
1. examples
2. findcontours
3. drawcontours

prototype

void findContours(
  InputOutputArray image, 
  OutputArrayOfArrays contours, 
  int mode, 
  int method, 
  Point offset=Point());

void drawContours(
  InputOutputArray image, 
  InputArrayOfArrays contours, 
  int contourIdx, 
  const Scalar& color, 
  int thickness=1, 
  int lineType=8, 
  InputArray hierarchy=noArray(), 
  int maxLevel=INT_MAX, Point offset=Point());

main.cpp

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <math.h>
#include <iostream>
 
using namespace cv;
using namespace std;
 
int main(int argc, char** argv)
{
  vector<vector<Point>> contours;

  Mat img = imread("main.jpg", CV_8UC1);
  if(img.empty()){
    printf("failed to load image\n");
    return -1;
  }
  namedWindow("image", 1);
  imshow("image", img);

  findContours(img, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);

  Mat cnt_img = Mat::zeros(250, 250, CV_8UC3);
  drawContours(cnt_img, contours, -1, Scalar(128, 255, 255));
  namedWindow("contours", 1);
  imshow("contours", cnt_img);
  waitKey();
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(main)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})

編譯並且執行

$ cmake .
$ make
$ ./main

完成


返回上一頁