作者Vvvahc (我來搞事了)
看板C_and_CPP
標題[問題] 請教thread與指標用法
時間Mon Jan 10 18:36:06 2022
開發平台(Platform): (Ex: Win10, Linux, ...)
win10/VS2019
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
<opencv2> <pco.sdk>
問題(Question):
我本來使用opencv搭配相機(pco工業相機)的sdk , 希望在拍攝物體的當下,除了顯示畫
面外也能夠及時儲存每張圖片
不過cpu速度很明顯會下降很多,查了網路上的解法需要加入thread做處理.
我的思路是在int main 函式前使用class與public分別定義imshow與imwrite的行為
,再用thread來做加速. 不曉得這樣做是否正確
還有一個問題是, 可能我對指標不是很熟…我在用下面語法寫 public class時無法讀取i
nt main的指標資訊, 希望各位大大能指點迷津
感謝
class test_thread{
public:
void cvshow( WORD Width, WORD Height, WORD buffer)
{
Width = &imgWidth; Height = & imgHeight; buffer = *imgBuffer;
cv::Mat cv_image;
cv_image = ( cv::Size(Width,Height), CV_16UC1, buffer)
imshow()
waitKey(1)
}
}
int main()
……
// 原始程式碼(已簡略), 目地是希望改成
// void cvshow()
// { cv: imshow }
// int main()
// { 讀入相機資訊,並回傳指標
// ex: imgWidth 、imgBuffer 給cvshow
// 做thread處理 }
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
#include "opencv.hpp"
#include "sc2_SDKStructures.h"
#include "PCO_Recorder_Defines.h"
using namespace std;
using namespace cv;
int main ( int argc , char*argv )
{
int acquireimage = 10;
// 從相機得到圖片長、寬資訊
int iRet;
WORD imgWidth = 0, imgHeight = 0;
iRet = Pco_RecorderGetSetting( ...,&imgWidth,&imgHeight );
// 為圖片分配內存
WORD *imgBuffer = NULL;
imgBuffer = new WORD[(__int64)imgWidth*(__int64)imgHeight];
iRet = RecorderGetStatus( ... );
while ( isRunning )
if ( imagecount > 0 )
{
iRet = ( ... , imgWidth, imgHeight,imgBuffer );
return ( imgWidth );
cv:: Mat cv_image;
cv_image = (cv:: Size( imgWidth , imgHeight ) , CV_16UC1 , imgBuffer , cv:: Ma
t :: AUTO_STEP );
cv:: nameWindow ( "image", CV_WINDOW_NORMAL );
imshow ( "image" , cv_image);
waitKey(1);
for (unsigned int i = 0; i < acquireimage; i++ )
{
string a = " C:/desktop/folder/img " + to_string(i) + ".tif" ;
imwrite( a ,cv_image )
}
}
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.175.4 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1641810968.A.0D7.html
※ 編輯: Vvvahc (140.112.175.4 臺灣), 01/10/2022 18:37:12
※ 編輯: Vvvahc (140.112.175.4 臺灣), 01/10/2022 18:39:11
→ nh60211as: 你這程式碼編譯邊得過嗎 01/10 19:03
→ Vvvahc: 下面程式碼(為方便閱讀有簡略過)是可以執行且順利顯示的(01/10 19:08
→ Vvvahc: 但我imwrite的寫法會很卡), 不過當我要改成有thread的寫01/10 19:08
→ Vvvahc: 法就會出問題…01/10 19:08
→ Vvvahc: 主要是想問如何在上方的class : void()中,去讀取下方int01/10 19:10
→ Vvvahc: main()中的指標資訊01/10 19:10
※ 編輯: Vvvahc (140.112.175.4 臺灣), 01/10/2022 19:19:04
推 ko27tye: 呃 把你的指標當參數傳進去阿 01/10 20:25
推 sarafciel: 先不講傳參的問題 你用來起thread的程式碼呢 01/10 21:05
推 lycantrope: 到底在寫什麼啊w 01/10 22:24
→ lycantrope: nameWindow可以移出while 只留下imshow 01/10 22:24
推 lycantrope: waitkey是millisecond,你應該不需要1000fps imshow 01/10 22:29
→ lycantrope: 然後opencv有startWindowThread可以讓視窗thread 01/10 22:31
推 henrylin8086: 先說thread要能加速,建立在你task能平行化上。不 01/15 11:56
→ henrylin8086: 能的話,用thread是把task給其他thread做,這樣才 01/15 11:56
→ henrylin8086: 不會卡main thread。速度不會被提升,但是程式不會 01/15 11:56
→ henrylin8086: 被卡住。 01/15 11:56
推 henrylin8086: 你可以把main裡的指標傳進去給class,比較粗糙的寫 01/15 12:08
→ henrylin8086: 法就是直接assign,在main裡面Class.ptr_inClass = 01/15 12:08
→ henrylin8086: ptr_inMain,但是要小心多執行緒的時序問題。 01/15 12:08
→ Vvvahc: 我主要是打算讓每張拍攝的照片能吐出到硬碟資料夾中 01/25 11:11
→ Vvvahc: 我有試著照h大的做法 不過在拆出來的function中,我遇到 01/25 11:15
→ Vvvahc: 相機buffer的ptr會報錯exception thrown 01/25 11:15
→ Vvvahc: 有點懷疑是不是我buffer ptr型態寫錯的問題… 01/25 11:16
→ Lipraxde: Backtrace 看一看,debug 加油 02/06 04:08