您的位置:首页 > 运维架构

opencv学习系列:实例练习,含多个工程实例

2017-12-22 10:25 399 查看
/*-----------------------------------------------------------------------------------------------------------*/
//****1.opencv实现RGB到HSV,直方图均衡化V空间,再用筛选范围阈值分割结合开、闭运算得到特定的颜色块****//
/*-----------------------------------------------------------------------------------------------------------*/
#include<opencv2/opencv.hpp>

#include <iomanip>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap(0); //capture the video from web cam

if (!cap.isOpened())  // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}

namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"

int iLowH = 100;
int iHighH = 140;

int iLowS = 90;
int iHighS = 255;

int iLowV = 90;
int iHighV = 255;

//Create trackbars in "Control" window
cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
cvCreateTrackbar("HighH", "Control", &iHighH, 179);

cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
cvCreateTrackbar("HighS", "Control", &iHighS, 255);

cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
cvCreateTrackbar("HighV", "Control", &iHighV, 255);

while (true)
{
Mat imgOriginal;

bool bSuccess = cap.read(imgOriginal); // read a new frame from video

if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}

Mat imgHSV;
vector<Mat> hsvSplit;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

//因为我们读取的是彩色图,直方图均衡化需要在HSV空间做
split(imgHSV, hsvSplit);
equalizeHist(hsvSplit[2], hsvSplit[2]);
merge(hsvSplit, imgHSV);
Mat imgThresholded;

inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

//开操作 (去除一些噪点)
Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element);

//闭操作 (连接一些连通域)
morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);

imshow("Thresholded Image", imgThresholded); //show the thresholded image
imshow("Original", imgOriginal); //show the original image

char key = (char)waitKey(300);
if (key == 27)
break;
}

return 0;

}
/*-----------------------------------------------------------------------------------------------------------*/
//****2.系统时间显示,帧率测试****//
/*-----------------------------------------------------------------------------------------------------------*/
#include <iostream>
#include <opencv2/opencv.hpp>  //头文件
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <cstring>
#include <vector>

/*返回字符串格式:星期,月,日,小时:分:秒,年:

time_t t;
time(&t);
printf("Today's date and time: %s", ctime(&t));
system("pause");
return 0;
*/

#include<windows.h>
#include<time.h>
#include <mmsystem.h>

using namespace cv;  //包含cv命名空间
using namespace std;

int main()
{
VideoCapture capture("../libo_output/output3.avi");
Mat image;
while (1)
{
double Time = (double)getTickCount();
char* realFPS = new char[128];
char* date = new char[30];
SYSTEMTIME sys;//获取当前系统时间
GetLocalTime(&sys);
char* dateTime = new char[128];
sprintf(dateTime, ("%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d"),
sys.wYear, sys.wMonth, sys.wDay,
sys.wHour, sys.wMinute, sys.wSecond);

//隔两帧配准
capture >> image;

if (image.empty())
{
break;
}

Time =(double)getTickFrequency() / ((double)getTickCount() - Time);//真实处理帧率
sprintf(realFPS, "realFPS:%.2f",Time);
putText(image, dateTime, Point(10, 25), FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0));
putText(image, realFPS, Point(10, 45), FONT_HERSHEY_COMPLEX, 0.5, Scalar(255, 255, 0));
imshow("测试", image);
waitKey(30);

}

waitKey(0);
return 0;
}

/*-----------------------------------------------------------------------------------------------------------*/
//****3.opencv实现时钟功能****//
/*-----------------------------------------------------------------------------------------------------------*/
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
//#include <sys/time.h>
#include<sys\utime.h>

using namespace std;
using namespace cv;

int main()
{
Mat clk(640, 640, CV_8UC3, Scalar(180, 120, 50)); //Mat to store clock image
Mat back_up(640, 640, CV_8UC3, Scalar(180, 120, 50)); //Mat to store backup image

Point cent(clk.rows / 2, clk.cols / 2);
Point perim(clk.cols / 2, 0);
int rad = clk.cols / 2;
float sec_angle = 270;
float min_angle = 330;
float hour_angle = 210;

//画秒针刻度
vector<Point> pt1, pt2;
for (int i = 0; i < 60; i++)
{
int x1 = cent.x + rad*cos(i * 6 * CV_PI / 180.0);
int y1 = cent.y + rad*sin(i * 6 * CV_PI / 180.0);
pt1.push_back(Point(x1, y1));

int x2 = cent.x + (rad - 20)*cos(i * 6 * CV_PI / 180.0);
int y2 = cent.y + (rad - 20)*sin(i * 6 * CV_PI / 180.0);
pt2.push_back(Point(x2, y2));

line(clk, pt1[i], pt2[i], Scalar(0, 255, 0, 0), 1.5, CV_AA, 0);
}
//画整点刻度
vector<Point> pt3, pt4;
for (int i = 0; i < 12; i++)
{
int x3 = cent.x + (rad - 40)*cos(i * 30 * CV_PI / 180.0);
int y3 = cent.y + (rad - 40)*sin(i * 30 * CV_PI / 180.0);
pt3.push_back(Point(x3, y3));

line(clk, pt1[(i * 5)], pt3[i], Scalar(0, 255, 0, 0), 5, CV_AA, 0);
}
//画最外围的圆和圆心的三针连接点
circle(clk, cent, rad, Scalar(50, 50, 255, 0), 6, CV_AA, 0); //Dreaw outercircle of clock
circle(clk, cent, 2, Scalar(0, 255, 0, 0), 5, CV_AA, 0); //Draw inner circle

back_up = clk.clone(); // Clone to backup image

time_t rawtime;
struct tm * timeinfo;
float second;
float minute;
float hour;
float millisec;
struct timeb tmb;

while (1){
//获取本地时间
ftime(&tmb);
rawtime = tmb.time;
timeinfo = localtime(&rawtime);

second = timeinfo->tm_sec;
minute = timeinfo->tm_min;
hour = timeinfo->tm_hour;
millisec = tmb.millitm;

second = second + millisec / 1000;
sec_angle = (second * 6) + 270; //Convert second to angle

minute = minute + second / 60;
min_angle = minute * 6 + 270; //Conver minute to angle

if (hour>12)hour = hour - 12;
hour_angle = (hour * 30) + (minute*.5) + 270; //Conver hour to angle

if (sec_angle>360)sec_angle = sec_angle - 360;
if (min_angle>360)min_angle = min_angle - 360;
if (hour_angle>360)hour_angle = hour_angle - 360;

//画秒针
perim.x = (int)round(cent.x + (rad - 5) * cos(sec_angle * CV_PI / 180.0));
perim.y = (int)round(cent.y + (rad - 5) * sin(sec_angle * CV_PI / 180.0));
line(clk, cent, perim, Scalar(0, 255, 255, 0), 1.5, CV_AA, 0);

//画分针
perim.x = (int)round(cent.x + (rad - 30) * cos(min_angle * CV_PI / 180.0));
perim.y = (int)round(cent.y + (rad - 30) * sin(min_angle * CV_PI / 180.0));
line(clk, cent, perim, Scalar(0, 255, 255, 0), 4, CV_AA, 0);

//画时针
perim.x = (int)round(cent.x + (rad - 100) * cos(hour_angle * CV_PI / 180.0));
perim.y = (int)round(cent.y + (rad - 100) * sin(hour_angle * CV_PI / 180.0));
line(clk, cent, perim, Scalar(0, 255, 255, 0), 12, CV_AA, 0);

imshow("Clock", clk); //Show result in a window
clk.setTo(0); // set clk image to zero for next drawing
clk = back_up.clone(); // Clone the previously drawned markings from back-up image

char c = waitKey(999); // 这里如果参数为10,则看到的是秒针连续地转动;如果是1000,则效果是秒针一秒一秒地跳动
if (c == 27)break;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: