2016-04-01 18 views
0

Mat'ın bir vektörüne bir 3 boyutlu görüntüyü (ikili dosya olarak depolanmış) okumaya çalışıyorum (böylece dilime göre dilimi okuyabileyim) ancak her zaman çalışmayı denerken bana bölümleme hatası veriyor create_mat. Neyin yanlış gittiğini bulmak için uğraşıyorum. Bu, verileri ilk sırada okumak için en iyi yol mu?vec görüntülerini okuma <Mat> 16bit hatası

#include <cstdint> 
#include <string> 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <iostream> 
using namespace cv; 
using namespace std; 
void read_tiff(string filename,unsigned long length,uint16_t* buffer) 
{ 
    //Reads a binary file containing little endean array of uint16_t into a single array pointed by buffer 
    FILE *ptr_file; 
    ptr_file = fopen(filename.c_str(),"rb"); 
    rewind(ptr_file); 
    uint16_t temp; 
    unsigned int k; 
    for (k=0;k<length;k++) 
    { 
     fread(&temp,2,1,ptr_file); 
     buffer[k] = temp;  
    } 
    fclose(ptr_file); 
    return; 
} 
void create_mat(vector<Mat> dst,const int width,const int height,const int stacks,uint16_t* src) 
{ 
    int i,j,k; 
    int counter = 0; 
    for (i=0;i<stacks;i++) 
    { 
     for (j=0;j<height;j++) 
    { 
     for (k=0;k<width;k++) 
     { 
      (dst[i]).at<ushort>(j,k)= src[counter]; 
      cout<<src[counter]<<endl; 
      //   cout<<dst[i].at<ushort>(j,k)<<endl; 
      counter++; 
     } 
    } 
    } 

} 
int main() 
{ 
    string dir; 
    dir = "/somedir.raw"; 
    cout<<dir<<std::endl; 
    unsigned long length = 1365ul*1531ul*1265ul; 
    uint16_t test[length]; 
    read_tiff(dir,length,test); 
    int size[3] = {1265,1365,1531}; 
    vector<Mat> img(size[0],Mat(size[1],size[2],CV_16UC1)); 
    cout <<"image loading done"<<endl; 
    create_mat(img,size[1],size[2],size[0],test); 
    imwrite("test.jpg",img[400]); 
    return 0; 
} 
+0

tarafından işaretlenir. Bunun yerine referans ile geçmek mi istiyorsunuz? –

+0

belki de hata değil ama img (boyut [0], Mat (boyut [1], boyut [2], CV_16UC1)); Her vektör elemanı (= her bir Mat) aynı veri belleğine bağlanacağından problemlere yol açacaktır! – Micka

+0

genişlik ve yükseklik değiştirildi. Mat yapıcı Mat (yükseklik, genişlik, ...) – Micka

cevap

0

Ben yorum olarak bahsedilen 2 sorunları giderir bu kodu, deneyin:

Kod değişiklikler create_mat fonksiyon vektörü img bir kopyasını yapar CHANGED:

#include <cstdint> 
#include <string> 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <iostream> 
using namespace cv; 
using namespace std; 
void read_tiff(string filename,unsigned long length,uint16_t* buffer) 
{ 
    //Reads a binary file containing little endean array of uint16_t into a single array pointed by buffer 
    FILE *ptr_file; 
    ptr_file = fopen(filename.c_str(),"rb"); 
    rewind(ptr_file); 
    uint16_t temp; 
    unsigned int k; 
    for (k=0;k<length;k++) 
    { 
     fread(&temp,2,1,ptr_file); 
     buffer[k] = temp;  
    } 
    fclose(ptr_file); 
    return; 
} 
void create_mat(vector<Mat> dst,const int width,const int height,const int stacks,uint16_t* src) 
{ 
    int i,j,k; 
    int counter = 0; 
    for (i=0;i<stacks;i++) 
    { 
     for (j=0;j<height;j++) 
    { 
     for (k=0;k<width;k++) 
     { 
      (dst[i]).at<ushort>(j,k)= src[counter]; 
      cout<<src[counter]<<endl; 
      //   cout<<dst[i].at<ushort>(j,k)<<endl; 
      counter++; 
     } 
    } 
    } 

} 
int main() 
{ 
    string dir; 
    dir = "/somedir.raw"; 
    cout<<dir<<std::endl; 
    unsigned long length = 1365ul*1531ul*1265ul; 
    uint16_t test[length]; 
    read_tiff(dir,length,test); 
    int size[3] = {1265,1365,1531}; 
    // CHANGED: Instead of allocating Mat memory once and copy-constructing the Mat-header, create a new Mat header + memory for each vector element. Otherwise all the elements will share their pixel data (if you don't call a function that assigns new memory to the Mat) 
    //vector<Mat> img(size[0],Mat(size[1],size[2],CV_16UC1)); 
    vector<Mat> img(size[0]); 
    for(int i=0; i<size[0]; ++i) 
     img[i] = Mat(size[2],size[1],CV_16UC1); // CHANGED: swapped height and width, because Mat constructor uses height-first 


    cout <<"image loading done"<<endl; 
    create_mat(img,size[1],size[2],size[0],test); 
    imwrite("test.jpg",img[400]); 
    return 0; 
} 
+1

Çok teşekkürler! Bu öyle. – henryhetired

İlgili konular