资源简介
C++ 和opencv 利用多线程的方法实现图像的识别检测模板程序,速度大大提升。有利于工程应用和学习
代码片段和文件信息
#include
#include
#include
#include
#include“putTextZh\putText.h“
using namespace std;
using namespace cv;
int key = 0;//捕获的键盘输入
int Threshold = 120; // 二值化的阈值
int Mouse_x = 0;//鼠标当前位置xy
int Mouse_y = 0;
int MouseState = -1;//鼠标状态。
const string MainWindow = “白酒检测系统“;
const string Tracker = “二值化阈值“;
int height = 480 width = 640;
//颜色表 https://blog.csdn.net/kc58236582/article/details/50563958 // 注意Scalar是 BGR顺序
Scalar yellow = Scalar(0 255 255);
Scalar red = Scalar(0 0 255);
Scalar white = Scalar(255 255 255);
Scalar black = Scalar(0 0 0);
Scalar green = Scalar(0 255 0);
DWORD WINAPI ThreadFunction1(LPVOID lpParameter);
DWORD WINAPI ThreadFunction2(LPVOID lpParameter);
Mat Interface(Mat Input);//界面所在
void on_mouse(int event int x int y int void *); //获取鼠标位置
void on_TrackerbarBW(int threshold void *){}//条码作用
Mat ImageT1In = Mat::zeros(height width CV_8UC3);//线程1的输入图像
Mat ImageT1Out = Mat::zeros(height width CV_8UC3);//线程1的输出图像
Mat ImageT2Out = Mat::zeros(height width CV_8UC3);//线程2的输出图像
Mat ImageT2In = Mat::zeros(height width CV_8UC3);//线程1的输入图像
Mat ImageToshow = Mat::zeros(height * 2 width * 2.5 CV_8UC3);//被显示的图像
int main()
{
HANDLE handle1 handle2;
handle1 = CreateThread(NULL 0 ThreadFunction1 NULL 0 NULL);
handle2 = CreateThread(NULL 0 ThreadFunction2 NULL 0 NULL);
if (NULL == handle1)
{
cout << “创建线程1 失败 !“ << endl;
return -1;
}
if (NULL == handle2)
{
cout << “创建线程2 失败 !“ << endl;
return -1;
}
CloseHandle(handle1);
CloseHandle(handle2);//创建完之后关闭线程
cout << “主线程运行 !“ << endl;
VideoCapture cap;
cap.open(0); //打开视频以上两句等价于VideoCapture cap(“E://2.avi“);
if (!cap.isOpened())//如果视频不能正常打开则返回
return -1;
Mat frame;
namedWindow(MainWindow CV_WINDOW_NORMAL);
cv::setWindowProperty(MainWindow CV_WND_PROP_FULLSCREEN CV_WINDOW_FULLSCREEN);//主界面设置全屏
cv::setMouseCallback(MainWindow on_mouse);//鼠标回调函数
cv::createTrackbar(Tracker MainWindow &Threshold 255 on_TrackerbarBW);//控制条barname winname &sidlervalue g_max ontracker
//on_TrackerbarB
while (1)
{ //cout << “主线程 !“ << endl;
ImageToshow = Mat::zeros(height * 2 width * 2.5 CV_8UC3);//被显示的图像
cap >> frame;//等价于cap.read(frame);
frame.copyTo(ImageT1In);
frame.copyTo(ImageT2In);
if (frame.empty())//如果某帧为空则退出循环
break;
//int threshold = cv::getTrackbarPos(“Track1“ “img“);//tracker bar
Mat gray;
cvtColor(ImageT2In gray COLOR_BGR2GRAY);
cv::Mat BW = gray < Threshold;
cvtColor(BW BW COLOR_GRAY2BGR);
Rect rectT1 rectT2 rectT3 rectT4;
rectT1.x = 0; rectT1.y = 0; rectT1.width = width; rectT1.height = height;
rectT2.x = 0; rectT2.y = height; rectT2.width = width; rectT2.height = height;
rec
- 上一篇:C语言活期储蓄账目管理系统
- 下一篇:侯捷C++全套课程视频资源
评论
共有 条评论