The article introduces how to display an image by OpenCV in CPlusPlus and Python language.
The user can exit the whole program when he presses the ESC key.
CPP
CMakeLists.txt
macro(use_cxx11)
if (CMAKE_VERSION VERSION_LESS "3.1")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
endif ()
else ()
set (CMAKE_CXX_STANDARD 11)
endif ()
endmacro(use_cxx11)
cmake_minimum_required(VERSION 2.8)
project( CVLearn )
use_cxx11()
find_package( OpenCV REQUIRED )
add_executable( ${PROJECT_NAME} "main.cpp" )
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )
main.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
Mat image;
image = imread( "/Users/weiyang/Desktop/Untitled.png", IMREAD_COLOR );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
// break when press ESC
while( 27 != waitKey(0) ) { }
return 0;
}
Python
import cv2
path = r'/Users/weiyang/Desktop/Untitled.png'
image = cv2.imread(path)
window_name = 'image'
cv2.imshow(window_name, image)
# waits for user to press ESC
while 27 != cv2.waitKey(0):
pass
# closing all open windows
cv2.destroyAllWindows()
Output: