From 9499eec1a84e143530ddd0210cd4ad5aee722432 Mon Sep 17 00:00:00 2001 From: Vilson Vieira Date: Mon, 5 Oct 2015 23:23:47 -0300 Subject: [PATCH] Check if imread fails If it fails, return null saliency and stop --- saliency.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/saliency.cpp b/saliency.cpp index ab27f4c..8474edc 100644 --- a/saliency.cpp +++ b/saliency.cpp @@ -53,7 +53,16 @@ int main(int argc, char *argv[]) { strcpy(original_image_path, argv[1]); Mat original_image; - original_image = imread(original_image_path); + original_image = imread(original_image_path, CV_LOAD_IMAGE_COLOR); + + // Return null saliency if fails opening the image file + if (original_image.empty()) { + original_image = imread(original_image_path, CV_LOAD_IMAGE_COLOR); + if (original_image.empty()) { + cout << "{\"saliency\": null}" << endl; + return 0; + } + } GMRsaliency GMRsal; Mat saliency_map; @@ -138,7 +147,7 @@ int main(int argc, char *argv[]) { vector center( contours.size() ); vector radius( contours.size() ); - for (size_t i = 0, max = contours.size(); i < max; ++i) { + for (size_t i = 0, max = contours.size(); i < max; ++i) { // Approximate polygon of a contour approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); // Calculate the bounding box for the contour @@ -152,7 +161,7 @@ int main(int argc, char *argv[]) { // Find the biggest area of all contours int big_id = 0; double big_area = 0; - for (size_t i = 0, max = contours.size(); i < max; ++i) { + for (size_t i = 0, max = contours.size(); i < max; ++i) { // Contour area double area = contourArea(contours[i]); if (area > big_area) { @@ -171,7 +180,7 @@ int main(int argc, char *argv[]) { xmax = 0; ymax = 0; xmin = INFINITY; - ymin = INFINITY; + ymin = INFINITY; for (size_t j=0, max = boundRect.size(); j(), 0, Point() ); @@ -210,7 +219,7 @@ int main(int argc, char *argv[]) { } // Draw the big rectangle rectangle( drawing, bigRect.tl(), bigRect.br(), Scalar(255,200,255), 2, 8, 0 ); - + sprintf(file_path, "%s_contours.png", original_image_path); imwrite(file_path, drawing);