2020年1月17日 星期五

opencv 及halcon存圖的速度比較

執行平台:
visual 2015
opencv 3.1
halcon 12.0.2

圖檔:
用ink一眼的圖測試之
16384 * 300000 pixels
約 1.52 G 

結果:
Mat > iplimage > halcon

mat存圖比較快,大多在十三秒左右,有時候不知為什麼,會到十七秒將近二十秒;iplimage存圖次之,約十七秒左右,有時候會到廿一秒;halcon最慢,約在卅八秒左右,但速度好像都滿穩定的。
對了,經由這次的測試發現,imshow及image watch好像没法看1.52 G的圖,我猜它們只能顯示1 G以內的圖。

p.s. 没用VS 2008 加上opencv 2.3.1測試的原因,是因為opencv 2.3.1只能讀取 1 G的圖,ink這張太大,讀不進來,若要讀,考慮用imdecode這個函式,將標頭讀進來就好,然後宣告一個與圖一樣大的mat及iplimage去存,相關資料如下:


以下附上2018.03.19測試的程式碼:
#include "stdafx.h"
#include "opencv2\opencv.hpp"
#include "HalconCpp\HalconCpp.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct
{
unsigned char *Src;
int Width;
int Height;
} ImageParam;

bool SaveImageUsingMat(ImageParam imgParam)
{
clock_t beginTime, endTime;
// method 1: Using Mat
try
{
beginTime = clock();
//cv::Mat tempImg = (cv::Mat(imgParam.Height, imgParam.Width, CV_8UC1, imgParam.Src).clone())(cv::Rect(0, 0, imgParam.Height, imgParam.Width));
cv::Mat Img_mat = cv::Mat(imgParam.Height, imgParam.Width, CV_8UC1, imgParam.Src).clone();
cv::imwrite("UsingMat.bmp", Img_mat.reshape(0, imgParam.Height));
Img_mat.release();
endTime = clock();
std::cout << "Speed of using cv::Mat to save image = " << endTime - beginTime << " ms" << std::endl;

return true;
}
catch (cv::Exception err)
{
std::cout << "When save image using mat have problem: " << err.what() << std::endl;

return false;
}
}

bool SaveImageUsingIplimage(ImageParam imgParam)
{
clock_t beginTime, endTime;
// method 2: Using IplImage
try
{
beginTime = clock();
IplImage *Img_iplimage;
Img_iplimage = ::cvCreateImageHeader(cvSize(imgParam.Width, imgParam.Height), IPL_DEPTH_8U, 1);
::cvSetData(Img_iplimage, imgParam.Src, imgParam.Width);
cvSaveImage("UsingIplimage.bmp", Img_iplimage);
cvReleaseImage(&Img_iplimage);
endTime = clock();
std::cout << "Speed of using IplImage to save image = " << endTime - beginTime << " ms" << std::endl;

return true;
}
catch (cv::Exception err)
{
std::cout << "When save image using iplimage have problem: " << err.what() << std::endl;

return false;
}
}

bool SaveImageUsingHalcon(ImageParam imgParam)
{
clock_t beginTime, endTime;
// method 3: Using Halcon HImage
try
{
beginTime = clock();
/*HalconCpp::HImage Img_halcon;*/
HalconCpp::HObject Img_halcon;
HalconCpp::GenImage1Extern(&Img_halcon, "byte", imgParam.Width, imgParam.Height, (__int64)(imgParam.Src), 0);
HalconCpp::WriteImage(Img_halcon, "bmp", 0, "UsingHalcon.bmp");
endTime = clock();
std::cout << "Speed of using Halcon, WriteImage to save image = " << endTime - beginTime << " ms" << std::endl;

return true;
}
catch (HalconCpp::HException err)
{
std::cout << "When save image using himage have problem: " << err.ErrorMessage() << std::endl;

return false;
}
}

int main(){
cv::Mat SrcImg;
SrcImg = cv::imread("D:\\Document\\program\\00.TestAndLearning\\Test\\TestSaveImageSpeedBetwnOpencvAndHalcon\\x64\\Debug\\left.bmp", 0);

// To simulate actual situation
ImageParam imgParam;
imgParam.Src = SrcImg.data;
imgParam.Height = SrcImg.rows;
imgParam.Width = SrcImg.cols;

SaveImageUsingMat(imgParam);
SaveImageUsingIplimage(imgParam);
SaveImageUsingHalcon(imgParam);

system("pause");
return 0;

}




沒有留言:

張貼留言