diff --git a/autoscoper/src/ui/AutoscoperMainWindow.cpp b/autoscoper/src/ui/AutoscoperMainWindow.cpp index 57fd26b5..ccff125b 100644 --- a/autoscoper/src/ui/AutoscoperMainWindow.cpp +++ b/autoscoper/src/ui/AutoscoperMainWindow.cpp @@ -902,8 +902,13 @@ void AutoscoperMainWindow::setPose(std::vector pose, unsigned int volume void AutoscoperMainWindow::setBackground(double threshold) { if (background_threshold_ < 0){ - for (xromm::Video &vi : tracker->trial()->videos) - vi.create_background_image(); + for (xromm::Video& vi : tracker->trial()->videos) { + if (vi.create_background_image() != 1) { + std::cerr << "Error creating background image for video " << vi.dirname() << "\n" + << "Failed to set background threshold" << std::endl; + return; + } + } tracker->updateBackground(); } diff --git a/libautoscoper/src/Video.cpp b/libautoscoper/src/Video.cpp index bfd3f6f0..7086445a 100644 --- a/libautoscoper/src/Video.cpp +++ b/libautoscoper/src/Video.cpp @@ -142,9 +142,10 @@ Video::operator=(const Video& video) int Video::create_background_image() { - if (filenames_.size() < 2) + if (filenames_.size() < 2) { + std::cerr << "Video::create_background_image(): Not enough images to create background image." << std::endl; return -1; - + } if (background_) delete[] background_; background_ = new float[width()*height()]; @@ -158,7 +159,7 @@ int Video::create_background_image() for (int i = 0; i < filenames_.size(); i++){ tif = TIFFOpen(filenames_.at(i).c_str(), "r"); if (!tif) { - std::cerr << "Video::frame(): Unable to open image. " << std::endl; + std::cerr << "Video::create_background_image(): Unable to open image. " << std::endl; return -2; } @@ -166,26 +167,17 @@ int Video::create_background_image() tiffImageRead(tif, tmp_image); TIFFClose(tif); - if (tmp_image->bitsPerSample == 8){ - unsigned char * ptr = reinterpret_cast (tmp_image->data); - float* ptr_b = background_; - for (; ptr < reinterpret_cast(tmp_image->data) + tmp_image->dataSize; ptr++, ptr_b++) - { - float val = *ptr; - if (val / 255 > *ptr_b) - *ptr_b = val / 255; - } - } - else + switch (tmp_image->bitsPerSample) { - unsigned short * ptr = static_cast (tmp_image->data); - float * ptr_b = background_; - for (; ptr < reinterpret_cast(tmp_image->data) + tmp_image->dataSize; ptr++, ptr_b++) - { - float val = *ptr; - if (val / 65535 > *ptr_b) - *ptr_b = val / 65535; - } + case 8: create_background_image_internal(tmp_image, 255); + break; + case 16: create_background_image_internal(tmp_image, 65535); + break; + case 32: create_background_image_internal(tmp_image, 4294967295); + break; + default: + std::cerr << "Video::create_background_image(): Unsupported bits per sample. " << std::endl; + return -3; } } @@ -194,6 +186,20 @@ int Video::create_background_image() return 1; } +template void Video::create_background_image_internal(TiffImage* tmp_img, unsigned int normalization_factor) { + static_assert(std::is_same::value || std::is_same::value || std::is_same::value, "T must be of type unsigned char, unsigned short, or unsigned int"); + T* start = reinterpret_cast(tmp_img->data); + T* end = start + (tmp_img->dataSize / sizeof(T)); + T* iter = start; + float* bg = background_; + while(iter < end) { + float val = *iter; + if (val / normalization_factor > *bg) + *bg = val / normalization_factor; + iter++; + bg++; + } +} void diff --git a/libautoscoper/src/Video.hpp b/libautoscoper/src/Video.hpp index d7d91d16..7a1b30eb 100644 --- a/libautoscoper/src/Video.hpp +++ b/libautoscoper/src/Video.hpp @@ -97,6 +97,7 @@ class Video const float* background() const { return background_; } private: + template void create_background_image_internal(TiffImage* tmp_img, unsigned int normalization_factor); std::string dirname_;