I am trying to write a function with multiple return values. I need to handle the scenario when if there is no image in the path. How can i do this?
I have tried to use map. But it is more for Key, Value pairs is what I think(new to C++).
Below is my code:
tuple<Mat, Mat> imageProcessing(boost::filesystem::path pickPath){
Mat img1, img2;
// Check if file exists, if not return NULL
if (!boost::filesystem::is_regular_file(pickPath)) {
return make_tuple(NULL, NULL);
}
imageFile = imread(pickPath.string());
// Preprocess code (return 2 mat files)
return make_tuple(img_1, img_2);
}
int main(){
path = "img.jpeg"
tie(img1, img2) = imageProcessing(path);
}
Use std::vector if you want a contiguous collection of objects or use std::set if your function guarantees to return only unique results.
Your method should also handle errors gracefully. In general there are two approaches in C++ for that:
std::vector<Mat> ProcessImages(const boost:filesystem::path filePath)
{
if (!boost::filesystem::is_regular_file(pickPath)) {
throw std::invalid_argument("file does not exist"!); //probably there's a better exception you could throw or you can define your own.
}
...
The caller would look like this:
try{
auto images = ProcessImage(myFilePath)
}
catch(const std::invalid_argument& e ) {
// write something to console, log the exception, terminate your process... choose your poison.
}
// if successful the function will return 0.
enum ErrorCode
{
Successful = 0,
InvalidArgs = 1,
...
}
ErrorCode ProcessImages(const boost:filesystem::path filePath, std::vector<Mat>& outImages)
{
if (!boost::filesystem::is_regular_file(pickPath)) {
{
return InvalidArgs;
}
imageFile = imread(pickPath.string());
outImages.insert(img1);
outImages.insert(img2);
return Successful;
}
int main(){
path = "img.jpeg"
std::vector<Mat> images;
auto result = ProcessImages(path, images);
if (result != Successfull)
{
//error
}
}