头文件
#include <opencv.hpp>
缺省:
#define HAVE_OPENCV_CALIB3D
#define HAVE_OPENCV_CORE
#define HAVE_OPENCV_DNN
#define HAVE_OPENCV_FEATURES2D
#define HAVE_OPENCV_FLANN
#define HAVE_OPENCV_HIGHGUI
#define HAVE_OPENCV_IMGCODECS
#define HAVE_OPENCV_IMGPROC
#define HAVE_OPENCV_ML
#define HAVE_OPENCV_OBJDETECT
#define HAVE_OPENCV_PHOTO
#define HAVE_OPENCV_SHAPE
#define HAVE_OPENCV_STITCHING
#define HAVE_OPENCV_SUPERRES
#define HAVE_OPENCV_VIDEO
#define HAVE_OPENCV_VIDEOIO
#define HAVE_OPENCV_VIDEOSTAB
命名空间:
using namespace cv;
图像载入到Mat类:
Mat cv::imread ( const String & filename, //载入的图像文件名,支持一般的图像格式:bmp;dib;jpeg;jpg;jp2;png;tif;tiff等
int flags = IMREAD_COLOR //IMREAD_UNCHANGED : 如有alpha通道,不会丢失
//IMREAD_GRAYSCALE :载入图像转换为单通道灰度图
//IMREAD_COLOR :缺省,载入图像转换为三通道BGR图
//IMREAD_ANYDEPTH:当输入具有相应的深度时返回16位/ 32位图像,否则将其转换为8位
//IMREAD_ANYCOLOR:以任何可能的颜色格式读取图像
//IMREAD_ANYDEPTH|IMREAD_ANYCOLOR:无损载入
);
读多幅图像
bool cv::imreadmulti (const String &filename, std::vector< Mat > &mats, int flags=IMREAD_ANYCOLOR)显示图像
void cv::imshow ( const String & winname, //窗口名
InputArray mat //显示的图像
);
void cv::namedWindow (const String &winname, int flags=WINDOW_AUTOSIZE);
flags:
WINDOW_NORMAL
WINDOW_AUTOSIZE
WINDOW_OPENGL
WINDOW_FULLSCREEN
WINDOW_FREERATIO
WINDOW_KEEPRATIO
WINDOW_GUI_EXPANDED
WINDOW_GUI_NORMAL
窗口上可以添加,Button,TrackBar,Mouse event
bool cv::imwrite (const String &filename, InputArray img, const std::vector< int > ¶ms=std::vector< int >());
扩展名存成某种格式图像,格式参数:
IMWRITE_JPEG_QUALITY 0-100
IMWRITE_PNG_COMPRESSION 0-9
#include <opencv.hpp>
using namespace cv;
int main()
{
String fileName = "e:\\qetang\\Studyopencv\\images\\ox.jpg";
Mat imgSrc = imread(fileName);
imshow("imread", imgSrc);
try {
imwrite("e:\\qetang\\Studyopencv\\images\\ox.png", imSrc);
}
catch (cv::Exception& ex) {
fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
return 1;
}
fprintf(stdout, "Saved PNG file with alpha data.\n");
waitKey(0);
return 0;
}MFC下显示图像
cv::String fileName = "e:\\qetang\\studyopencv\\images\\ox.jpg";
Mat imgSrc = imread(fileName);
namedWindow("view", WINDOW_AUTOSIZE);
HWND hWnd = (HWND)cvGetWindowHandle("view");
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_STATIC_PIC)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);//将控件句柄设置为窗口的父句柄
CRect rect;
GetDlgItem(IDC_STATIC_PIC)->GetClientRect(&rect);
if (!imgSrc.empty())
{
Mat imgDst;
cv::resize(imgSrc, imgDst, cv::Size(rect.Width(), rect.Height()));//将图片调整为控件大小显示
imshow("view", imgDst);
}
本文介绍如何使用OpenCV进行图像处理,包括图像的加载、显示和保存。详细解释了不同参数的作用,例如保持图像原始深度和颜色格式等,并提供了示例代码。
2284

被折叠的 条评论
为什么被折叠?



