opencv各种小例子

图像腐蚀

#include <opencv2/highgui/highgui.hpp>//OpenCV highgui 模块头文件 ~
#include <opencv2/imgproc/imgproc.hpp>//OpenCV 图像处理头文件 
using namespace cv; // 包含 cv 命名空间
int main() //控制台应用程序的入 口 函数,我们的程序从这里开始
{
Mat srclmage = imread("G:\\QQ图片20190428194331.jpg"); 
imshow("[ 原图 ] ",srclmage);
//进行腐蚀操作 
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));//getStructuringElement函数的返回值为指定形状和尺寸的结构元素(内核矩阵〉
Mat dstlmage;
erode(srclmage, dstlmage, element);
//显示效果图 
imshow ("[ 效果图 ] ", dstlmage); 
	waitKey(0);
	return 0;
}

图像模糊

#include <opencv2/highgui/highgui.hpp>//OpenCV highgui 模块头文件 ~
#include <opencv2/imgproc/imgproc.hpp>//OpenCV 图像处理头文件 
using namespace cv; // 包含 cv 命名空间
int main() //控制台应用程序的入 口 函数,我们的程序从这里开始
{
Mat srclmage = imread("G:\\QQ图片20190428194331.jpg"); 
imshow("[ 原图 ] ",srclmage);
//进行均值滤波操作
Mat dstlmage;
blur(srclmage, dstlmage, Size(7, 7));
imshow ("[ 效果图 ] ", dstlmage); 
	waitKey(0);
	return 0;
}

canny边缘检测

#include<opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>//OpenCV 图像处理头文件 
using namespace cv; // 包含 cv 命名空间
int main() //控制台应用程序的入 口 函数,我们的程序从这里开始
{
Mat srcImage = imread("G:\\QQ图片20190428194331.jpg"); 
imshow("[ 原图 ] ",srcImage);
Mat dstImage, edge, grayImage;
//创建与src同类型和大小的矩阵(dst)
dstImage.create(srcImage.size(), srcImage.type());
//将原图像转换为灰度图像,Opencv2
cvtColor(srcImage, grayImage, CV_BGR2GRAY);
/*Opencv3
cvtColor(srcImage,grayImage,COLOR_BGR2GRAY);
*/
//先使用3x3内核来降噪
blur(grayImage, edge, Size(3, 3));
//运行Canny算子
Canny(edge, edge, 3, 9, 3);
//显示
imshow ("[ 效果图 ] ", edge); 
	waitKey(0);
	return 0;
}

读取视频

#include<opencv2\opencv.hpp>
using namespace cv;
int main()
{
	//读入视频
	VideoCapture capture("G:\\视觉资料\\【OpenCV3版】《OpenCV3编程入门》书本配套源代码\\【1】书本正篇程序源代码\\【1】第一章\\【6】播放视频\\6_播放视频\\1.avi ");
		//循环显示每一帧
		while(1)
	{
		Mat frame;//定义一个Mat变量,用于储存每一帧的图像
		capture >> frame;//读取当前帧
		imshow("读取视频",frame);//显示当前帧
		waitKey(30);//延时30ms

	}
	return 0;
}

canny从摄像头得到的视频

#include<opencv2\opencv.hpp>
using namespace cv;
int main()
{
	
	VideoCapture capture(0);
	Mat edges;
		//循环显示每一帧
		while(1)
	{
		Mat frame;//定义一个Mat变量,用于储存每一帧的图像
		capture >> frame;//读取当前帧
		cvtColor(frame, edges, CV_BGR2GRAY);
		blur(edges, edges, Size(7, 7));
		Canny(edges, edges, 0, 30, 3);
		imshow("canny后的视频",edges);//显示当前帧
		if(waitKey(30)>=0)break;//延时30ms

	}
	return 0;
}

2.1.1 彩色目标跟踪:Camshift

注意:本代码仅供学习交流所用,所有权归《OpenCV3编程入门》OpenCV3版书,请勿商用

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;



//-----------------------------------【全局变量声明】-----------------------------------------
//		描述:声明全局变量
//-------------------------------------------------------------------------------------------------
Mat image;
bool backprojMode = false;
bool selectObject = false;
int trackObject = 0;
bool showHist = true;
Point origin;
Rect selection;
int vmin = 10, vmax = 256, smin = 30;


//--------------------------------【onMouse( )回调函数】------------------------------------
//		描述:鼠标操作回调
//-------------------------------------------------------------------------------------------------
static void onMouse(int event, int x, int y, int, void*)
{
	if (selectObject)
	{
		selection.x = MIN(x, origin.x);
		selection.y = MIN(y, origin.y);
		selection.width = std::abs(x - origin.x);
		selection.height = std::abs(y - origin.y);

		selection &= Rect(0, 0, image.cols, image.rows);
	}

	switch (event)
	{
		//此句代码的OpenCV2版为:
		//case CV_EVENT_LBUTTONDOWN:
		//此句代码的OpenCV3版为:
	case EVENT_LBUTTONDOWN:
		origin = Point(x, y);
		selection = Rect(x, y, 0, 0);
		selectObject = true;
		break;
		//此句代码的OpenCV2版为:
		//case CV_EVENT_LBUTTONUP:
		//此句代码的OpenCV3版为:
	case EVENT_LBUTTONUP:
		selectObject = false;
		if (selection.width > 0 && selection.height > 0)
			trackObject = -1;
		break;
	}
}

//--------------------------------【help( )函数】----------------------------------------------
//		描述:输出帮助信息
//-------------------------------------------------------------------------------------------------
static void ShowHelpText()
{
	cout << "\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n"
		<< "\n\n\t\t\t此为本书OpenCV3版的第8个配套示例程序\n"
		<< "\n\n\t\t\t   当前使用的OpenCV版本为:" << CV_VERSION
		<< "\n\n  ----------------------------------------------------------------------------";

	cout << "\n\n\t此Demo显示了基于均值漂移的追踪(tracking)技术\n"
		"\t请用鼠标框选一个有颜色的物体,对它进行追踪操作\n";

	cout << "\n\n\t操作说明: \n"
		"\t\t用鼠标框选对象来初始化跟踪\n"
		"\t\tESC - 退出程序\n"
		"\t\tc - 停止追踪\n"
		"\t\tb - 开/关-投影视图\n"
		"\t\th - 显示/隐藏-对象直方图\n"
		"\t\tp - 暂停视频\n";
}

const char* keys =
{
	"{1|  | 0 | camera number}"
};


//-----------------------------------【main( )函数】--------------------------------------------
//		描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main(int argc, const char** argv)
{
	ShowHelpText();

	VideoCapture cap;
	Rect trackWindow;
	int hsize = 16;
	float hranges[] = { 0,180 };
	const float* phranges = hranges;

	cap.open(0);

	if (!cap.isOpened())
	{
		cout << "不能初始化摄像头\n";
	}

	namedWindow("Histogram", 0);
	namedWindow("CamShift Demo", 0);
	setMouseCallback("CamShift Demo", onMouse, 0);
	createTrackbar("Vmin", "CamShift Demo", &vmin, 256, 0);
	createTrackbar("Vmax", "CamShift Demo", &vmax, 256, 0);
	createTrackbar("Smin", "CamShift Demo", &smin, 256, 0);

	Mat frame, hsv, hue, mask, hist, histimg = Mat::zeros(200, 320, CV_8UC3), backproj;
	bool paused = false;

	for (;;)
	{
		if (!paused)
		{
			cap >> frame;
			if (frame.empty())
				break;
		}

		frame.copyTo(image);

		if (!paused)
		{
			cvtColor(image, hsv, COLOR_BGR2HSV);

			if (trackObject)
			{
				int _vmin = vmin, _vmax = vmax;

				inRange(hsv, Scalar(0, smin, MIN(_vmin, _vmax)),
					Scalar(180, 256, MAX(_vmin, _vmax)), mask);
				int ch[] = { 0, 0 };
				hue.create(hsv.size(), hsv.depth());
				mixChannels(&hsv, 1, &hue, 1, ch, 1);

				if (trackObject < 0)
				{
					Mat roi(hue, selection), maskroi(mask, selection);
					calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
					//此句代码的OpenCV3版为:
					normalize(hist, hist, 0, 255, NORM_MINMAX);
					//此句代码的OpenCV2版为:
					//normalize(hist, hist, 0, 255, CV_MINMAX);

					trackWindow = selection;
					trackObject = 1;

					histimg = Scalar::all(0);
					int binW = histimg.cols / hsize;
					Mat buf(1, hsize, CV_8UC3);
					for (int i = 0; i < hsize; i++)
						buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*180. / hsize), 255, 255);

					//此句代码的OpenCV3版为:
					cvtColor(buf, buf, COLOR_HSV2BGR);
					//此句代码的OpenCV2版为:
					//cvtColor(buf, buf, CV_HSV2BGR);

					for (int i = 0; i < hsize; i++)
					{
						int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows / 255);
						rectangle(histimg, Point(i*binW, histimg.rows),
							Point((i + 1)*binW, histimg.rows - val),
							Scalar(buf.at<Vec3b>(i)), -1, 8);
					}
				}

				calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
				backproj &= mask;
				RotatedRect trackBox = CamShift(backproj, trackWindow,

					//此句代码的OpenCV3版为:
					TermCriteria(TermCriteria::EPS | TermCriteria::COUNT, 10, 1));
				//此句代码的OpenCV2版为:
				//TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));

				if (trackWindow.area() <= 1)
				{
					int cols = backproj.cols, rows = backproj.rows, r = (MIN(cols, rows) + 5) / 6;
					trackWindow = Rect(trackWindow.x - r, trackWindow.y - r,
						trackWindow.x + r, trackWindow.y + r) &
						Rect(0, 0, cols, rows);
				}

				if (backprojMode)
					cvtColor(backproj, image, COLOR_GRAY2BGR);

				//此句代码的OpenCV3版为:
				ellipse(image, trackBox, Scalar(0, 0, 255), 3, LINE_AA);
				//此句代码的OpenCV2版为:
				//ellipse( image, trackBox, Scalar(0,0,255), 3, CV_AA );

			}
		}
		else if (trackObject < 0)
			paused = false;

		if (selectObject && selection.width > 0 && selection.height > 0)
		{
			Mat roi(image, selection);
			bitwise_not(roi, roi);
		}

		imshow("CamShift Demo", image);
		imshow("Histogram", histimg);

		char c = (char)waitKey(10);
		if (c == 27)
			break;
		switch (c)
		{
		case 'b':
			backprojMode = !backprojMode;
			break;
		case 'c':
			trackObject = 0;
			histimg = Scalar::all(0);
			break;
		case 'h':
			showHist = !showHist;
			if (!showHist)
				destroyWindow("Histogram");
			else
				namedWindow("Histogram", 1);
			break;
		case 'p':
			paused = !paused;
			break;
		default:
			;
		}
	}

	return 0;
}

9_用光流法进行运动目标检测

//---------------------------------【头文件、命名空间包含部分】----------------------------
//		描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include <opencv2/video/video.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <cstdio>

using namespace std;
using namespace cv;





//-----------------------------------【全局函数声明】-----------------------------------------
//		描述:声明全局函数
//-------------------------------------------------------------------------------------------------
void tracking(Mat &frame, Mat &output);
bool addNewPoints();
bool acceptTrackedPoint(int i);

//-----------------------------------【全局变量声明】-----------------------------------------
//		描述:声明全局变量
//-------------------------------------------------------------------------------------------------
string window_name = "optical flow tracking";
Mat gray;	// 当前图片
Mat gray_prev;	// 预测图片
vector<Point2f> points[2];	// point0为特征点的原来位置,point1为特征点的新位置
vector<Point2f> initial;	// 初始化跟踪点的位置
vector<Point2f> features;	// 检测的特征
int maxCount = 500;	// 检测的最大特征数
double qLevel = 0.01;	// 特征检测的等级
double minDist = 10.0;	// 两特征点之间的最小距离
vector<uchar> status;	// 跟踪特征的状态,特征的流发现为1,否则为0
vector<float> err;


//--------------------------------【help( )函数】----------------------------------------------
//		描述:输出帮助信息
//-------------------------------------------------------------------------------------------------
static void help()
{
	//输出欢迎信息和OpenCV版本
	cout <<"\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n"
		<<"\n\n\t\t\t此为本书OpenCV3版的第9个配套示例程序\n"
		<<	"\n\n\t\t\t   当前使用的OpenCV版本为:" << CV_VERSION 
		<<"\n\n  ----------------------------------------------------------------------------" ;
}


//-----------------------------------【main( )函数】--------------------------------------------
//		描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main()
{

	Mat frame;
	Mat result;

	VideoCapture capture("1.avi");

	help();
	if(capture.isOpened())	// 摄像头读取文件开关
	{
		while(true)
		{
			capture >> frame;

			if(!frame.empty())
			{ 
				tracking(frame, result);
			}
			else
			{ 
				printf(" --(!) No captured frame -- Break!");
				break;
			}

			int c = waitKey(50);
			if( (char)c == 27 )
			{
				break; 
			} 
		}
	}
	return 0;
}

//-------------------------------------------------------------------------------------------------
// function: tracking
// brief: 跟踪
// parameter: frame	输入的视频帧
//			  output 有跟踪结果的视频帧
// return: void
//-------------------------------------------------------------------------------------------------
void tracking(Mat &frame, Mat &output)
{

	//此句代码的OpenCV3版为:
	cvtColor(frame, gray, COLOR_BGR2GRAY);
	//此句代码的OpenCV2版为:
	//cvtColor(frame, gray, CV_BGR2GRAY);

	frame.copyTo(output);

	// 添加特征点
	if (addNewPoints())
	{
		goodFeaturesToTrack(gray, features, maxCount, qLevel, minDist);
		points[0].insert(points[0].end(), features.begin(), features.end());
		initial.insert(initial.end(), features.begin(), features.end());
	}

	if (gray_prev.empty())
	{
		gray.copyTo(gray_prev);
	}
	// l-k光流法运动估计
	calcOpticalFlowPyrLK(gray_prev, gray, points[0], points[1], status, err);
	// 去掉一些不好的特征点
	int k = 0;
	for (size_t i=0; i<points[1].size(); i++)
	{
		if (acceptTrackedPoint(i))
		{
			initial[k] = initial[i];
			points[1][k++] = points[1][i];
		}
	}
	points[1].resize(k);
	initial.resize(k);
	// 显示特征点和运动轨迹
	for (size_t i=0; i<points[1].size(); i++)
	{
		line(output, initial[i], points[1][i], Scalar(0, 0, 255));
		circle(output, points[1][i], 3, Scalar(0, 255, 0), -1);
	}

	// 把当前跟踪结果作为下一此参考
	swap(points[1], points[0]);
	swap(gray_prev, gray);

	imshow(window_name, output);
}

//-------------------------------------------------------------------------------------------------
// function: addNewPoints
// brief: 检测新点是否应该被添加
// parameter:
// return: 是否被添加标志
//-------------------------------------------------------------------------------------------------
bool addNewPoints()
{
	return points[0].size() <= 10;
}

//-------------------------------------------------------------------------------------------------
// function: acceptTrackedPoint
// brief: 决定哪些跟踪点被接受
// parameter:
// return:
//-------------------------------------------------------------------------------------------------
bool acceptTrackedPoint(int i)
{
	return status[i] && ((abs(points[0][i].x - points[1][i].x) + abs(points[0][i].y - points[1][i].y)) > 2);
}

点追踪

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;



//--------------------------------【help( )函数】----------------------------------------------
//		描述:输出帮助信息
//-------------------------------------------------------------------------------------------------
static void help()
{
	//输出欢迎信息和OpenCV版本
	cout << "\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n"
		<< "\n\n\t\t\t此为本书OpenCV3版的第10个配套示例程序\n"
		<< "\n\n\t\t\t   当前使用的OpenCV版本为:" << CV_VERSION
		<< "\n\n  ----------------------------------------------------------------------------";
	cout << "\n\n\t该Demo演示了 Lukas-Kanade基于光流的lkdemo\n";
	cout << "\n\t程序默认从摄像头读入视频,可以按需改为从视频文件读入图像\n";
	cout << "\n\t操作说明: \n"
		"\t\t通过点击在图像中添加/删除特征点\n"
		"\t\tESC - 退出程序\n"
		"\t\tr -自动进行追踪\n"
		"\t\tc - 删除所有点\n"
		"\t\tn - 开/光-夜晚模式\n" << endl;
}

Point2f point;
bool addRemovePt = false;

//--------------------------------【onMouse( )回调函数】------------------------------------
//		描述:鼠标操作回调
//-------------------------------------------------------------------------------------------------
static void onMouse(int event, int x, int y, int /*flags*/, void* /*param*/)
{
	//此句代码的OpenCV2版为:
	//if( event == CV_EVENT_LBUTTONDOWN )
	//此句代码的OpenCV3版为:
	if (event == EVENT_LBUTTONDOWN)
	{
		point = Point2f((float)x, (float)y);
		addRemovePt = true;
	}
}

//-----------------------------------【main( )函数】--------------------------------------------
//		描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
	help();

	VideoCapture cap;

	//此句代码的OpenCV2版为:
	//TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
	//此句代码的OpenCV3版为:
	TermCriteria termcrit(TermCriteria::MAX_ITER | TermCriteria::EPS, 20, 0.03);
	Size subPixWinSize(10, 10), winSize(31, 31);

	const int MAX_COUNT = 500;
	bool needToInit = false;
	bool nightMode = false;


	cap.open(0);

	if (!cap.isOpened())
	{
		cout << "Could not initialize capturing...\n";
		return 0;
	}

	namedWindow("LK Demo", 1);
	setMouseCallback("LK Demo", onMouse, 0);

	Mat gray, prevGray, image;
	vector<Point2f> points[2];

	for (;;)
	{
		Mat frame;
		cap >> frame;
		if (frame.empty())
			break;

		frame.copyTo(image);
		cvtColor(image, gray, COLOR_BGR2GRAY);

		if (nightMode)
			image = Scalar::all(0);

		if (needToInit)
		{
			// 自动初始化
			goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, (double)10, Mat(), 3,(bool) 0, 0.04);
			cornerSubPix(gray, points[1], subPixWinSize, Size(-1, -1), termcrit);
			addRemovePt = false;
		}
		else if (!points[0].empty())
		{
			vector<uchar> status;
			vector<float> err;
			if (prevGray.empty())
				gray.copyTo(prevGray);
			calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
				3, termcrit, 0, 0.001);
			size_t i, k;
			for (i = k = 0; i < points[1].size(); i++)
			{
				if (addRemovePt)
				{
					if (norm(point - points[1][i]) <= 5)
					{
						addRemovePt = false;
						continue;
					}
				}

				if (!status[i])
					continue;

				points[1][k++] = points[1][i];
				circle(image, points[1][i], 3, Scalar(0, 255, 0), -1, 8);
			}
			points[1].resize(k);
		}

		if (addRemovePt && points[1].size() < (size_t)MAX_COUNT)
		{
			vector<Point2f> tmp;
			tmp.push_back(point);

			//此句代码的OpenCV2版为:
			//cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
			//此句代码的OpenCV3版为:
			cornerSubPix(gray, tmp, winSize, Size(-1, -1), termcrit);
			points[1].push_back(tmp[0]);
			addRemovePt = false;
		}

		needToInit = false;
		imshow("LK Demo", image);

		char c = (char)waitKey(10);
		if (c == 27)
			break;
		switch (c)
		{
		case 'r':
			needToInit = true;
			break;
		case 'c':
			points[0].clear();
			points[1].clear();
			break;
		case 'n':
			nightMode = !nightMode;
			break;
		}

		std::swap(points[1], points[0]);
		cv::swap(prevGray, gray);
	}

	return 0;
}
posted @ 2019-07-08 16:00  星空与沧海  阅读(8793)  评论(2编辑  收藏  举报