opencv_example/example.cpp

82 lines
1.6 KiB
C++
Raw Normal View History

2023-02-03 20:56:29 +00:00
#include <iostream>
2023-02-04 15:43:38 +00:00
#include <opencv2/opencv.hpp>
2023-02-03 21:15:44 +00:00
#include <sys/stat.h>
#include <string>
#include <vector>
#include <algorithm>
2023-02-04 02:50:01 +00:00
2023-02-03 20:56:29 +00:00
using namespace std;
using namespace cv;
2023-02-03 21:15:44 +00:00
std::vector<std::string> supportedExt = {
"jpg", "JPG",
"jpeg", "JPEG",
"png", "PNG",
"tif", "TIF",
"tiff", "tiff"
2023-02-04 02:50:01 +00:00
};
inline bool in_array(const std::string &value, const std::vector<std::string> &array)
{
return std::find(array.begin(), array.end(), value) != array.end();
2023-02-03 21:15:44 +00:00
}
2023-02-04 02:50:01 +00:00
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);
2023-02-04 02:50:01 +00:00
}
2023-02-03 20:56:29 +00:00
int main(int argc, char** argv)
{
2023-02-04 15:43:38 +00:00
Mat image;
Mat inverted;
2023-02-03 20:56:29 +00:00
string input;
2023-02-05 23:43:38 +00:00
if (argc == 2)
{
2023-02-03 20:56:29 +00:00
input = argv[1];
2023-02-05 23:43:38 +00:00
}
else if (argc < 2)
{
2023-02-03 21:15:44 +00:00
cerr << "Please provide one image." << endl;
2023-02-03 20:56:29 +00:00
return -1;
2023-02-05 23:43:38 +00:00
}
else if (argc > 2)
{
2023-02-03 21:15:44 +00:00
cerr << "Please provide only one image." << endl;
2023-02-03 20:56:29 +00:00
return -2;
}
if (!file_exists(input))
{
2023-02-03 21:15:44 +00:00
cerr << "File " << input << " does not exist." << endl;
return -3;
}
if (!is_image(input))
{
cerr << "File " << input << " is not a valid image." << endl;
return -4;
}
2023-02-05 23:43:38 +00:00
cout << "Using image " << input << "." << endl;
2023-02-04 15:43:38 +00:00
image = imread(input);
bitwise_not(image, inverted);
imshow("Image", inverted);
waitKey(0);
destroyAllWindows();
2023-02-05 23:43:38 +00:00
2023-02-03 20:56:29 +00:00
return 0;
}