Opencv3.x下实现小坦克在地图上跑动

实现坦克在地图上的跑动,虽然这只是一个小应用,但是涉及的东西确实不少,前期一直学opencv不过放了很长时间,帮同学重写这个东西,确实有点麻烦,本来很简单就能做的,硬是发了一个小时。

接着说说这个应用涉及的东西吧:

(1)图像的读取;

(2)图像感兴区域的获取;

(3)图像融合;

(4)图像的遍历;

把这些图像处理的过程连串起来确实不是什么难事。写下这篇博客记录一下,免得以后要用到这些类似的东西的时候又要到处找。

我使用的环境:VS2015+Opencv3.2;代码如下:

// 贴图3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include<opencv2/opencv.hpp>
using namespace cv;
RNG rng;
void RemoveBackground(Mat& src, Mat& dst);
void main()
{
	Mat tank = imread("tanke.png", 1);//tank pic
	Mat m_map = imread("map.jpg", 1);//map pic
	namedWindow("show pic move", CV_WINDOW_AUTOSIZE);//define windows to show pic
	size_t mapwidth = m_map.cols;
	size_t mapHeight = m_map.rows;
	size_t movewidth = m_map.cols - tank.cols;
	size_t moveheight = m_map.rows - tank.rows;
	while (1)
	{
		Mat tmap=m_map.clone();
		int x = rng.uniform(0, movewidth);
		int y = rng.uniform(0, moveheight);
		Mat imageroi;
		imageroi = tmap(Rect(x, y, tank.cols, tank.rows));
//		addWeighted(tank, 0.3, imageroi, 0.7, 0.0, imageroi);
		
		RemoveBackground(tank, imageroi);
	//	tank.copyTo(imageroi);
		imshow("show pic move", tmap);
		waitKey(1000);
	}
	waitKey(0);
	return;
}
void RemoveBackground(Mat& src, Mat& dst)
{
	int channels = src.channels();
	size_t width = src.cols * channels;
	size_t height = src.rows;
	for (size_t i = 0; i < height; i++)
	{
		uchar* sdata = src.ptr<uchar>(i);
		uchar* ddata = dst.ptr<uchar>(i);
		for (size_t j = 0; j < width; j+= channels)
		{
			if (sdata[j] < 180 && sdata[j+1] < 180 && sdata[j + 2] < 180)
			{
				ddata[j] = sdata[j];
				ddata[j+1] = sdata[j+1];
				ddata[j+2] = sdata[j+2];
			}
		}
	}
	
}


猜你喜欢

转载自blog.csdn.net/thecentry/article/details/79809653
今日推荐