| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | #include "stdafx_ut.h" |
| |
|
| | #include "ImageCapture.h" |
| | #include "ImageManipulationHelpers.h" |
| |
|
| | using namespace Utilities; |
| |
|
| | #define INFO_STREAM( stream ) \ |
| | std::cout << stream << std::endl |
| |
|
| | #define WARN_STREAM( stream ) \ |
| | std::cout << "Warning: " << stream << std::endl |
| |
|
| | #define ERROR_STREAM( stream ) \ |
| | std::cout << "Error: " << stream << std::endl |
| |
|
| | bool ImageCapture::Open(std::vector<std::string>& arguments) |
| | { |
| |
|
| | |
| | bool* valid = new bool[arguments.size()]; |
| |
|
| | for (size_t i = 0; i < arguments.size(); ++i) |
| | { |
| | valid[i] = true; |
| | } |
| |
|
| | |
| | std::string input_root = ""; |
| | fx = -1; fy = -1; cx = -1; cy = -1; |
| |
|
| | std::string separator = std::string(1, fs::path::preferred_separator); |
| |
|
| | |
| | for (size_t i = 0; i < arguments.size(); ++i) |
| | { |
| | if (arguments[i].compare("-root") == 0) |
| | { |
| | input_root = arguments[i + 1] + separator; |
| | i++; |
| | } |
| | if (arguments[i].compare("-inroot") == 0) |
| | { |
| | input_root = arguments[i + 1] + separator; |
| | i++; |
| | } |
| | } |
| |
|
| | std::string input_directory; |
| | std::string bbox_directory; |
| |
|
| | bool directory_found = false; |
| | has_bounding_boxes = false; |
| |
|
| | std::vector<std::string> input_image_files; |
| |
|
| | for (size_t i = 0; i < arguments.size(); ++i) |
| | { |
| | if (arguments[i].compare("-f") == 0) |
| | { |
| | input_image_files.push_back(input_root + arguments[i + 1]); |
| | valid[i] = false; |
| | valid[i + 1] = false; |
| | i++; |
| | } |
| | else if (arguments[i].compare("-fdir") == 0) |
| | { |
| | if (directory_found) |
| | { |
| | WARN_STREAM("Input directory already found, using the first one:" + input_directory); |
| | } |
| | else |
| | { |
| | input_directory = (input_root + arguments[i + 1]); |
| | valid[i] = false; |
| | valid[i + 1] = false; |
| | i++; |
| | directory_found = true; |
| | } |
| | } |
| | else if (arguments[i].compare("-bboxdir") == 0) |
| | { |
| | bbox_directory = (input_root + arguments[i + 1]); |
| | valid[i] = false; |
| | valid[i + 1] = false; |
| | has_bounding_boxes = true; |
| | i++; |
| | } |
| | else if (arguments[i].compare("-fx") == 0) |
| | { |
| | std::stringstream data(arguments[i + 1]); |
| | data >> fx; |
| | i++; |
| | } |
| | else if (arguments[i].compare("-fy") == 0) |
| | { |
| | std::stringstream data(arguments[i + 1]); |
| | data >> fy; |
| | i++; |
| | } |
| | else if (arguments[i].compare("-cx") == 0) |
| | { |
| | std::stringstream data(arguments[i + 1]); |
| | data >> cx; |
| | i++; |
| | } |
| | else if (arguments[i].compare("-cy") == 0) |
| | { |
| | std::stringstream data(arguments[i + 1]); |
| | data >> cy; |
| | i++; |
| | } |
| | } |
| |
|
| | for (int i = (int)arguments.size() - 1; i >= 0; --i) |
| | { |
| | if (!valid[i]) |
| | { |
| | arguments.erase(arguments.begin() + i); |
| | } |
| | } |
| |
|
| | |
| | if (!input_image_files.empty()) |
| | { |
| | return OpenImageFiles(input_image_files, fx, fy, cx, cy); |
| | } |
| | if (!input_directory.empty()) |
| | { |
| | return OpenDirectory(input_directory, bbox_directory, fx, fy, cx, cy); |
| | } |
| |
|
| | |
| | no_input_specified = true; |
| |
|
| | return false; |
| | } |
| |
|
| | bool ImageCapture::OpenImageFiles(const std::vector<std::string>& image_files, float fx, float fy, float cx, float cy) |
| | { |
| | |
| | frame_num = 0; |
| | no_input_specified = false; |
| |
|
| | latest_frame = cv::Mat(); |
| | latest_gray_frame = cv::Mat(); |
| | this->image_files = image_files; |
| |
|
| | |
| | if (fx != -1 && fy != -1 ) |
| | { |
| | image_focal_length_set = true; |
| | this->fx = fx; |
| | this->fy = fy; |
| |
|
| | } |
| | else |
| | { |
| | image_focal_length_set = false; |
| | } |
| |
|
| | if (cx != -1 && cy != -1) |
| | { |
| | this->cx = cx; |
| | this->cy = cy; |
| | image_optical_center_set = true; |
| | } |
| | else |
| | { |
| | image_optical_center_set = false; |
| | } |
| |
|
| | return true; |
| |
|
| | } |
| |
|
| | bool ImageCapture::OpenDirectory(std::string directory, std::string bbox_directory, float fx, float fy, float cx, float cy) |
| | { |
| | INFO_STREAM("Attempting to read from directory: " << directory); |
| |
|
| | |
| | frame_num = 0; |
| | no_input_specified = false; |
| |
|
| | image_files.clear(); |
| |
|
| | fs::path image_directory(directory); |
| | std::vector<fs::path> file_in_directory; |
| | copy(fs::directory_iterator(image_directory), fs::directory_iterator(), back_inserter(file_in_directory)); |
| |
|
| | |
| | sort(file_in_directory.begin(), file_in_directory.end()); |
| |
|
| | std::vector<std::string> curr_dir_files; |
| |
|
| | for (std::vector<fs::path>::const_iterator file_iterator(file_in_directory.begin()); file_iterator != file_in_directory.end(); ++file_iterator) |
| | { |
| | |
| | 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()); |
| |
|
| | |
| | if (!bbox_directory.empty()) |
| | { |
| | fs::path current_file = *file_iterator; |
| | fs::path bbox_file = bbox_directory / current_file.filename().replace_extension("txt"); |
| | |
| | |
| | if (fs::exists(bbox_file)) |
| | { |
| | std::ifstream in_bbox(bbox_file.string().c_str(), std::ios_base::in); |
| |
|
| | std::vector<cv::Rect_<float> > bboxes_image; |
| |
|
| | |
| | while (!in_bbox.eof()) |
| | { |
| | std::string bbox_string; |
| | std::getline(in_bbox, bbox_string); |
| |
|
| | if (bbox_string.empty()) |
| | continue; |
| |
|
| | std::stringstream ss(bbox_string); |
| |
|
| | float min_x, min_y, max_x, max_y; |
| |
|
| | ss >> min_x >> min_y >> max_x >> max_y; |
| | bboxes_image.push_back(cv::Rect_<float>(min_x, min_y, max_x - min_x, max_y - min_y)); |
| | } |
| | in_bbox.close(); |
| |
|
| | bounding_boxes.push_back(bboxes_image); |
| | } |
| | else |
| | { |
| | ERROR_STREAM("Could not find the corresponding bounding box for file:" + file_iterator->string()); |
| | exit(1); |
| | } |
| | } |
| | } |
| | } |
| |
|
| | image_files = curr_dir_files; |
| |
|
| | if (image_files.empty()) |
| | { |
| | std::cout << "No images found in the directory: " << directory << std::endl; |
| | return false; |
| | } |
| |
|
| | |
| | if (fx != -1 && fy != -1) |
| | { |
| | image_focal_length_set = true; |
| | this->fx = fx; |
| | this->fy = fy; |
| |
|
| | } |
| | else |
| | { |
| | image_focal_length_set = false; |
| | } |
| |
|
| | if (cx != -1 && cy != -1) |
| | { |
| | this->cx = cx; |
| | this->cy = cy; |
| | image_optical_center_set = true; |
| | } |
| | else |
| | { |
| | image_optical_center_set = false; |
| | } |
| |
|
| | return true; |
| |
|
| | } |
| |
|
| | void ImageCapture::SetCameraIntrinsics(float fx, float fy, float cx, float cy) |
| | { |
| | |
| | if (cx == -1) |
| | { |
| | this->cx = this->image_width / 2.0f; |
| | this->cy = this->image_height / 2.0f; |
| | } |
| | else |
| | { |
| | this->cx = cx; |
| | this->cy = cy; |
| | } |
| | |
| | if (fx == -1) |
| | { |
| | this->fx = 500.0f * (this->image_width / 640.0f); |
| | this->fy = 500.0f * (this->image_height / 480.0f); |
| |
|
| | this->fx = (this->fx + this->fy) / 2.0f; |
| | this->fy = this->fx; |
| | } |
| | else |
| | { |
| | this->fx = fx; |
| | this->fy = fy; |
| | } |
| | } |
| |
|
| | |
| | cv::Mat ImageCapture::GetNextImage() |
| | { |
| | if (image_files.empty() || frame_num >= image_files.size()) |
| | { |
| | |
| | latest_frame = cv::Mat(); |
| | return latest_frame; |
| | } |
| | |
| | |
| | latest_frame = cv::imread(image_files[frame_num], cv::IMREAD_COLOR); |
| |
|
| | if (latest_frame.empty()) |
| | { |
| | ERROR_STREAM("Could not open the image: " + image_files[frame_num]); |
| | exit(1); |
| | } |
| |
|
| | image_height = latest_frame.size().height; |
| | image_width = latest_frame.size().width; |
| |
|
| | |
| | float _fx = -1; |
| | float _fy = -1; |
| | |
| | if (image_focal_length_set) |
| | { |
| | _fx = fx; |
| | _fy = fy; |
| | } |
| | |
| | float _cx = -1; |
| | float _cy = -1; |
| |
|
| | if (image_optical_center_set) |
| | { |
| | _cx = cx; |
| | _cy = cy; |
| | } |
| |
|
| | SetCameraIntrinsics(_fx, _fy, _cx, _cy); |
| |
|
| | |
| | ConvertToGrayscale_8bit(latest_frame, latest_gray_frame); |
| |
|
| | this->name = image_files[frame_num]; |
| |
|
| | frame_num++; |
| |
|
| | return latest_frame; |
| | } |
| |
|
| | std::vector<cv::Rect_<float> > ImageCapture::GetBoundingBoxes() |
| | { |
| | if (!bounding_boxes.empty()) |
| | { |
| | return bounding_boxes[frame_num - 1]; |
| | } |
| | else |
| | { |
| | return std::vector<cv::Rect_<float> >(); |
| | } |
| | } |
| |
|
| | double ImageCapture::GetProgress() |
| | { |
| | return (double)frame_num / (double)image_files.size(); |
| | } |
| |
|
| | cv::Mat_<uchar> ImageCapture::GetGrayFrame() |
| | { |
| | return latest_gray_frame; |
| | } |
| |
|