作者:
Vvvahc (我來搞事了)
2022-01-10 18:36:06開發平台(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;
}