opencv_example/example.cpp

82 lines
1.6 KiB
C++

#include <iostream>
#include <opencv2/opencv.hpp>
#include <sys/stat.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;
std::vector<std::string> supportedExt = {
"jpg", "JPG",
"jpeg", "JPEG",
"png", "PNG",
"tif", "TIF",
"tiff", "tiff"
};
inline bool in_array(const std::string &value, const std::vector<std::string> &array)
{
return std::find(array.begin(), array.end(), value) != array.end();
}
inline bool file_exists (const std::string& name)
{
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
inline bool is_image (const std::string& name)
{
string ext = name.substr(name.find_last_of(".") + 1);
return in_array(ext, supportedExt);
}
int main(int argc, char** argv)
{
Mat image;
Mat inverted;
string input;
if (argc == 2)
{
input = argv[1];
}
else if (argc < 2)
{
cerr << "Please provide one image." << endl;
return -1;
}
else if (argc > 2)
{
cerr << "Please provide only one image." << endl;
return -2;
}
if (!file_exists(input))
{
cerr << "File " << input << " does not exist." << endl;
return -3;
}
if (!is_image(input))
{
cerr << "File " << input << " is not a valid image." << endl;
return -4;
}
cout << "Using image " << input << "." << endl;
image = imread(input);
bitwise_not(image, inverted);
imshow("Image", inverted);
waitKey(0);
destroyAllWindows();
return 0;
}