传送带视频流图像拼接算法改进

19 篇文章 4 订阅
5 篇文章 0 订阅

接上篇:https://blog.csdn.net/iamqianrenzhan/article/details/89648491
原来的算法为了尽可能的减少拼接,所以,每块图像尽可能的大。但是由于每帧图像采集间隔和传送带运动速度不固定,有时会出现匹配靶标过少或循环而出现拼接不上或者拼接错误的情况。本文对原来算法进行改进,以期达到更好的稳定性。

算法的思想如下:

在处理速度满足的情况下,尽可能多的进行拼接,极致的情况时对任意相邻两帧图片进行平移计算并进行拼接。这样在使用时就更能把面阵相机抽象为一个线阵相机,每次返回几行或者几十行图像。

代码备份:

首先还是要对图像进行校正:

Point2f PerspectivePoints00[4] = { Point2f(32, 41),
	Point2f(1255, 63), Point2f(1233, 908) , Point2f(33, 898) };
Point2f PerspectivePoints11[4] = { Point2f(20, 30),
	Point2f(1260, 30), Point2f(1260, 880), Point2f(20, 880) };
Mat m = getPerspectiveTransform(PerspectivePoints00, PerspectivePoints11);

然后对每一帧图像:

warpPerspective(frame, frame, m, Size(frame.cols, frame.rows));

需要对每一帧图片进行预处理

一般是转为灰度图像,滤波,二值化,找轮廓。

cvtColor(src_all, src_all, COLOR_RGB2GRAY);
imshow("1.原图", src_all);
Mat gray_all;
GaussianBlur(src_all, gray_all, Size(3, 3), 0);
threshold(gray_all, gray_all, 100, 255, THRESH_BINARY); //90以上

//找轮廓,并标准轮廓
findContours(gray_all, contours_all, hierarchy_all, RETR_TREE, CHAIN_APPROX_NONE, Point(0, 0));

靶标的定位,并输出看处理结果

contours_new.clear();
for (int i = 0; i < contours_all.size(); i++)
{
	//周长,面积,重心位置,这里的参数根据实际修改
	Point centerpoint;
	double area = contourArea(contours_all[i]);
	GetContourCenter(contours_all[i], centerpoint);
	if (contours_all[i].size() > 450 && contours_all[i].size() < 550
		&& area > 11000 && area < 14000
		&& centerpoint.x > 1000
		&& centerpoint.y >100 && centerpoint.y < 1024-100)
		contours_new.push_back(contours_all[i]);
}
Mat temp2 = frame.clone();
drawContours(temp2, contours_new, -1, CV_RGB(255, 0, 0), 10); //rng.uniform(0,255)  ,CV_FILLED

//imshow("轮廓", temp2);

靶标的识别:

用到的结构体:

struct targetObject {
	vector<Point> points;
	int message = 0;  //message 0代码没有,1,2,3,4分别代表信息
};
struct keyFrame{
	Mat frame;
	targetObject targets[4];
	int offsety;
	int offsetx;
};

用到的函数:

void GetContourCenter(vector<Point> contour, Point &p)
{
	//重心法抓中心点
	double avg_px = 0, avg_py = 0;
	for (int i = 0; i < contour.size(); i++)
	{
		avg_px += contour[i].x;
		avg_py += contour[i].y;
	}
	p.x = avg_px / contour.size();
	p.y = avg_py / contour.size();
}

识别代码:

//靶标识别
tempframe.frame = frame.clone();
for (int i = 0; i < contours_new.size(); i++)
{
	Rect rect = boundingRect(contours_new[i]);
	Point point(rect.x + rect.width / 2, rect.y + rect.height / 2);
	Point centerpoint;
	//GetContourCenter(contours_new[i], centerpoint);
	Moments m = moments(contours_new[i]);
	centerpoint.x = m.m10 / m.m00;
	centerpoint.y = m.m01 / m.m00;

	tempframe.targets[i].points = contours_new[i];
	if (point.x > centerpoint.x) //1,4
	{
		if (point.y > centerpoint.y)
			tempframe.targets[i].message = 4;
		else
			tempframe.targets[i].message = 1;
	}
	else
	{
		if (point.y > centerpoint.y)
			tempframe.targets[i].message = 3;
		else
			tempframe.targets[i].message = 2;
	}
}
for (int i = contours_new.size(); i < 4; i++)
{
	tempframe.targets[i].message = 0;
}

靶标平移计算:

用到的函数:

cv::Mat mergeRows(cv::Mat A, cv::Mat B)
{
	// cv::CV_ASSERT(A.cols == B.cols&&A.type() == B.type());
	int totalRows = A.rows + B.rows;
	cv::Mat mergedDescriptors(totalRows, A.cols, A.type());
	cv::Mat submat = mergedDescriptors.rowRange(0, A.rows);
	A.copyTo(submat);
	submat = mergedDescriptors.rowRange(A.rows, totalRows);
	B.copyTo(submat);
	return mergedDescriptors;
}

首先计算总共有多少个靶标匹配上:

vector<int> offsety;
vector<int> offsetx;
for (int i = 1; i < 5; i++)
{
	int j1, j2;
	bool isexist1 = false;
	bool isexist2 = false;
	bool isexist = false;
	for (j1 = 0; j1 < 4; j1++)
		if (tempframe.targets[j1].message == i) {
			isexist1 = true; break;
		}
	for (j2 = 0; j2 < 4; j2++)
		if (keyframe.targets[j2].message == i) {
			isexist2 = true; break;
		}
	isexist = isexist1 & isexist2;
	if (isexist)
	{
		//计算这两组点的平移量
		Point centerpoint1;
		Point centerpoint2;
		GetContourCenter(tempframe.targets[j1].points, centerpoint1);
		GetContourCenter(keyframe.targets[j2].points, centerpoint2);
		int y = centerpoint1.y - centerpoint2.y;
		int x = centerpoint1.x - centerpoint2.x;
		if (y > 0)
		{
			offsety.push_back(y);
			offsetx.push_back(x);
		}
	}
}

如果匹配的靶标数量大于1,才继续处理:

//只有相邻帧有2个或者3个靶标能匹配,才进行计算
if (offsety.size() > 1)
{
	for (int i = 0; i < offsety.size(); i++)
		for (int j = 0; j < offsety.size(); j++)
			if (abs(offsety[i] - offsety[j]) > 50)
			{
				cout << "全为正但有错误" << endl;
			}
	int sum = 0;
	for (int i = 0; i < offsety.size(); i++)
	{
		cout << offsety[i] << " ";
		sum += offsety[i];
	}
	cout << endl << endl << endl << endl;
	keyframe.offsety = sum / offsety.size();


	Mat tem = tempframe.frame.clone();

	//对keyframe中图片进行处理,截取掉和上一帧重复的部分(1024-x)
	keyframe.frame = tem(Rect(0, 0, keyframe.frame.cols, keyframe.offsety));
	//imshow("结果", keyframe.frame);

	//如果resultMat达到一定行数,则返回,并把当前处理结果赋值,否则,进行拼接
	if (resultMat.rows > 1000)
	{
		//getpic(resultMat);
		//保存resultMat进行处理
		string filename = to_string(count) + ".bmp";
		imwrite(filename, resultMat);

		resultMat = keyframe.frame.clone();

		newpic = true;
	}
	else
	{
		resultMat = mergeRows(keyframe.frame.clone(), resultMat.clone());
		newpic = false;
	}
		

	//imshow("结果", keyframe.frame);
	//resultMat = mergeRows(keyframe.frame.clone(), resultMat.clone());
	
}

结果处理:

if (newpic)
{
	//这儿写处理算法
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仟人斩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值