In OpenCV you can easily read in images with different file formats (JPG, PNG, TIFF etc.) using imread. The basic usage is shown below
C++
Mat imread(const string& filename, int flags=IMREAD_COLOR )
Python
image = cv2.imread(filename, flags=cv2.IMREAD_COLOR)
The flags option is used to control how the image is read. Let’s look at some common examples. In all the examples below, make sure you use the right namespace for C++ and import OpenCV for Python.
C++
using namespace cv;
Python
import cv2
Read as 8-bit / channel Color Image ( without Alpha Channel)
A vast majority of images are 8-bit per channel ( or 24-bit ) images. They can be read using default flags.
C++
Mat image = imread("image.jpg");
Python
image = cv2.imread("image.jpg")
Read as 8-bit Grayscale Image
C++
Mat image = imread("image.jpg", IMREAD_GRAYSCALE);
Python
image = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
Read 16-bit / channel Color Image
Most digital SLR cameras are capable of recording images at a higher bit depth than 8-bits / channel. The raw images from these cameras can be converted to 16-bit / channel PNG or TIFF images. These 16-bit / channel images can be read using
C++
Mat image = imread("image.png", IMREAD_ANYCOLOR | IMREAD_ANYDEPTH);
Python
im = cv2.imread("image.png", cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
You may also use the flag IMREAD_UNCHANGED instead. See an example below.
Read a Transparent PNG or TIFF in OpenCV
A transparent image has four channels — 3 for color, and one for transparency. These images can be read in OpenCV using the IMREAD_UNCHANGED flag.
C++
Mat image = imread("image.png", IMREAD_UNCHANGED);
Python
im = cv2.imread("image.png", cv2.IMREAD_UNCHANGED)
NOTE : CV_LOAD_IMAGE_COLOR, CV_LOAD_IMAGE_GRAYSCALE, CV_LOAD_IMAGE_ANYCOLOR, CV_LOAD_IMAGE_ANYDEPTH, and CV_LOAD_IMAGE_UNCHANGED will be removed in future versions of OpenCV.
hello,
can you make something simple about stitching more than two images? (not the simple Stitch class)
I am sorry to send this message through here. I didn’t find your contact.
findMatches(image1, image2); //find features, matches and returns points1 and points2
Mat Homog = findHomography(points1, points2, CV_RANSAC);
Mat result;
warpPerspective(image1, result, Homog, Size(image1.cols+image2.cols, image1.rows+image2.rows));
Mat half(result, Rect(0, 0, image2.cols, image2.rows));
image2.copyTo(half);
something like this.
thank you!
Great website, I already subscribed =)
Thanks a bunch for the suggestion. I will put this on my list for sure. My next post will be Part 2 of OpenCV as a web API, but I can write this one after that.
thank you, OpenCV as a web API is also very interesting! Do you know any easy way to make an openCV web API for C++ too?
Hi Satya,
Can you please suggest which parameter I should use if I want to read Hyperspecral image with 120 bands and is in .TIFF format. I tried all the parameters, but when I check it using img.channels() it is showing 4 bands maximum.
Thank You.
Hi Pankaj,
OpenCV is used to read 3 Bands RGB image (meaning 3 channels). Sometimes there may be an alpha channel so RGBA (4 channels) image.
You can not read 120 bands using opencv function imread. You will have to write your own code to read 120 bands. You can process it as stack of OpenCV Mat containing 120 Matrices.
I am not sure how can you read .TIFF file using an opensource library. There is multibandread function in Matlab which does that.
Regards,
Abhinav
Hi, There is a recent(few months back, so not so recent) addition of handling of EXIF Orientation metadata. Check this out: https://github.com/Itseez/opencv/pull/5538
Thanks. Very nice. I will ready up on it and add to this post.
Were you able to make it work? I’m not. :/
Are you using latest addition? I didnt use it as we are already using imagemagick to auto-orient images before processing(calling imagemagick bash command from java). If interested, install imagemagick and run this command “convert -auto-orient originalImagePath orientedImagePath”
However, before moving to imagemagick, I suggest you make sure that opencv source that you are building has these changes. You can do a git pull from github.com/itseez/opencv.git and then build it. This will have all the changes. Once built, install exiftool and find images which have orientation 2 to 8.
Test on these images. Open these images in sublime(yes sublime can open images as well), they will open with orientation not corrected, similar to how opencv used to open it before. But if you open them in any image viewer, these image will have different orientation. Not use opencv and this time image will open with correct orientation if you are using latest repo.
I used exiftags from PIL to find the orientation in python, but I want to make this work in C++. I was using opencv v3.0 only. I’ll clone the latest and try this out! 🙂
Check this out: http://opencv-cpp.blogspot.com
opencv C++ Tutorials available with full source code !
We can load any image file in Opencv C++, python or android. Its
steps are very simple and easy.We can show any format of image in opencv
like JPG and PNG. We can use three functions from opencv to show , load
and save frames imread(), imshow() and imwrite().
Mat cv::imread ( const String & filename, int flags = IMREAD_COLOR ) // Read image
void cv::imshow ( Window Name , Mat Object to show ) // Show image
Void cv::imwrite ( filename, InputArray img) // Write frame in hard drive
Download complete code of imread function in C++ from opencvcraze.com