#include
#include
#include
using namespace std;
using namespace cv;
/*
cv::Point与cv::Scalar
1)Point表示2D平面上的一个点x,y
Point p;
p.x=10;
p.y=8;
or
p=Point(10,8);
2)Scalar表示四个元素的向量
Scalar(a,b,c); a=blue,b=green,c=red表示RGB三个通道
*/
void My_Line();
void My_Rectangle();
void My_Ellipse();
void My_Circle();
void My_Ploygon();
void RandomLinedemo();
const char* drawdemo_win = "draw shapes and test demo";
Mat bgImage;
int main(int argc, char** argv)
{
string filepath1 = "D:/Learning/Opecv/1.jpg";
//string filepath2 = "D:/Learning/Opecv/2.jpg";
//Mat src1, src2, dst;
bgImage = imread(filepath1);
//src2 = imread(filepath2);
if (!bgImage.data)
{
cout << "could not load image!!!" << endl;
return -1;
}
//My_Line();
//My_Rectangle();
//My_Ellipse();
//My_Circle();
//My_Ploygon();
//putText(bgImage, "Hello OpenCV", Point(300, 300), CV_FONT_BLACK, 1.0, Scalar(12, 255, 200), 1, 8);
char input_image[] = "input_image";
namedWindow(input_image, CV_WINDOW_AUTOSIZE);
imshow(input_image, bgImage);
RandomLinedemo();
waitKey(0);
return 0;
}
void My_Line()
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 300;
p2.y = 300;
Scalar color = Scalar(0, 0, 255);
line(bgImage, p1, p2, color, 1, LINE_AA); //LINE_AA->反锯齿
}
void My_Rectangle()
{
Rect rect = Rect(200, 100, 300, 300);
Scalar color = Scalar(255, 0, 0);
rectangle(bgImage, rect, color, 2, LINE_8);
}
void My_Ellipse()
{
Scalar color = Scalar(0, 255, 0);
ellipse(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), Size(bgImage.cols / 4, bgImage.rows / 8), 90, 0, 630, color, 2, LINE_4);
}
void My_Circle()
{
Scalar color = Scalar(0, 255, 255);
Point center = Point(bgImage.cols / 2, bgImage.rows / 2);
circle(bgImage, center, 50, color, 1, LINE_4);
}
void My_Ploygon()
{
Point pts[1][5];
pts[0][0] = Point(100, 100);
pts[0][1] = Point(100, 200);
pts[0][2] = Point(200, 200);
pts[0][3] = Point(200, 100);
pts[0][4] = Point(100, 100);
const Point* ppts[] = { pts[0] };
int npt[] = { 5 };
Scalar color = Scalar(255, 12, 255);
fillPoly(bgImage, ppts, npt, 1, color, 8);
}
void RandomLinedemo()
{
RNG rng(12345);
Point pt1;
Point pt2;
Mat bg = Mat::zeros(bgImage.size(), bgImage.type());
namedWindow("random line demo", CV_WINDOW_AUTOSIZE);
for (int i = 0; i < 10000; i++)
{
pt1.x = rng.uniform(0, bgImage.cols);
pt2.x = rng.uniform(0, bgImage.cols);
pt1.y = rng.uniform(0, bgImage.rows);
pt2.y = rng.uniform(0, bgImage.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50) > 0)
break;
line(bg, pt1, pt2, color, 1, 8);
imshow("random line demo", bg);
}
}