• Open Source Computer Vision Library

轮廓(contour)检测

Wikipedia,自由的百科全书

来自于仕琪的讲稿《使用OpenCV进行图像处理》中的例程

/**************************************************
 * 轮廓检测
 * 主要函数:
 *      cvFindContours
 *      cvDrawContours
 **************************************************/
 
/***********************************************************************
 * OpenCV example
 * By Shiqi Yu 2006
 ***********************************************************************/
 
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
 
int main( int argc, char** argv )
{
  //声明IplImage指针
  IplImage* pImg = NULL; 
  IplImage* pContourImg = NULL;
 
 
  CvMemStorage * storage = cvCreateMemStorage(0);
  CvSeq * contour = 0;
  int mode = CV_RETR_EXTERNAL;
 
  if( argc == 3)
      if(strcmp(argv[2], "all") == 0)
	mode = CV_RETR_CCOMP; //内外轮廓都检测
 
 
  //创建窗口
  cvNamedWindow("src", 1);
  cvNamedWindow("contour",1);
 
 
  //载入图像,强制转化为Gray
  if( argc >= 2 && 
      (pImg = cvLoadImage( argv[1], 0)) != 0 )
    {
 
      cvShowImage( "src", pImg );
 
      //为轮廓显示图像申请空间
      //3通道图像,以便用彩色显示
      pContourImg = cvCreateImage(cvGetSize(pImg),
					  IPL_DEPTH_8U,
					  3);
      //copy source image and convert it to BGR image
      cvCvtColor(pImg, pContourImg, CV_GRAY2BGR);
 
 
      //查找contour
      cvFindContours( pImg, storage, &contour, sizeof(CvContour), 
		  mode, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
 
    }
  else
    {
      //销毁窗口
      cvDestroyWindow( "src" );
      cvDestroyWindow( "contour" );
      cvReleaseMemStorage(&storage);
 
      return -1;
    }
 
 
 
 
  //将轮廓画出    
  cvDrawContours(pContourImg, contour, 
		 CV_RGB(0,0,255), CV_RGB(255, 0, 0), 
		 2, 2, 8, cvPoint(0,0));
  //显示图像
  cvShowImage( "contour", pContourImg );
 
  cvWaitKey(0);
 
 
  //销毁窗口
  cvDestroyWindow( "src" );
  cvDestroyWindow( "contour" );
  //释放图像
  cvReleaseImage( &pImg ); 
  cvReleaseImage( &pContourImg ); 
 
  cvReleaseMemStorage(&storage);
 
  return 0;
}

Python版本

# -*- coding:utf-8 -*-
###########################################################
# OpenCV example
#
# 轮廓检测
#
# 主要函数:cvFindContours, cvDrawContours
#
# By ChaiShushan 2008
###########################################################
 
import sys
 
# 导入OpenCV模块
 
from opencv.cv import *
from opencv.highgui import *
 
if __name__ == '__main__':
 
    # 声明IplImage指针
 
    pImg = None;
    pContourImg = None;
 
    storage = cvCreateMemStorage(0);
    contour = None;
    mode = CV_RETR_EXTERNAL;
 
    if len(sys.argv) == 3 and sys.argv[2] == "all":
        mode = CV_RETR_CCOMP
 
    # 创建窗口
 
    cvNamedWindow("src", 1)
    cvNamedWindow("contour",1)
 
    # 载入图像,强制转化为Gray
 
    if len(sys.argv) >= 2:
 
        pImg = cvLoadImage(sys.argv[1], 0)
        if not pImg: sys.exit(-1)
 
        cvShowImage( "src", pImg );
 
        # 为轮廓显示图像申请空间
        # 3通道图像,以便用彩色显示
 
        pContourImg = cvCreateImage(cvGetSize(pImg),IPL_DEPTH_8U,3);
 
        # copy source image and convert it to BGR image
 
        cvCvtColor(pImg, pContourImg, CV_GRAY2BGR);
 
        # 查找contour
 
        n, contour = cvFindContours( pImg, storage, sizeof_CvContour,
            mode, CV_CHAIN_APPROX_SIMPLE);
 
    else:
 
        # 销毁窗口
 
        cvDestroyWindow( "src" );
        cvDestroyWindow( "contour" );
        cvReleaseMemStorage(storage);
        sys.exit(-1)
 
    # 将轮廓画出
 
    cvDrawContours(pContourImg, contour, CV_RGB(255,0,0), CV_RGB(0, 0, 255),
        2, 2, 8);
 
    # 显示图像
 
    cvShowImage( "contour", pContourImg );
    cvWaitKey(0);
 
    # 销毁窗口
 
    cvDestroyWindow( "src" );
    cvDestroyWindow( "contour" );
 
    # 释放图像
 
    cvReleaseImage( pImg );
    cvReleaseImage( pContourImg );
 
    cvReleaseMemStorage(storage);
 
    sys.exit(0)
注: Python版本由chai2010改写.

Python2.7-OpenCV2.2版本

'''
Created on 2011-8-18
 
@author: Sunny
'''
#import cv module
import cv
import sys
 
if __name__ == '__main__':
    # Declare the IplImage
    pImg = None;
    pContourImg = None;
 
    storage = cv.CreateMemStorage(0);
    contour = None;
    mode = cv.CV_RETR_EXTERNAL;    
 
    # Create Windows
 
    cv.NamedWindow("src", 1)
    cv.NamedWindow("contour",1)
 
    # Load Image, Convert to Gray by force
 
    pImg = cv.LoadImage("image.jpg", 0) # A image in the same directory of the file.
 
    cv.ShowImage( "src", pImg );
 
    # Apply the Memory Storage for Contour Image.        
 
    pContourImg = cv.CreateImage(cv.GetSize(pImg),cv.IPL_DEPTH_8U,3);
 
    # copy source image and convert it to BGR image
 
    cv.CvtColor(pImg, pContourImg, cv.CV_GRAY2BGR);
 
    # Find contours
 
    contour = cv.FindContours( pImg, storage, cv.CV_RETR_CCOMP,
        cv.CV_CHAIN_APPROX_SIMPLE); 
 
    # Draw the Contours
 
    cv.DrawContours(pContourImg, contour, cv.CV_RGB(255,0,0), cv.CV_RGB(0, 0, 255),
        2, 2, 8);
 
    # Show Image
 
    cv.ShowImage( "contour", pContourImg );
    cv.WaitKey(0);
 
    # Destory Windows
 
    cv.DestroyWindow( "src" );
    cv.DestroyWindow( "contour" );
 
    # Python2.7-OpenCV2.2 will Release Image MemStorage Automatically
 
    #cv.ReleaseImage( pImg );
    #cv.ReleaseImage( pContourImg );
 
    #cv.ReleaseMemStorage(storage);
 
    sys.exit(0)
注: Python2.7-OpenCV2.2版本由sunny2020改写.
Views
Personal tools