(C++) OpenCvExample1
February 24, 2017 · View on GitHub
(C++) OpenCvExample1
OpenCvExample1 is a OpenCV example how to display an image and how to convert one to greyscale.
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 12.04 (precise)
Qt Creator 2.3.0
- G++ 4.6.3
Libraries used:
Qt project file: CppOpenCvExample1.pro
#------------------------------------------------- # # Project created by QtCreator 2012-07-11T13:49:58 # #------------------------------------------------- QT += core QT -= gui unix { CONFIG += link_pkgconfig PKGCONFIG += opencv } #LIBS += -lopencv TARGET = CppOpenCvExample1 CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp RESOURCES += \ CppOpenCvExample1.qrc
main.cpp
#include <QFile> #include <QResource> #include <opencv2/opencv.hpp> int main() { const std::string filename = "image.jpg"; //Create the source image if it does not exist { if (!QFile::exists(filename.c_str())) { QFile f((std::string(":/images/") + filename).c_str()); f.copy(filename.c_str()); } assert(QFile::exists(filename.c_str())); } //Load the image const cv::Mat image_original = cv::imread(filename); assert(image_original.data); //Create the resulting image cv::Mat image_result; cv::cvtColor(image_original,image_result,CV_RGB2GRAY ); cv::imwrite("R_grey.png", image_result ); //Display the original cv::imshow("Original",image_original ); //Display the result cv::imshow("Result", image_result ); //Wait for a key press cv::waitKey(0); }