Read Multiple Images From a Folder in Opencv C++
Reading, displaying, and writing images are basic to paradigm processing and computer vision. Even when cropping, resizing, rotating, or applying different filters to process images, you lot'll need to beginning read in the images. So it'due south important that you master these basic operations.
OpenCV, the largest reckoner vision library in the earth has these three built-in functions, let's discover out what exactly each one does:
-
imread()
helps united states read an image -
imshow()
displays an image in a window -
imwrite()
writes an image into the file directory
- Reading an Epitome
- Displaying an Paradigm
- Writing an Image
- Summary
We volition use the following image to demonstrate all the functions here.
First, become through this lawmaking example. It reads and displays the in a higher place image. Run across, how information technology contains all the three functions, we merely mentioned. As y'all go on further, we will discuss every single function used in this implementation.
Python
# import the cv2 library import cv2 # The function cv2.imread() is used to read an epitome. img_grayscale = cv2.imread('exam.jpg',0) # The function cv2.imshow() is used to display an prototype in a window. cv2.imshow('graycsale image',img_grayscale) # waitKey() waits for a key press to shut the window and 0 specifies indefinite loop cv2.waitKey(0) # cv2.destroyAllWindows() simply destroys all the windows we created. cv2.destroyAllWindows() # The office cv2.imwrite() is used to write an image. cv2.imwrite('grayscale.jpg',img_grayscale)
Download Code To hands follow along this tutorial, please download code past clicking on the push below. It's Gratuitous!
C++
//Include Libraries #include<opencv2/opencv.hpp> #include<iostream> // Namespace nullifies the use of cv::role(); using namespace std; using namespace cv; // Read an image Mat img_grayscale = imread("test.jpg", 0); // Display the image. imshow("grayscale image", img_grayscale); // Wait for a keystroke. waitKey(0); // Destroys all the windows created destroyAllWindows(); // Write the image in the same directory imwrite("grayscale.jpg", img_grayscale);
Permit's brainstorm by importing the OpenCV library in Python and C++ (as shown beneath).
Python:
# import the cv2 library import cv2
In C++, utilize #include
(as shown below) to accomplish the aforementioned. Also specifying the namespaces for it lets you lot refer to part names directly. No need to prepend them with the namespace (e.g. instead of cv::imread()
, you can simply directly employ read()
).
C++:
//Include Libraries #include<opencv2/opencv.hpp> #include<iostream> // Namespace nullifies the employ of cv::part(); using namespace std; using namespace cv;
Reading an Epitome
For reading an prototype, utilise the imread()
function in OpenCV. Here's the syntax:
imread(filename, flags)
It takes two arguments:
- The first statement is the image proper noun, which requires a fully qualified pathname to the file.
- The 2nd argument is an optional flag that lets you specify how the image should be represented. OpenCV offers several options for this flag, only those that are nearly mutual include:
-
cv2.IMREAD_UNCHANGED
or-1
-
cv2.IMREAD_GRAYSCALE
or0
-
cv2.IMREAD_COLOR
ori
NEW Grade Live ON KICKSTARTER
Deep Learning With TensorFlow & Keras
The default value for flags is 1, which will read in the paradigm as a Colored image. When you want to read in an paradigm in a particular format, just specify the advisable flag. To check out the different flag options, click hither
Information technology's as well important to note at this signal that OpenCV reads colour images in BGR format, whereas most other computer vision libraries use the RGB aqueduct format order. And then, when using OpenCV with other toolkits, don't forget to bandy the bluish and red color channels, as you switch from one library to another.
Equally shown in the lawmaking sections below, we volition commencement read in the test epitome, using all iii flag values described higher up.
Python
# Read an image img_color = cv2.imread('test.jpg',cv2.IMREAD_COLOR) img_grayscale = cv2.imread('test.jpg',cv2.IMREAD_GRAYSCALE) img_unchanged = cv2.imread('test.jpg',cv2.IMREAD_UNCHANGED)
C++
// Read an prototype Mat img_color = imread("exam.jpg", IMREAD_COLOR); Mat img_grayscale = imread("test.jpg", IMREAD_GRAYSCALE); Mat img_unchanged = imread("examination.jpg", IMREAD_UNCHANGED);
Or
Python
img_color = cv2.imread('test.jpg',1) img_grayscale = cv2.imread('examination.jpg',0) img_unchanged = cv2.imread('test.jpg',-1)
C++
Mat img_color = imread("test.jpg", 1); Mat img_grayscale = imread("test.jpg", 0); Mat img_unchanged = imread("test.jpg", -1);
Displaying an Image
In OpenCV, you display an prototype using the imshow()
office. Here's the syntax:
imshow(window_name, paradigm)
This function likewise takes two arguments:
- The commencement argument is the window name that will be displayed on the window.
- The second statement is the prototype that you lot want to display.
To display multiple images at once, specify a new window proper noun for every paradigm you want to display.
The imshow()
function is designed to be used along with the waitKey()
and destroyAllWindows()
/ destroyWindow()
functions.
The waitKey()
part is a keyboard-binding function.
- It takes a single argument, which is the time (in milliseconds), for which the window will exist displayed.
- If the user presses any key within this time period, the plan continues.
- If 0 is passed, the programme waits indefinitely for a keystroke.
- You can likewise set the function to detect specific keystrokes similar the Q key or the ESC primal on the keyboard, thereby telling more explicitly which key shall trigger which beliefs.
The function destroyAllWindows()
destroys all the windows we created. If a specific window needs to be destroyed, give that exact window name equally the statement. Using destroyAllWindows()
also clears the window or paradigm from the main memory of the arrangement.The code examples beneath show how the imshow()
function is used to brandish the images you read in.
Python
#Displays paradigm inside a window cv2.imshow('color paradigm',img_color) cv2.imshow('grayscale image',img_grayscale) cv2.imshow('unchanged paradigm',img_unchanged) # Waits for a keystroke cv2.waitKey(0) # Destroys all the windows created cv2.destroyAllwindows()
C++
// Create a window. namedWindow( "color image", WINDOW_AUTOSIZE ); namedWindow( "grayscale image", WINDOW_AUTOSIZE ); namedWindow( "unchanged prototype", WINDOW_AUTOSIZE ); // Show the image inside it. imshow( "colour prototype", img_color ); imshow( "grayscale prototype", img_grayscale ); imshow( "unchanged prototype", img_unchanged ); // Wait for a keystroke. waitKey(0); // Destroys all the windows created destroyAllWindows();
Below is the GIF demonstrating the process of executing the code, visualizing the outputs, and closing the output window:
In the three output screens shown below, you tin can see:
- The first image is displayed in color
- The next as grayscale
- The third is once again in color, as this was the original format of the prototype (which was read using
cv2.IMREAD_UNCHANGED
)
The beneath GIF shows execution of the code to read and brandish the epitome but without waitKey()
. The window gets destroyed within milliseconds, and no output is shown on the screen.
Writing an Image
Finally, let's discuss how to write/save an epitome into the file directory, using the imwrite()
office. Check out its syntax:
imwrite(filename, image)
.
- The starting time argument is the filename, which must include the filename extension (for example .png, .jpg etc). OpenCV uses this filename extension to specify the format of the file.
- The second statement is the image y'all want to salvage. The function returns
Truthful
if the image is saved successfully.
Have a wait at the lawmaking below. Run into how unproblematic it is to write images to deejay. Just specify the filename with its proper extension (with any desired path prepended). Include the variable proper noun that contains the paradigm data, and yous're done.
Python
cv2.imwrite('grayscale.jpg',img_grayscale)
C++
imwrite("grayscale.jpg", img_grayscale);
Summary
Here, you learned to use the:
-
imread()
,imshow()
andimwrite()
functions to read, display, and write images -
waitKey()
anddestroyAllWindows()
functions, along with the display function to- close the image window on key press
- and clear any open paradigm window from the memory.
You take to experiment a lot when it comes to the waitkey()
function, for it can be quite confusing. The more familiar you go with information technology, the better you can employ it. Exercise download the complete code to become some hands-on experience. Practise well, equally these are the basic building blocks that can really help you acquire and primary the OpenCV library → OpenCV colab notebook.
Subscribe & Download Lawmaking
If you liked this article and would like to download code (C++ and Python) and example images used in this post, please click here. Alternately, sign up to receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Estimator Vision and Machine Learning algorithms and news.
Source: https://learnopencv.com/read-display-and-write-an-image-using-opencv/
Post a Comment for "Read Multiple Images From a Folder in Opencv C++"