基于的类圆目标自动计数演示程序

图1:原图 图2:阈值化 图3:黄色圆圈标记结果

请点击超练级获取图像地址

实现过程:我对这个演示Demo做个大概介绍,图中圆斑重叠区域较少,因此处理难度也较小,充其量只能做个演示作用;再次,程序中的分割阈值也是几经尝试认为确定的,没有使用自适应阈值化;经观察图中目标的面积特征较为明显,因此最后的结果是根据面积特征得出的(当然目标和背景的颜色差异也是较大的,也可以作为特征使用,本人未做验证)。基本流程如下:

  1. 将图像灰度化;cvtColor()
  2. 对图像进行平滑处理,减少噪声;blur()
  3. 对图像进行阈值化处理;threshold(),效果见图2
  4. 提取图像中目标的轮廓,分析了轮廓的周长和面积,可以的话,可以建立目标面积特征的直方图,可以根据直方图进行自适应选取特征阈值,但是本程序没有采用直方图,而是根据观察做出的阈值。
  5. 根据面积阈值,在原图中标记目标,效果见图3。从图中看,目标基本上都识别出来了,但是也有一些错误识别。
下面是本演示项目的程序:
/*************************************************
 ***matching_test.cpp
 ***Created on: Oct 17, 2010
 ***Author: ethan
 *************************************************/

/*************************************************

**************************************************/
#pragma	warning (disable:4786)
//#pragma	comment ()
/*************************************************/
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"
#include "opencv2/stitching/warpers.hpp"
#include <map>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <Windows.h>
/***命名空间***/
using namespace cv;
using namespace std;
/*************************/

/*************************/
int main( int argc, char** argv )
{
	Mat	src;
	src = imread("test1.jpg");
	resize(src,src,Size(640,480));

	Mat	gray;
	cvtColor(src,gray,CV_BGR2GRAY);
	blur(gray,gray,Size(13,13));
	imshow("Gray Image",gray);

	Mat	thresh;
	threshold(gray,thresh,90,255,THRESH_BINARY);
	imshow("Thresholded Image",thresh);
	imwrite("thresh.jpg",thresh);
	vector<vector<Point>>	contours;
	vector<Vec4i>			hierarchy;
	findContours(thresh,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
	Mat	src_show = src.clone();
	for(int i = 0; i < contours.size(); i++)
	{
		float	length = arcLength(contours[i],true);
		float	area = contourArea(contours[i],false);
		
		if(area > 10 && area < 800)
		{
			drawContours(src_show,contours,i,Scalar(0,236,255));
			cout << "contour[" << i << "] = " << length << endl;
			cout << "Area[" << i << "] = " << area << endl;
			imshow("src_show",src_show);
			waitKey(2000);
		}
	}
	imwrite("result.jpg",src_show);
	waitKey(0);
	system("pause");
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值