提取电容元件

// CV_IP.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <stdio.h>

using namespace cv;
using namespace std;


//全局变量声明
Mat g_srcImage, g_grayImage;
int g_nThresh = 160;
int g_nThresh_max = 255;
RNG g_rng(12345);
//设置颜色
Scalar color = Scalar(0, 255, 0);
//全局函数声明
void on_COntoursChange(int, void *);

//主函数
int main()
{
	//载入源图像
	g_srcImage = imread("G:\\picture\\cv\\sample1.bmp");
	if (!g_srcImage.data) { printf("读取源图像srcImage错误~!\n"); return false; }

	//将原图像灰度化并进行平滑降噪
	cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY);
	blur(g_grayImage, g_grayImage, Size(3, 3));

	//创建原始图窗口以及阈值滑动条
	namedWindow("image[origin]");
	imshow("image[origin]", g_srcImage);
	createTrackbar("threshold: ", "image[origin]", &g_nThresh, g_nThresh_max, on_COntoursChange);
	on_COntoursChange(0, 0);

	waitKey(0);
	return 0;

}

//回调函数
void on_COntoursChange(int, void *)
{
	//定义一些参数
	Mat threshold_output;
	vector<vector<Point>>contours;
	vector<Vec4i>hierarchy;
	//使用threshold检测边缘
	threshold(g_grayImage, threshold_output, g_nThresh, 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 (unsigned int i = 0; i < contours.size(); ++i)
	{
		approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);//用指定精度3逼近多边形曲线
		boundRect[i] = boundingRect(Mat(contours_poly[i]));//计算点集的最外面(up-right)矩形边界
		minEnclosingCircle(contours_poly[i], center[i], radius[i]);//对给定的2D点集,寻找最小面积的包围圆形
	}
	//绘制包围的矩形框
	Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
	Mat temp;

	
	int cnt = 0;
	for (int i=0; i < contours.size(); ++i)  //标注元件,保存样本
	{
		if (boundRect[i].width < 10||boundRect[i].height<10||boundRect[i].width>100)
			continue;
		
		
		//	temp = g_srcImage(boundRect[i]);
		//	char name[128] = { 0 };
		//	sprintf_s(name, "G:\\picture\\cv\\data\\data%d.jpg", ++cnt);
		//	imwrite(name, temp);
		//drawContours(drawing, contours_poly, i, color, 1, 1, vector<Vec4i>(), 0, Point());
		//标注元件
		rectangle(g_srcImage, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
		


	}
	namedWindow("image[renderings]");
	imshow("image[renderings]", g_srcImage);
	imwrite("G:\\picture\\cv\\temp.bmp", g_srcImage);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值