为了账号安全,请及时绑定邮箱和手机立即绑定

求问一个多线程的例程C++(API),情况如下所示:

求问一个多线程的例程C++(API),情况如下所示:

慕桂英546537 2021-11-20 15:11:49
#include "stdafx.h"#include "CameraDS.h"#include "string"#include <cv.h>#include <highgui.h>#include <stdio.h>#include <iostream>#include "windows.h"#include <fstream>using namespace std; int singlematching(IplImage *srcl, IplImage *srcr , IplImage *tp1);int main() {int cam_count;//仅仅获取摄像头数目cam_count = CCameraDS::CameraCount();printf("There are %d cameras.\n", cam_count);//获取所有摄像头的名称for(int i=0; i < cam_count; i++){char camera_name[1024]; int retval = CCameraDS::CameraName(i, camera_name, sizeof(camera_name) );if(retval >0)printf("Camera #%d's Name is '%s'.\n", i, camera_name);elseprintf("Can not get Camera #%d's name.\n", i);}if(cam_count==0)return -1;// 创建2个摄像头类CCameraDS camera1;CCameraDS camera2; if(! camera1.OpenCamera(0, false)) {fprintf(stderr, "Can not open camera.\n");return -1;}//打开第二个摄像头camera2.OpenCamera(1, false); cvNamedWindow("cam1",1);cvNamedWindow("cam2",1);int nFrmNum = 0;int k=0;while(1){//获取一帧IplImage *pFrame1 = camera1.QueryFrame();IplImage *pFrame2 = camera2.QueryFrame();IplImage *fFrame1 = 0;IplImage *fFrame2 = 0;float scale=0.2;CvSize dst_cvsize;dst_cvsize.width=pFrame1->width*scale;dst_cvsize.height=pFrame1->height*scale;fFrame1=cvCreateImage(dst_cvsize,pFrame1->depth,pFrame1->nChannels);fFrame2=cvCreateImage(dst_cvsize,pFrame1->depth,pFrame1->nChannels);cvResize(pFrame1,fFrame1,CV_INTER_LINEAR);cvResize(pFrame2,fFrame2,CV_INTER_LINEAR);nFrmNum++;DWORD dwStart = GetTickCount();DWORD dwEnd = dwStart;do{dwEnd = GetTickCount()-dwStart;}while(dwEnd <1);// 显示实时的摄像头视频cvShowImage("cam1", fFrame1);cvShowImage("cam2", fFrame2);IplImage *tp1 = cvLoadImage("tr.bmp",1); int m;if(k==5){m=singlematching( pFrame2, pFrame1,tp1);\\运算k=0;}k++;//cvWaitKey(33);int key = cvWaitKey(33);if( key == 27 ) break;}camera1.CloseCamera(); //可不调用此函数,CCameraDS析构时会自动关闭摄像头camera2.CloseCamera();cvDestroyAllWindows();return 0;}int singlematching(IplImage *srcl, IplImage *srcr ,IplImage *tp1){}
查看完整描述

2 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

#include <windows.h>

typedef HANDLE HSEMAPHORE;

#define P(hHandle) ::WaitForSingleObject(hHandle, INFINITE)
#define V(hHandle) ::ReleaseSemaphore(hHandle, 1, NULL)
#define Semaphore(lInitial, lMaximum) ::CreateSemaphore(NULL, lInitial, lMaximum, NULL)
#define Thread(lpProc, lpParam) ::CreateThread(NULL, 0, lpProc, (LPVOID)lpParam, 0, NULL)
#define Wait(nCount, lpHandles) ::WaitForMultipleObjects(nCount, lpHandles, TRUE, INFINITE)

HSEMAPHORE n = Semaphore(0, 1);
HSEMAPHORE s = Semaphore(1, 1);
HSEMAPHORE e = Semaphore(100, 100);

UINT count = 0;

void Produce()
void Consume()

DWORD WINAPI Producer(LPVOID lpParameter)
{
while (true)
{
Produce();
P(e);
P(s);
count++;
printf("Producer: %d\n", count);
V(s);
V(n);
}
return 0;
}

DWORD WINAPI Consumer(LPVOID lpParameter)
{
while (true)
{
P(n);
P(s);
count--;
printf("Consumer: %d\n", count);
V(s);
V(e);
Consume();
}
return 0;
}

int main()
{
HANDLE hThreads[2];

hThreads[0] = Thread(Producer, (LPVOID)NULL);
hThreads[1] = Thread(Consumer, (LPVOID)NULL);

Wait(2, hThreads);
return 0;
}



查看完整回答
反对 回复 2021-11-24
?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

首先,你要有一个概念,多线程只是在有CPU空闲的程序中实现多个线程并行,从而使程序显得更”流畅“。多线程并不会让你的CPU运算能力得到提高。
你的程序,或许可以用双线程:
一个线程捕捉图像,一个线程处理数据。但要记住两点:
1,两个线程间要加缓存
2,当缓存已经满了的时候,捕捉线程应该要等处理数据的线程处理完缓存才继续捕捉。
因此,并不能保证你用了双线程就会更加流畅,要你的的电脑运算能力。和捕捉图像的数据大小。

查看完整回答
反对 回复 2021-11-24
  • 2 回答
  • 0 关注
  • 283 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信