[英]Moving my array to Mat and showing image with open CV
我在使用opencv顯示圖像時遇到問題。 由於我的代碼當前正在工作,因此我具有將78個大小為710X710的無符號短褲的圖像加載到單個數組中的功能。 我已經通過將數據寫入文件並使用imageJ讀取來驗證了此方法。 我現在正嘗試從數組中提取單個圖像幀並將其加載到Mat中,以便對其執行一些處理。 現在,我嘗試了兩種方法來執行此操作。 如果我不嘗試讀取輸出,但是如果我cout <,則代碼將編譯並運行
我的問題是,如何從尺寸為710 * 710的78張圖像的大型一維數組中提取數據到單個Mat圖像中。 還是有一種更有效的方法,可以將圖像加載到尺寸為710X710X78的3-D墊中,並根據需要在每個710X710切片上進行操作?
int main(int argc, char *argv[])
{
Mat OriginalMat, TestImage;
long int VImageSize = 710*710;
int NumberofPlanes = 78;
int FrameNum = 150;
unsigned short int *PlaneStack = new unsigned short int[NumberofPlanes*VImageSize];
unsigned short int *testplane = new unsigned short int[VImageSize];
/////Load PlaneStack/////
Load_Vimage(PlaneStack, Path, NumberofPlanes);
//Here I try to extract a single plane image to the mat testplane, I try it two different ways with the same results
memcpy(testplane, &PlaneStack[710*710*40], VImageSize*sizeof(unsigned short int));
//copy(&PlaneStack[VImageSize*40],&PlaneStack[VImageSize*41], testplane);
// move single plane to a mat file
OriginalMat = Mat(710,710,CV_8U, &testplane) ;
//cout<<OriginalMat;
namedWindow("Original");
imshow("Original", OriginalMat);
}
問題是您正在使用構造函數Mat::Mat(int rows, int cols, int type, void* data)
和指向16位數據(無符號short int)的指針,但是要指定CV_8U
類型(8位)。 因此,您的16位像素的第一個字節成為OriginalMat中的第一個像素,第一個像素的第二個字節變為OriginalMat中的第二個像素,依此類推。
您需要創建一個16位Mat,如果要顯示它,則將其轉換為8位,例如:
int main(int argc, char *argv[])
{
long int VImageSize = 710*710;
int NumberofPlanes = 78;
int FrameNum = 150;
/////Load PlaneStack/////
unsigned short int *PlaneStack = new unsigned short int[NumberofPlanes*VImageSize];
Load_Vimage(PlaneStack, Path, NumberofPlanes);
// Get a pointer to the plane we want to view
unsigned short int *testplane = &PlaneStack[710*710*40];
// "move" single plane to a mat file
// actually nothing gets moved, OriginalMat will just contain a pointer to your data.
Mat OriginalMat(710,710,CV_16UC1, &testplane) ;
double scale_factor = 1.0 / 256.0;
Mat DisplayMat;
OriginalMat.convertTo(DisplayMat, CV_8UC1, scale_factor);
namedWindow("Original");
imshow("Original", DisplayMat);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.