Commit 55417e1e8b561618ec296f38d3233c7f14fefb5c
1 parent
035d968f
fixed data type loading error in stim::image
Showing
2 changed files
with
27 additions
and
3 deletions
Show diff stats
matlab/stimImageStack.m renamed to matlab/stim_images2matrix.m
1 | -function S = stimImageStack(filemask) | 1 | +function S = stim_images2matrix(filemask) |
2 | +%This function loads a set of images as a 3D matrix. Color images are | ||
3 | +%converted to grayscale when loaded, so the resulting matrix is always 3D | ||
4 | +%with size X x Y x Z, where: | ||
5 | +% X is the size of the images along the X axis | ||
6 | +% Y is the size of the images along the Y axis | ||
7 | +% Z is the number of images | ||
8 | +% | ||
9 | +% all images are assumed to be the same size (though they do not have to | ||
10 | +% be the same file format or number of bits per pixel | ||
11 | + | ||
2 | files = dir(filemask); | 12 | files = dir(filemask); |
3 | 13 | ||
4 | %figure out the file size | 14 | %figure out the file size |
stim/image/image.h
@@ -53,6 +53,10 @@ class image{ | @@ -53,6 +53,10 @@ class image{ | ||
53 | void allocate(){ | 53 | void allocate(){ |
54 | unalloc(); | 54 | unalloc(); |
55 | img = (T*) malloc( sizeof(T) * R[0] * R[1] * R[2] ); //allocate memory | 55 | img = (T*) malloc( sizeof(T) * R[0] * R[1] * R[2] ); //allocate memory |
56 | + if (img == NULL) { | ||
57 | + std::cout << "stim::image ERROR - failed to allocate memory for image" << std::endl; | ||
58 | + exit(1); | ||
59 | + } | ||
56 | } | 60 | } |
57 | 61 | ||
58 | void allocate(size_t x, size_t y, size_t c){ //allocate memory based on the resolution | 62 | void allocate(size_t x, size_t y, size_t c){ //allocate memory based on the resolution |
@@ -228,6 +232,14 @@ public: | @@ -228,6 +232,14 @@ public: | ||
228 | } | 232 | } |
229 | } | 233 | } |
230 | #endif | 234 | #endif |
235 | + //Copy N data points from source to dest, casting while doing so | ||
236 | + template<typename S, typename D> | ||
237 | + void type_copy(S* source, D* dest, size_t N) { | ||
238 | + if (typeid(S) == typeid(D)) //if both types are the same | ||
239 | + memcpy(dest, source, N * sizeof(S)); //just use a memcpy | ||
240 | + for (size_t n = 0; n < N; n++) //otherwise, iterate through each element | ||
241 | + dest[n] = (D)source[n]; //copy and cast | ||
242 | + } | ||
231 | /// Load an image from a file | 243 | /// Load an image from a file |
232 | void load(std::string filename){ | 244 | void load(std::string filename){ |
233 | #ifdef USING_OPENCV | 245 | #ifdef USING_OPENCV |
@@ -236,13 +248,15 @@ public: | @@ -236,13 +248,15 @@ public: | ||
236 | std::cout<<"ERROR stim::image::load() - unable to find image "<<filename<<std::endl; | 248 | std::cout<<"ERROR stim::image::load() - unable to find image "<<filename<<std::endl; |
237 | exit(1); | 249 | exit(1); |
238 | } | 250 | } |
251 | + int cv_type = cvImage.type(); | ||
239 | int cols = cvImage.cols; | 252 | int cols = cvImage.cols; |
240 | int rows = cvImage.rows; | 253 | int rows = cvImage.rows; |
241 | int channels = cvImage.channels(); | 254 | int channels = cvImage.channels(); |
242 | allocate(cols, rows, channels); //allocate space for the image | 255 | allocate(cols, rows, channels); //allocate space for the image |
256 | + size_t img_bytes = bytes(); | ||
243 | unsigned char* cv_ptr = (unsigned char*)cvImage.data; | 257 | unsigned char* cv_ptr = (unsigned char*)cvImage.data; |
244 | - if(C() == 1) //if this is a single-color image, just copy the data | ||
245 | - memcpy(img, cv_ptr, bytes()); | 258 | + if (C() == 1) //if this is a single-color image, just copy the data |
259 | + type_copy<unsigned char, T>(cv_ptr, img, size()); | ||
246 | if(C() == 3) //if this is a 3-color image, OpenCV uses BGR interleaving | 260 | if(C() == 3) //if this is a 3-color image, OpenCV uses BGR interleaving |
247 | from_opencv(cv_ptr, X(), Y()); | 261 | from_opencv(cv_ptr, X(), Y()); |
248 | #else | 262 | #else |