From 839e1bc73821fa57fb437dff6978ddefd9f7187a Mon Sep 17 00:00:00 2001 From: Tadas Baltrusaitis Date: Wed, 8 Nov 2017 20:50:46 +0000 Subject: [PATCH] Working on sequence captures. --- exe/FeatureExtraction/FeatureExtraction.cpp | 4 +- lib/local/Utilities/include/SequenceCapture.h | 16 +- lib/local/Utilities/src/SequenceCapture.cpp | 138 ++++++++++++++++-- 3 files changed, 146 insertions(+), 12 deletions(-) diff --git a/exe/FeatureExtraction/FeatureExtraction.cpp b/exe/FeatureExtraction/FeatureExtraction.cpp index b07575f..243985d 100644 --- a/exe/FeatureExtraction/FeatureExtraction.cpp +++ b/exe/FeatureExtraction/FeatureExtraction.cpp @@ -332,7 +332,7 @@ int main (int argc, char **argv) while (!captured_image.empty()) { - // Grab the timestamp first + // Grab the timestamp first (TODO timestamp should be grabbed from sequence) if (video_input) { time_stamp = (double)frame_count * (1.0 / fps_vid_in); @@ -343,7 +343,7 @@ int main (int argc, char **argv) time_stamp = (double)frame_count * (1.0 / 30.0); } - // Reading the images + // Reading the images, TODO grayscale should be grabbed another way cv::Mat_ grayscale_image; if (captured_image.channels() == 3) diff --git a/lib/local/Utilities/include/SequenceCapture.h b/lib/local/Utilities/include/SequenceCapture.h index 553da0a..f639fed 100644 --- a/lib/local/Utilities/include/SequenceCapture.h +++ b/lib/local/Utilities/include/SequenceCapture.h @@ -55,7 +55,9 @@ namespace Utilities public: // Default constructor - SequenceCapture(); + SequenceCapture() {}; + + // TODO block copy, move etc. // Opening based on command line arguments bool Open(std::vector arguments); @@ -71,6 +73,16 @@ namespace Utilities // Video file bool OpenVideoFile(std::string video_file, float fx = -1, float fy = -1, float cx = -1, float cy = -1); + // Getting the next frame + cv::Mat GetNextFrame(); + + // Getting the most recent grayscale frame (need to call GetNextFrame first) + cv::Mat_ GetGrayFrame(); + + double GetProgress(); + + bool IsOpened(); + int frame_width; int frame_height; @@ -98,6 +110,8 @@ namespace Utilities // Length of video allowing to assess progress int vid_length; + bool img_grabbed; + }; } #endif \ No newline at end of file diff --git a/lib/local/Utilities/src/SequenceCapture.cpp b/lib/local/Utilities/src/SequenceCapture.cpp index 598a280..f48c417 100644 --- a/lib/local/Utilities/src/SequenceCapture.cpp +++ b/lib/local/Utilities/src/SequenceCapture.cpp @@ -42,12 +42,6 @@ using namespace Utilities; -// TODO initialize defaults -SequenceCapture::SequenceCapture(): -{ - -} - bool SequenceCapture::Open(std::vector arguments) { @@ -198,7 +192,22 @@ bool SequenceCapture::OpenWebcam(int device, int image_width, int image_height, this->fps = capture.get(CV_CAP_PROP_FPS); - // TODO estimate the fx, fy etc. + // If optical centers are not defined just use center of image + if (cx == -1) + { + cx = frame_width / 2.0f; + cy = frame_height / 2.0f; + } + // Use a rough guess-timate of focal length + if (fx == -1) + { + fx = 500 * (frame_width / 640.0); + fy = 500 * (frame_height / 480.0); + + fx = (fx + fy) / 2.0; + fy = fx; + } + return true; } @@ -229,13 +238,27 @@ bool SequenceCapture::OpenVideoFile(std::string video_file, float fx, float fy, return false; } - // TODO estimate the fx, fy etc. + // If optical centers are not defined just use center of image + if (cx == -1) + { + cx = frame_width / 2.0f; + cy = frame_height / 2.0f; + } + // Use a rough guess-timate of focal length + if (fx == -1) + { + fx = 500 * (frame_width / 640.0); + fy = 500 * (frame_height / 480.0); + + fx = (fx + fy) / 2.0; + fy = fx; + } return true; } -void SequenceCapture::OpenImageSequence(std::string directory, float fx, float fy, float cx, float cy) +bool SequenceCapture::OpenImageSequence(std::string directory, float fx, float fy, float cx, float cy) { image_files.clear(); @@ -265,6 +288,103 @@ void SequenceCapture::OpenImageSequence(std::string directory, float fx, float f return false; } + // Assume all images are same size in an image sequence + cv::Mat tmp = cv::imread(image_files[0], -1); + this->frame_height = tmp.size().height; + this->frame_width = tmp.size().width; + + // If optical centers are not defined just use center of image + if (cx == -1) + { + cx = frame_width / 2.0f; + cy = frame_height / 2.0f; + } + // Use a rough guess-timate of focal length + if (fx == -1) + { + fx = 500 * (frame_width / 640.0); + fy = 500 * (frame_height / 480.0); + + fx = (fx + fy) / 2.0; + fy = fx; + } + + // No fps as we have a sequence + this->fps = 0; + return true; } + +cv::Mat SequenceCapture::GetNextFrame() +{ + frame_num++; + + if (is_webcam && !is_image_seq) + { + + bool success = capture.read(latest_frame); + + if (!success) + { + // Indicate lack of success by returning an empty image + latest_frame = cv::Mat(); + } + } + else if (is_image_seq) + { + if (image_files.empty()) + { + // Indicate lack of success by returning an empty image + latest_frame = cv::Mat(); + } + + latest_frame = cv::imread(image_files[frame_num-1], -1); + } + + // Set the grayscale frame + if (grayFrame == nullptr) { + if (latestFrame->Width > 0) { + grayFrame = gcnew OpenCVWrappers::RawImage(latestFrame->Width, latestFrame->Height, CV_8UC1); + } + } + + if (grayFrame != nullptr) { + cvtColor(latestFrame->Mat, grayFrame->Mat, CV_BGR2GRAY); + } + + return latest_frame; +} + +double SequenceCapture::GetProgress() +{ + if (is_webcam) + { + return -1.0; + } + else + { + return (double)frame_num / (double)vid_length; + } +} + +bool SequenceCapture::IsOpened() +{ + if (is_webcam || !is_image_seq) + return capture.isOpened(); + else + return (image_files.size() > 0 && frame_num < image_files.size()); +} + +cv::Mat_ SequenceCapture::GetGrayFrame() { + if (img_grabbed) + { + img_grabbed = false; + return latest_gray_frame; + } + else + { + std::cout << "Need to call GetNextFrame(), before calling GetGrayFrame() " << std::endl; + return cv::Mat_(); + } +} \ No newline at end of file