opencv函数库包含了众多图像操作的基本函数,以下列出将常用的函数,方便以后查询:
1.读取图片:imread
原函数:CV_EXPORTS_W Mat imread( const string& filename, int flags=1 );
输入参数:filename:文件名,包含路径;flags:图片类型()
返回值:图像标识符(可通过图像标识符对图像进行操作)
2.显示图片:imshow
原函数:void imshow(const string& winname, InputArray mat);
输入参数:winname:窗口名称;mat:要显示的图片标识符
3.缩放图片:resize
原函数:void resize( InputArray src, OutputArray dst,
Size dsize, double fx=0, double fy=0,
int interpolation=INTER_LINEAR );
输入:src:要缩放的图片;dst:缩放后输出的图片:dsize:行像素缩放比例,列像素缩放比例;
4.翻转图像:
原函数:void flip(InputArray src, OutputArray dst, int flipCode);
输入参数:src:要翻转的源图像;dst:翻转后输出的图像;filpCode:翻转方向:0(垂直),1(水平),负数(水平+垂直)
5.画圆:
原函数:void circle(CV_IN_OUT Mat& img, Point center, int radius,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
输入参数:img:要操作的图像;center:圆心;color:颜色;thickness:线条粗细;
6.画直线:
原函数:void line(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color,
int thickness=1, int lineType=8, int shift=0);
输入参数:img:要操作的图像;pt1:起点;pt2:终点;color:颜色;thinckness:线条粗细;
7.画矩形:rectangle
原函数:void rectangle(CV_IN_OUT Mat& img, Rect rec,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
输入参数:img:要操作的图像;rec:矩形块的尺寸;color:颜色;thinckness:线条粗细;
8.灰度图像:cvtColor
原函数:void cvtColor( InputArray src, OutputArray dst, int code, int dstCn=0 );
输入参数:src:要处理的原图片;dst:处理后的图片;code:要执行的操作选项
9.二值化处理:
原函数:double threshold( InputArray src, OutputArray dst,
double thresh, double maxval, int type );
输入参数:输入参数:src:要处理的原图片;dst:处理后的图片;thresh:阈值
示例代码:
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace std;
int main()
{
/*open image*/
cv::Mat image = cv::imread("crazy.jpg",1);//CV_THRESH_BINARY
if (image.empty())
{
cout << "read image is failure" << endl;
exit(0);
}
cv::resize(image, image, cv::Size(image.cols / 2, image.rows / 2));
/*display image*/
cv::namedWindow("Origin Image");
cv::imshow("Origin Image", image);
/*print image size*/
cout << image.rows << "x" << image.cols << endl;
cv::Mat result;
cv::flip(image, result, 1);
cv::imshow("flip Image", result);
cv::circle(result,cv::Point(140, 280),50, cv::Scalar(0, 255, 0),3);
cv::line(result, cv::Point(0, 5), cv::Point(result.cols,5),cv::Scalar(255,0,0),2);
cv::rectangle(result, cv::Rect(10, 10, 100, 100),cv::Scalar(0, 0, 255),2);
cv::putText(result,"Zootopia",cv::Point(10, 140),cv::FONT_HERSHEY_PLAIN,2.0, cv::Scalar(255, 0, 0),2);
cv::imshow("DrawImage", result);
cv::Mat grayImage,binary;
cv::cvtColor(image, grayImage, CV_BGR2GRAY);
cv::threshold(grayImage, binary, 100, 255, CV_THRESH_BINARY);
cv::imshow("grayImage", grayImage);
cv::imshow("binaryImage", binary);
cv::waitKey(0);
}