2013-07-03 39 views
1

我想在我的程序中加載大量圖像,稍後將它們縫合在一起。我正在使用命令行來提及圖像地址。它的工作原理,如果我事先知道圖像的數量我會在輸入,但它並不適用於輸入圖像的變量數:如何使用命令行參數在opencv中讀取多個圖像文件?

if (argc < 3) 
{ 
    cerr<<"Usage:" << argv[0] << "SOURCE DESTINATION" << endl; 
    return 1; 
} 

for (int i = 0;i <argc;i++) 
{ 
    Mat img[i] = imread(argv[i+1], 0); 
    imshow("image", img[i]); 
} 

是否有其他方法來獲得在這種情況下輸入。我需要處理20到25張圖像。我可以使用一些文件,存儲地址並閱讀它。我認爲這是可能的,但它是如何工作的?

編輯:

一些修改後,我的程序現在這個樣子:

#include <stdio.h> 
#include <iostream> 
#include <stdlib.h> 
#include "opencv2/core/core.hpp" 
#include "opencv2/features2d/features2d.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc_c.h" 
#include <opencv2/imgproc/imgproc.hpp> 
#include "opencv2/calib3d/calib3d.hpp" 
#include <math.h> 
#include <cmath> 

#define PI 3.14159 
#define max_img 25 

using namespace cv; 
using namespace std; 

//main function 
int main(int argc, char** argv) 
{const int MAX_NUM_OF_IMG = 1024; 
cv::Mat img[MAX_NUM_OF_IMG]; 
for(int i=1;i<argc;i++) 
    { 
std::cout << i << ": " << argv[i] << std::endl; 
    img[i-1] = imread(argv[i],0); 
std::cout << "Finished reading images1" << std::endl; 
    imshow("image",img[i]); 
//start stitching operations 

    }std::cout << "Finished reading images2" << std::endl; 
cv::waitKey(); 
return 0; 
} 

我給在命令行下面:

./img_many camera3/imageb-032.jpgcamera3/imageb-033.jpgcamera3/imageb-034.jpgcamera3/imageb-035.jpgcamera3/imageb-036.jpgcamera3/imageb-037.jpgcamera3/imageb-038.jpgcamera3/imageb-039.jpg 

我得到的輸出是

1: camera3/imageb-032.jpgcamera3/imageb-033.jpgcamera3/imageb-034.jpgcamera3/imageb-035.jpgcamera3/imageb-036.jpgcamera3/imageb-037.jpgcamera3/imageb-038.jpgcamera3/imageb-039.jpg 
Finished reading images1 

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482 
terminate called after throwing an instance of 'cv::Exception' 
    what(): /build/buildd/opencv-2.3.1/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat 

Aborted (core dumped) 

是否可以從命令行讀取多個圖像,並將它們作爲參數?它爲什麼會崩潰?

+0

你試過imshow(「image」,imread(argv [i + 1],0)); ? – Alexis

+0

如果將圖像拼接在一起,您可能會發現它有幫助。不完整,而是一個開始。 http://stackoverflow.com/questions/13893464/in-opencv-how-do-i-copy-and-scale-a-greyscale-image-into-another-colour-image – Emile

回答

2

我不認爲這編譯。 您必須分配圈外CV的數組::墊

const int MAX_NUM_OF_IMG = 1024; 
cv::Mat img[MAX_NUM_OF_IMG]; //You could also allocate this dynamically based on the number of arguments/the value of argc 

然後,在循環中,你正在打破界限,你必須改變這樣的:

for(int i=1;i<argc;i++) //You should also check you have not more than MAX_NUM_OF_IMG 
    { 
    img[i-1] = imread(argv[i],0); ///What is zero by the way 
    imshow("image",img[i]); 
    //Probably here you want some short sleep, or a "press a key" event 
    } 

你得到的圖像的數量是argc-1

然後,如果你想擁有存儲在一些txt文件的文件名,你可以使用類似的std :: ifstream的解析它,因爲它是給std :: cin 。有成千上萬的例子,像這樣How To Parse String File Txt Into Array With C++

+1

零指如果圖像將被讀取作爲灰度或不,如果我沒有錯 –

+1

謝謝安東尼奧! Boyko就像你說的那樣,零是用來裝灰度的。 –

+0

Anotonio,我試過,收到這個錯誤在運行時:OpenCV的錯誤:在cvGetMat爲標誌(參數或結構域)(無法識別的或不支持的陣列型),文件/build/buildd/opencv-2.3.1/modules/core /build/buildd/opencv-2.3.1/modules/core/src/array.cpp:2482:/src/array.cpp,線路2482 終止拋出 'CV ::例外' 的一個實例後 什麼()被稱爲:錯誤:在功能cvGetMat(-206)無法識別的或不支持的陣列型 中止(核心轉儲) –

1

如果在你的負載循環,你會基本上得到閃爍的負載有imshow。只需將你的iamges首先加載到你的數組中。

for(int i=1;i<argc;i++) 
{ 
    std::cout << i << ": " << argv[i] << std::endl; 
    img[i-1] = imread(argv[i],0); 
    std::cout << "Finished reading images1" << std::endl;   
} 

然後嘗試顯示您的圖像。

imshow("image",img[0]); 

您可以嘗試通過顯示您的圖片添加一個按鍵循環。我認爲有一個openCV教程已經顯示了這一點。

我敢肯定,我已經看到了這個錯誤了幾次,每次都出於不同的原因。圖像只是數組,所以你的圖像有一些問題。這可能是一個糟糕的JPG。

通過在循環不顯示的每個圖像,你會希望看到如果有一個形象造成問題。

還要確保「我」在你的循環不會到達25,即一個出界異常。你也可以放入try/catch語句來幫助識別錯誤發生的時間。

希望能幫助您縮小問題範圍。

+0

謝謝,埃米爾。有用。 –

相關問題