OpenCV求取轮廓线

// Threshold.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"


#include <cv.h>
#include <highgui.h>
int g_threshold = 100;
IplImage* img1= NULL;
IplImage* g_gray = NULL;
CvMemStorage* g_storage = NULL;
void on_trackbar(int)
{
	
	
	//将RGB转化为灰度图像
	cvCvtColor(img1, g_gray, CV_BGR2GRAY);
	
	//设定阈值为 g_threshold
    cvThreshold(g_gray, g_gray,  g_threshold, 255, CV_THRESH_BINARY);  

	
	//用来创建一个内存存储器,来统一管理各种动态对象的内存。比方说序列,这个函数返回一个新创建的内存存储器指针。
	//參数block_size相应内存器中每一个内存块的大小,为0时内存块默认大小为64k(没设过大小,一直用的是默认0)。

g_storage = cvCreateMemStorage(0); CvSeq* contours = 0; //默认mode为list,method设定轮廓近似的方法 cvFindContours(g_gray, g_storage, &contours); cvZero(g_gray); if (contours) { //maxlevel为1,就画同层的轮廓 cvDrawContours(g_gray, contours, cvScalarAll(255),cvScalarAll(255), 1); } //释放内存资源 cvReleaseMemStorage(&g_storage); cvShowImage("Countours", g_gray); cvWaitKey(0); } void main() { //以原始通道数读取图片1 img1 = cvLoadImage("D://vc6.0//MSDev98//MyProjects//MachineVision//TestPic//PeppersRGB.bmp",CV_LOAD_IMAGE_UNCHANGED); g_gray = cvCreateImage(cvGetSize(img1), 8, 1); //命名窗体 cvNamedWindow("Countours", CV_WINDOW_AUTOSIZE); cvCreateTrackbar("Threshold", "Countours", &g_threshold, 100, on_trackbar); on_trackbar(0); cvWaitKey(0); cvDestroyAllWindows(); }


本人对这一章学习兴趣不大。大致过了一下

原文地址:https://www.cnblogs.com/mthoutai/p/6761016.html