opencv 常用的數據結構和函數
顏色空間轉換函數 cvtColor 函數
cvtColor 函數是opencv 中的顏色空間轉換函數。
可以實現rgb向hsv hsi等顏色空間的轉換,也可以轉換成灰度圖像
原型:void cvtColor (InputArray src,OutArray dst,int code,int dstCn =0);
src :輸入圖像
dst:輸出圖像
code:顏色轉換空間標示
dstCn:目標圖像的通道數 若該參數為0 則為目標圖像的通道數
cvtColor 函數標識符
RGB-> BGR CV_BGR2BGRA ,CV_RGB2BGRA,CV_BGRA2RGBA,CV_BGR2BGRA,CV_BGRA2BGR
RGB->GRAY CV_RGB2GRAY,CV_GRAY2RGB,CV_RGBA2GRAY,CV_GRAY2RGBA
RGB->HSV CV_RGB2HSV,CV_BGR2HSV,CV_HSV2BGR,CV_HSV2BGR,CV_HSV2RGB
RGB->HLS CV_RGB2HLS,CV_BGR2HLS,CV_HLS2RGB,CV_HLS2BGR
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main()
{
Mat srcImage = imread("jpg/1.jpeg");
Mat dstImage;
cvtColor(srcImage,dstImage,CV_RGB2BGR);
imshow("src",srcImage);
imshow("dst",dstImage);
waitKey();
return 0;
}
圖形繪制函數
Ellipse函數的用法
函數原型:void ellipse(Mat&img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar&color, int thickness=1, int lineType=8, int shift=0)
img :畫布容器
center:橢圓中心
axes :大小位於該矩形中
angle:橢圓的旋轉角度
startangle:開始弧度
endAngle:結束弧度
color :圖形顏色
thickness :線寬
lineType :線型
shift :圓心坐標點和數軸的精度
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
#define WINDOW_WIDTH 600
void DrawEllipse(Mat img,double angle)
{
int thickness = 2;
int lineType = 8;
ellipse(
img,
Point(WINDOW_WIDTH/2,WINDOW_WIDTH/2),
Size(WINDOW_WIDTH/4,WINDOW_WIDTH/16),
angle,
0,
360,
Scalar(255,129,0),
thickness,
lineType
);
}
int main()
{
Mat img(1000,600,CV_8UC3,Scalar::all(0));
DrawEllipse(img,30);
imshow("ellipse",img);
waitKey();
return 0;
}
Circle 函數的用法
函數原型 void circle(Mat& img, Point center, int radius, const Scalar& color, intthickness=1, int lineType=8, int shift=0)
img: 將要畫圓的圖像;
center: 圓心;
radius: 半徑;
color: 圓的顏色;
thickness: 如果值是正,圓外輪廓的厚度,如果值是負,表示要繪制一個填充圓;
lineType:線類型;
shift:
void DrawCircle(Mat img)
{
int thickness = -1;
int lineType = 8;
circle(
img,
Point(100,300),
60,
Scalar(0,89,255),
thickness,
lineType
);
}
line 函數的用法
函數原型 line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
img :輸出圖像
pt1 :開始點
pt2 :結束點
color:線條顏色
thickness :線條粗細
lineType :線型
shift
void DrawLine(Mat img)
{
int thickness = 1;
int lineType = 8;
line(
img,
Point(100,200),
Point(500,600),
Scalar(255,23,56),
thickness,
lineType
);
}