亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

求問一個多線程的例程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 關注
  • 315 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號