Load Image from Specified File using OpenCV

While working with image processing, one of the first steps is load an image from a file.

OpenCV provides imread function that enables to load an image from a file. This function supports various image formats such as PNG, JPEG, BMP, WebP, and other. Image format is identified by the content of an image, not by the filename extension. Function loads an image where the order of the color channels is Blue, Green, Red (BGR) instead of RGB.

import cv2

img = cv2.imread('test.jpg')

cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#include <opencv2/opencv.hpp>

using namespace cv;

int main()
{
    Mat img = imread("test.jpg");

    imshow("Image", img);
    waitKey(0);
    destroyAllWindows();

    return 0;
}
package app;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;

public class Main
{
    static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main(String[] args)
    {
        Mat img = Imgcodecs.imread("test.jpg");

        HighGui.imshow("Image", img);
        HighGui.waitKey(0);
        HighGui.destroyAllWindows();

        System.exit(0);
    }
}

Leave a Comment

Your email address will not be published.