#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
//이미지 파일을 저장할 Mat 클래스의 인스턴스를 생성한다.
Mat img1, img2;
//imread() 함수로 이미지 파일을 불러온다.
img1 = imread("img/background.png");
//Mat클래스의 empty() 메소드를 이용하여 이미지 파일이 제대로 불러와졌는지 확인한다.
if (img1.empty() && img2.empty()) {
//기본적으로 imread() 함수에서 지정한 경로에 파일이 없으면 실패한다.
cout << "이미지 파일을 읽을 수 없습니다." << endl;
return -1;
}
imshow("1", img1);
//키입력을 받을 때까지 무한히 대기시킨다.
waitKey(0);
}
imread 함수에 대해서
https://docs.opencv.org/4.5.5/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56
역할 : 이미지를 읽고 Mat 인스턴스로 반환해주는 함수
헤더파일 opencv2/imgcodecs.hpp (opencv.hpp에 포함)
원형 ==> Mat cv::imread(const String & filename, int flags = IMREAD_COLOR)
리턴값 Mat 인스턴스
인자 1 : 이미지 파일의 경로
인자 2 : 이미지 파일을 읽는 방법 (기본값 있음)
두번째 인자가 기본값이 설정되어 있으므로 2가지 형태로 호출 가능.
1) imread( "파일경로"); 2) imread( "파일경로", flags);
flags값은 enum cv::ImreadModes의 값이다.
IMREAD_UNCHANGED = -1 // If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
이미지를 알파채널을 포함하여 그대로 읽음. 방향정보를 무시한다.
IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
이미지를 단일 채널을 사용하는 회색조 이미지로 변환해서 읽는다.
IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
이미지를 BGR채널을 사용하는 컬러 이미지로 변환해서 읽는다.
IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
이미지에 따라 8/16/32비트 이미지로 읽는다.
IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
이미지를 가능한 모든 색상 형식으로 읽는다.
IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the image.
GDAL 드라이버로 이미지를 읽는다.
IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
이미지를 단일 채널을 사용하는 회색조 이미지로 변환하고 사이즈를 1/2로 줄인다. (넓이 1/2, 높이 1/2 이므로 크기 자체는 1/4이 된다.)
IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
이미지를 BGR채널을 사용하는 컬러 이미지로 변환하고 사이즈를 1/2로 줄인다. (넓이 1/2, 높이 1/2 이므로 크기 자체는 1/4이 된다.)
IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
이미지를 읽을 때 방향정보를 무시한다.
자주쓰이는 것은
IMREAD_UNCHANGED = -1 // 이미지를 알파채널을 포함해서 그대로 출력.
IMREAD_GRAYSCALE = 0 // 이미지를 단일채널의 회색조 이미지로 변환해서 출력.
IMREAD_COLOR = 1 // 이미지를 BGR 3채널을 사용하는 컬러 이미지로 변환해서 출력.
IMREAD_REDUECE_GRAYSCALE_2 = 16, IMREAD_REDUCED_COLOR_2 = 17
IMREAD_REDUECE_GRAYSCALE_4 = 32, IMREAD_REDUCED_COLOR_2 = 33
IMREAD_REDUECE_GRAYSCALE_8 = 64, IMREAD_REDUCED_COLOR_2 = 65
의 6가지는 각각 0번과 1번을 1/2, 1/4, 1/8 씩 줄여서 출력한다.
가로, 세로 길이를 1/n씩 줄이기 때문에 전체 크기는 1/n^2 으로 줄어든다.
이미지 읽기에 실패하는 경우 (Mat::data == NULL을 반환)
1. 파일 누락 2. 부적절한 권한 3. 지원되지 않거나 잘못된 형식의 파일인 경우이다.
1. 파일 누락의 경우 지정한 경로에 파일이 없을 경우에 해당된다.
2. 시스템(운영체제)의 사용자에게 이미지 파일을 읽을 권한이 없는 경우에 해당된다.
3. opencv에서 지원하지 않는 형식이거나 파일 자체에 문제가 있는 경우에 해당된다.
읽기에 실패하더라도 imread() 함수는 에러가 나지 않으므로 empty() 메소드로 확인해줄 필요가 있다.
opencv에서 지원하는 이미지 형식
항상 지원 :
windows비트맵(bmp, dib),
휴대용 이미지(pbm, pgm, ppm, pxm, pnm),
SUN래스터 (sr, ras),
Radiance HDR (hdr, pic)
조건이 충족되면 지원 :
JPEG(jpeg, jpg, jpe),
JPEG2000(jp2),
휴대용 네트워크 그래픽 (png),
webP(webp),
TIFF (tiff, tif),
OpenEXR (exr),
GDAL에서 지원하는 래스터 및 벡터 지리 공간 데이터