From f62516789cc8db6354500930a60a4cbc6f57a430 Mon Sep 17 00:00:00 2001 From: Tadas Baltrusaitis Date: Sat, 26 Aug 2017 17:01:54 +0100 Subject: [PATCH] Simplifying conversion to grayscale --- exe/FaceLandmarkImg/FaceLandmarkImg.cpp | 45 +------------------ .../FaceLandmarkVidMulti.cpp | 18 +------- .../include/LandmarkDetectorUtils.h | 5 +++ .../src/LandmarkDetectorFunc.cpp | 28 ++---------- .../src/LandmarkDetectorUtils.cpp | 42 +++++++++++++++++ 5 files changed, 54 insertions(+), 84 deletions(-) diff --git a/exe/FaceLandmarkImg/FaceLandmarkImg.cpp b/exe/FaceLandmarkImg/FaceLandmarkImg.cpp index b979e04f..028ed03f 100644 --- a/exe/FaceLandmarkImg/FaceLandmarkImg.cpp +++ b/exe/FaceLandmarkImg/FaceLandmarkImg.cpp @@ -74,47 +74,6 @@ vector get_arguments(int argc, char **argv) return arguments; } -void convert_to_grayscale(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(); - } - } -} - // Useful utility for creating directories for storing the output files void create_directory_from_file(string output_path) { @@ -362,9 +321,9 @@ int main(int argc, char **argv) return 1; } - // Making sure the image is in uchar grayscale + // Making sure the image is in uchar grayscale, TODO proper conversion, move out the function cv::Mat_ grayscale_image; - convert_to_grayscale(read_image, grayscale_image); + LandmarkDetector::convert_to_grayscale(read_image, grayscale_image); // If optical centers are not defined just use center of image diff --git a/exe/FaceLandmarkVidMulti/FaceLandmarkVidMulti.cpp b/exe/FaceLandmarkVidMulti/FaceLandmarkVidMulti.cpp index 98546e21..d060f285 100644 --- a/exe/FaceLandmarkVidMulti/FaceLandmarkVidMulti.cpp +++ b/exe/FaceLandmarkVidMulti/FaceLandmarkVidMulti.cpp @@ -239,21 +239,12 @@ int main(int argc, char **argv) while (!captured_image.empty()) { - // Reading the images - cv::Mat_ depth_image; + // Preparing the images cv::Mat_ grayscale_image; + LandmarkDetector::convert_to_grayscale(captured_image, grayscale_image); cv::Mat disp_image = captured_image.clone(); - if (captured_image.channels() == 3) - { - cv::cvtColor(captured_image, grayscale_image, CV_BGR2GRAY); - } - else - { - grayscale_image = captured_image.clone(); - } - vector > face_detections; bool all_models_active = true; @@ -407,11 +398,6 @@ int main(int argc, char **argv) cv::namedWindow("tracking_result", 1); cv::imshow("tracking_result", disp_image); - if (!depth_image.empty()) - { - // Division needed for visualisation purposes - imshow("depth", depth_image / 2000.0); - } } // output the tracked video diff --git a/lib/local/LandmarkDetector/include/LandmarkDetectorUtils.h b/lib/local/LandmarkDetector/include/LandmarkDetectorUtils.h index 56f113bc..4798f570 100644 --- a/lib/local/LandmarkDetector/include/LandmarkDetectorUtils.h +++ b/lib/local/LandmarkDetector/include/LandmarkDetectorUtils.h @@ -155,5 +155,10 @@ namespace LandmarkDetector // Skipping comments (lines starting with # symbol) void SkipComments(std::ifstream& stream); + //============================================================================ + // General utilty functions + //============================================================================ + void convert_to_grayscale(const cv::Mat& in, cv::Mat& out); + } #endif diff --git a/lib/local/LandmarkDetector/src/LandmarkDetectorFunc.cpp b/lib/local/LandmarkDetector/src/LandmarkDetectorFunc.cpp index ba6c9728..d9bb37f0 100644 --- a/lib/local/LandmarkDetector/src/LandmarkDetectorFunc.cpp +++ b/lib/local/LandmarkDetector/src/LandmarkDetectorFunc.cpp @@ -215,15 +215,7 @@ bool LandmarkDetector::DetectLandmarksInVideo(const cv::Mat &image, CLNF& clnf_m // and using a smaller search area cv::Mat grayscale_image; - if (image.channels() == 3) - { - cv::cvtColor(image, grayscale_image, CV_BGR2GRAY); - } - else - { - grayscale_image = image.clone(); - } - + convert_to_grayscale(image, grayscale_image); // Indicating that this is a first detection in video sequence or after restart bool initial_detection = !clnf_model.tracking_initialised; @@ -646,14 +638,7 @@ bool LandmarkDetector::DetectLandmarksInImage(const cv::Mat &image, const cv::Re { cv::Mat grayscale_image; - if (image.channels() == 3) - { - cv::cvtColor(image, grayscale_image, CV_BGR2GRAY); - } - else - { - grayscale_image = image.clone(); - } + convert_to_grayscale(image, grayscale_image); // Can have multiple hypotheses vector rotation_hypotheses; @@ -697,14 +682,7 @@ bool LandmarkDetector::DetectLandmarksInImage(const cv::Mat &image, const cv::Re bool LandmarkDetector::DetectLandmarksInImage(const cv::Mat &image, CLNF& clnf_model, FaceModelParameters& params) { cv::Mat grayscale_image; - if (image.channels() == 3) - { - cv::cvtColor(image, grayscale_image, CV_BGR2GRAY); - } - else - { - grayscale_image = image.clone(); - } + convert_to_grayscale(image, grayscale_image); cv::Rect_ bounding_box; diff --git a/lib/local/LandmarkDetector/src/LandmarkDetectorUtils.cpp b/lib/local/LandmarkDetector/src/LandmarkDetectorUtils.cpp index 6ce7e911..f2833a7a 100644 --- a/lib/local/LandmarkDetector/src/LandmarkDetectorUtils.cpp +++ b/lib/local/LandmarkDetector/src/LandmarkDetectorUtils.cpp @@ -1724,4 +1724,46 @@ void SkipComments(std::ifstream& stream) } } +// Some other utility functions +void convert_to_grayscale(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(); + } + } +} + }