2010-07-16 5 views
0

OpenCV 1.1 방식으로 작성된 (예 : IplImages) 이전 OpenCV 코드를 업데이트하고 있습니다.OpenCV : 여러 이미지로드

내가 지금 달성하고자하는 것은 일련의 이미지 (명령 줄 인수로 전달 된 이미지)를 Mats로로드하는 것입니다. 이것은 더 큰 작업의 일부입니다. 아래의 첫 번째 코드 샘플은 이전 코드의 이미지로드 메소드입니다. 명령 줄에서 5 개의 이미지를로드하고 순서대로 표시하고 각각 이후의 키 히트를 위해 일시 ​​중지 한 다음 종료합니다.

두 번째 코드 샘플은 Mat을 사용하는 업데이트 버전입니다. 지금까지는 제대로 작동하지만 이것이 가장 좋은 방법입니까? 저는 Mats 배열을 사용했습니다. 대신 Mats에 대한 포인터 배열을 사용해야합니까? 그리고 실행 시간에 이미지 수가 argc에서 결정되고 미리 IMAGE_NUM으로 설정하지 않아도되도록이 작업을 수행 할 수있는 방법이 있습니다.

기본적으로 명령 줄 인수로 이미지의 숫자를 전달할 수 있고 나중에 참조 할 수 있도록 편리한 배열이나 다른 유사한 저장소에로드 할 수 있기를 바랍니다.

감사합니다.

오래된 코드 :

#include <iostream> 
#include <cv.h> 
#include <cxcore.h> 
#include <highgui.h> 
using namespace std; 
using namespace cv; 

// the number of input images 
#define IMAGE_NUM 5 

int main(int argc, char **argv) 
{ 
    uchar **imgdata; 
    IplImage **img; 
    int index = 0; 
    char *img_file[IMAGE_NUM]; 

    cout << "Loading files" << endl; 
    while(++index < argc) 
     if (index <= IMAGE_NUM) 
      img_file[index-1] = argv[index]; 

    // malloc memory for images 
    img = (IplImage **)malloc(IMAGE_NUM * sizeof(IplImage *)); // Allocates memory to store just an IplImage pointer for each image loaded 
    imgdata = (uchar **)malloc(IMAGE_NUM * sizeof(uchar *)); 

    // load images. Note: cvLoadImage actually allocates the memory for the images 
    for (index = 0; index < IMAGE_NUM; index++) { 
     img[index] = cvLoadImage(img_file[index], 1); 
     if (!img[index]->imageData){ 
      cout << "Image data not loaded properly" << endl; 
      return -1; 
     } 
     imgdata[index] = (uchar *)img[index]->imageData; 
    } 

    for (index = 0; index < IMAGE_NUM; index++){ 
     imshow("myWin", img[index]); 
     waitKey(0); 
    } 

    cvDestroyWindow("myWin"); 
    cvReleaseImage(img); 

    return 0; 
} 

새로운 코드 : 예를 들어

#include <iostream> 
#include <cv.h> 
#include <cxcore.h> 
#include <highgui.h> 
#include <time.h> 
#include <vector> 
using namespace std; 
using namespace cv; 

int main(int argc, char **argv) 
{ 
    vector<Mat> img; 
    //Mat img[IMAGE_NUM]; 
    int index = 0; 

    for (index = 0; index < IMAGE_NUM; index++) { 

     //img[index] = imread(argv[index+1]); 
     img.push_back(imread(argy[index+1])); 

     if (!img[index].data){ 
      cout << "Image data not loaded properly" << endl; 
      cin.get(); 
      return -1; 
     } 
    } 

    vector<Mat>::iterator it; 

    for (it = img.begin(); it != img.end() ; it++) { 
     imshow("myWin", (*it)); 
     waitKey(0); 
    } 
    cvDestroyWindow("myWin"); 
    return 0; 
} 

답변

2

돌아 가려고 잠시 동안 이,하지만 내가 뭘했는지는 Gootik의 제안과 기능적으로 같을 것이다. 이것은 나를 위해 잘 작동했습니다. Mat& (즉, 단일 cv::Mat)을 사용하는 함수의 경우 매트 배열을 참조 해제하여 전달할 수 있습니다.이 표기법은 Matlab에서 많은 이미지 처리 작업을 수행 한 후 더욱 편안합니다.

#include <iostream> 
#include <cv.h> 
#include <cxcore.h> 
#include <highgui.h> 
using namespace std; 
using namespace cv; 

int main(int argc, char **argv) 
{ 
    if (argc==1){ 
     cout << "No images to load!" << endl; 
     cin.get(); 
     return 0; 
    } 

    int index = 0; 
    int image_num = argc-1; 

    Mat *img = new Mat[image_num]; // allocates table on heap instead of stack 

    // Load the images from command line: 
    for (index = 0; index < image_num; index++) { 
     img[index] = imread(argv[index+1]); 
     if (!img[index].data){ 
      cout << "Image data not loaded properly" << endl; 
      cin.get(); 
      return -1; 
     } 
    } 

    for (index = 0; index < image_num; index++) { 
     imshow("myWin", img[index]); 
     waitKey(0); 
    } 
    cvDestroyWindow("myWin"); 

    delete [] img; // notice the [] when deleting an array. 
    return 0; 
} 
0

그것은 나를 데려 : 당신이 배열 대신 벡터를 사용할 수 있습니다

#include <iostream> 
#include <cv.h> 
#include <cxcore.h> 
#include <highgui.h> 
#include <time.h> 
using namespace std; 
using namespace cv; 

// the number of input images 
#define IMAGE_NUM 5 

int main(int argc, char **argv) 
{ 
    Mat img[IMAGE_NUM]; 
    int index = 0; 

    for (index = 0; index < IMAGE_NUM; index++) { 
     img[index] = imread(argv[index+1]); 
     if (!img[index].data){ 
      cout << "Image data not loaded properly" << endl; 
      cin.get(); 
      return -1; 
     } 
    } 

    for (index = 0; index < IMAGE_NUM; index++) { 
     imshow("myWin", img[index]); 
     waitKey(0); 
    } 
    cvDestroyWindow("myWin"); 
    return 0; 
} 
+0

빠른 업데이트 : Gootik이 대답 할 당시 Standad Template Library의 std :: vector에 익숙하지 않았으며 그의 제안에서 가치를 보지 못했습니다. 이제는 벡터에 더 익숙해 졌으므로 제 제안이 더 마음에 들었습니다. 제 목적이 잘 작동 했었지만 말입니다. – SSilk