From 4b54738b688c34ee2e30dea945b12e595ed02afe Mon Sep 17 00:00:00 2001 From: Tadas Baltrusaitis Date: Wed, 8 Nov 2017 21:13:02 +0000 Subject: [PATCH] More work on sequence capture. --- exe/FaceLandmarkImg/FaceLandmarkImg.cpp | 1 + lib/local/Utilities/src/SequenceCapture.cpp | 71 +++++++++++++++------ 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/exe/FaceLandmarkImg/FaceLandmarkImg.cpp b/exe/FaceLandmarkImg/FaceLandmarkImg.cpp index 8050611..4b0336f 100644 --- a/exe/FaceLandmarkImg/FaceLandmarkImg.cpp +++ b/exe/FaceLandmarkImg/FaceLandmarkImg.cpp @@ -72,6 +72,7 @@ vector get_arguments(int argc, char **argv) return arguments; } +// TODO rem void convert_to_grayscale(const cv::Mat& in, cv::Mat& out) { if(in.channels() == 3) diff --git a/lib/local/Utilities/src/SequenceCapture.cpp b/lib/local/Utilities/src/SequenceCapture.cpp index f48c417..2f14c7d 100644 --- a/lib/local/Utilities/src/SequenceCapture.cpp +++ b/lib/local/Utilities/src/SequenceCapture.cpp @@ -40,6 +40,9 @@ #include #include +// OpenCV includes +#include + using namespace Utilities; bool SequenceCapture::Open(std::vector arguments) @@ -274,7 +277,7 @@ bool SequenceCapture::OpenImageSequence(std::string directory, float fx, float f for (std::vector::const_iterator file_iterator(file_in_directory.begin()); file_iterator != file_in_directory.end(); ++file_iterator) { // Possible image extension .jpg and .png - if (file_iterator->extension().string().compare(".jpg") == 0 || file_iterator->extension().string().compare(".png") == 0) + if (file_iterator->extension().string().compare(".jpg") == 0 || file_iterator->extension().string().compare(".jpeg") == 0 || file_iterator->extension().string().compare(".png") == 0 || file_iterator->extension().string().compare(".bmp") == 0) { curr_dir_files.push_back(file_iterator->string()); } @@ -316,6 +319,48 @@ bool SequenceCapture::OpenImageSequence(std::string directory, float fx, float f } +void convertToGrayscale(const cv::Mat& in, cv::Mat& out) +{ + if (in.channels() == 3) + { + // Make sure it's in a correct format + if (in.depth() != CV_8U) + { + if (in.depth() == CV_16U) + { + cv::Mat tmp = in / 256; + tmp.convertTo(tmp, CV_8U); + cv::cvtColor(tmp, out, CV_BGR2GRAY); + } + } + else + { + cv::cvtColor(in, out, CV_BGR2GRAY); + } + } + else if (in.channels() == 4) + { + cv::cvtColor(in, out, CV_BGRA2GRAY); + } + else + { + if (in.depth() == CV_16U) + { + cv::Mat tmp = in / 256; + out = tmp.clone(); + } + else if (in.depth() != CV_8U) + { + in.convertTo(out, CV_8U); + } + else + { + out = in.clone(); + } + } +} + + cv::Mat SequenceCapture::GetNextFrame() { frame_num++; @@ -343,15 +388,7 @@ cv::Mat SequenceCapture::GetNextFrame() } // 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); - } + convertToGrayscale(latest_frame, latest_gray_frame); return latest_frame; } @@ -376,15 +413,7 @@ bool SequenceCapture::IsOpened() 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_(); - } +cv::Mat_ SequenceCapture::GetGrayFrame() +{ + return latest_gray_frame; } \ No newline at end of file