Get the center of contour

本文介绍了一种基于OpenCV的图像处理方法,用于计算轮廓的中心并进行校正。通过对图像进行预处理,检测边缘并找到轮廓后,计算每个轮廓的矩来确定其几何中心。该方法适用于图像分析与目标定位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过寻找轮廓的中心,以便对此进行旋转,校正;

计算方法:

 mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 );

即不同方向的矩进行运算;

static int calCoutorCenter(char* Imgname, vector<Point2f> & mc, double minarea = 0, double whRatio = 1);
int main()
{
	char* filename = new char[50];
	strcpy(filename, "../image/rl_4.jpg");
	//getContoursByCplus(filename);
	vector<Point2f> mc;
	calCoutorCenter(filename, mc);
	delete[] filename;
	return 0;
}

API实现转自 opencv ref.

static int calCoutorCenter(char* Imgname, vector<Point2f> & mc, double minarea, double whRatio)
{
	RNG rng(12345);
	cv::Mat src, dst, canny_output;
	/// Load source image and convert it to gray
	src = imread(Imgname, 0);

	if (!src.data)
	{
		std::cout << "read data error!" << std::endl;
		return -1;
	}
	blur(src, src, Size(3, 3));


	//the pram. for findContours,
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;

	/// Detect edges using canny
	Canny(src, canny_output, 80, 255, 3);
	/// Find contours
	findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
	//CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE

	double maxarea = 0;
	int maxAreaIdx = 0;

	for (int i = 0; i<contours.size(); i++)
	{
		double tmparea = fabs(contourArea(contours[i]));
		if (tmparea>maxarea)
		{
			maxarea = tmparea;
			maxAreaIdx = i;
			continue;
		}

		if (tmparea < minarea)
		{
			//删除面积小于设定值的轮廓
			contours.erase(contours.begin() + i);
			std::wcout << "delete a small area" << std::endl;
			continue;
		}
		//计算轮廓的直径宽高
		Rect aRect = boundingRect(contours[i]);
		if ((aRect.width / aRect.height)<whRatio)
		{
			//删除宽高比例小于设定值的轮廓
			contours.erase(contours.begin() + i);
			std::wcout << "delete a unnomalRatio area" << std::endl;
			continue;
		}
	}
	/// Get the moments
	vector<Moments> mu(contours.size());
	for (int i = 0; i < contours.size(); i++)
	{
		mu[i] = moments(contours[i], false);
	}

	///  Get the mass centers:
	vector<Point2f> mc(contours.size());
	for (int i = 0; i < contours.size(); i++)
	{
		mc[i] = Point2f(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
		std::cout << "mc[" << i << "]=" << mc[i].x << "," << mc[i].y << std::endl;
	}
	/// Draw contours,彩色轮廓
	dst = Mat::zeros(canny_output.size(), CV_8UC3);
	for (int i = 0; i< contours.size(); i++)
	{
		//随机颜色
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		drawContours(dst, contours, i, color, 2, 8, hierarchy, 0, Point());
		circle(dst, mc[i], 4, color, -1, 8, 0);
	}
	// Create Window
	char* source_window = "countors";
	namedWindow(source_window, CV_WINDOW_NORMAL);
	imshow(source_window, dst);
	imwrite("dst.jpg", dst);
	cv:; waitKey(0);

	return 0;
}


* * Matching 02: ************************************************ * Matching 02: BEGIN of generated code for model initialization * Matching 02: ************************************************ * * Matching 02: Obtain the model image read_image (Image, 'C:/Users/19841/Desktop/B22031711张彬洋/1.png') * * Matching 02: Build the ROI from basic regions gen_ellipse (ModelRegion, 364.766, 536.091, rad(-11.8461), 70.2011, 13.1313) * * Matching 02: Reduce the model template reduce_domain (Image, ModelRegion, TemplateImage) * * Matching 02: Create and train the shape model create_generic_shape_model (ModelID) * Matching 02: set the model parameters set_generic_shape_model_param (ModelID, 'metric', 'use_polarity') train_generic_shape_model (TemplateImage, ModelID) * * Matching 02: Get the model contour for transforming it later into the image get_shape_model_contours (ModelContours, ModelID, 1) * * Matching 02: Support for displaying the model * Matching 02: Get the reference position area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn) vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D) affine_trans_contour_xld (ModelContours, TransContours, HomMat2D) * * Matching 02: Display the model contours dev_display (Image) dev_set_color ('green') dev_set_draw ('margin') dev_display (ModelRegion) dev_display (TransContours) stop () * * Matching 02: END of generated code for model initialization * Matching 02: * * * * * * * * * * * * * * * * * * * * * * * * Matching 02: BEGIN of generated code for model application * Matching 02: Set the search paramaters set_generic_shape_model_param (ModelID, 'border_shape_models', 'false') * Matching 02: The following operations are usually moved into * Matching 02: that loop where the acquired images are processed * * Matching 02: Find the model find_generic_shape_model (Image, ModelID, MatchResultID, NumMatchResult) * * Matching 02: Retrieve results for I := 0 to NumMatchResult-1 by 1 * * Matching 02: Display the detected match dev_display (Image) get_generic_shape_model_result_object (MatchContour, MatchResultID, I, 'contours') dev_set_color ('green') dev_display (MatchContour) * * Matching 02: Retrieve parameters of the detected match get_generic_shape_model_result (MatchResultID, I, 'row', Row) get_generic_shape_model_result (MatchResultID, I, 'column', Column) get_generic_shape_model_result (MatchResultID, I, 'angle', Angle) get_generic_shape_model_result (MatchResultID, I, 'scale_row', ScaleRow) get_generic_shape_model_result (MatchResultID, I, 'scale_column', ScaleColumn) get_generic_shape_model_result (MatchResultID, I, 'hom_mat_2d', HomMat2D) get_generic_shape_model_result (MatchResultID, I, 'score', Score) stop () endfor * * Matching 02: ******************************************* * Matching 02: END of generated code for model application * Matching 02: ******************************************* * 以这个代码为基础,编写使用模板匹配的方法,定位四张CD封面上的CD序列号的halcon代码
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值