Opencv绘制曲线,并添加标注

#include "draw.h"
#include "stdio.h"

using namespace cv;

void draw_route(const double(*C)[2], int *Shortest_Route)
{
	Size size(560, 560);
	Mat img = Mat::zeros(size, CV_8UC3);
	img += Scalar(255, 255, 255);
	rectangle(img, Rect(30, 30, 500, 500), Scalar(0, 255, 0));
	for (int i = 0; i < 31; i++)	//画点
	{
		Point p((C[i][0] / 10), (C[i][1] / 10));
		circle(img, p, 3, Scalar(255, 0, 0));
	}
	for (int i = 0; i < 30; i++)	//画线
	{
		Point p1((C[*(Shortest_Route + i)][0] / 10), (C[*(Shortest_Route + i)][1] / 10));
		Point p2((C[*(Shortest_Route + i + 1)][0] / 10), (C[*(Shortest_Route + i + 1)][1] / 10));
		line(img, p1, p2, Scalar(255, 0, 255));
	}
	Point p1((C[*(Shortest_Route)][0] / 10), (C[*(Shortest_Route)][1] / 10));
	Point p2((C[*(Shortest_Route + 30)][0] / 10), (C[*(Shortest_Route + 30)][1] / 10));
	line(img, p1, p2, Scalar(255, 0, 255));

	IplImage *src;					//给图片添加标注
	src = &IplImage(img);
	CvFont font, font1;
	double hScale = 0.9;
	double vScale = 0.9;
	int lineWidth = 1;
	cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth);
	cvInitFont(&font1, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale - 0.4, vScale - 0.4, 0, lineWidth);
	cvPutText(src, "The shortest route", cvPoint(150, 25), &font, CV_RGB(0, 0, 0));
	
	for (int i = 0; i < 31; i++)	//添加城市名字
	{
		char str[10];
		sprintf_s(str, "%d", i);
		cvPutText(src, str, cvPoint((C[i][0] / 10 + 3), (C[i][1] / 10)), &font1, CV_RGB(128, 128, 255));
	}

	namedWindow("The shortest route", CV_WINDOW_NORMAL);	//使窗口可以调节大小
	imshow("The shortest route", img);
	waitKey(0);
}

void draw_Lbest(const int NC_max, double *L_best)
{
	Size size(560, 560);
	Mat img = Mat::zeros(size, CV_8UC3);
	img += Scalar(255, 255, 255);
	rectangle(img, Rect(30, 30, 500, 500), Scalar(0, 255, 0));
	for (int i = 0; i < NC_max - 1; i++)
	{
		Point p1(480.0 / (double)NC_max * (double)i + 30.0, 1000 - (*(L_best + i)) / 20);
		Point p2(480.0 / (double)NC_max * (double)(i + 1) + 30.0, 1000 - (*(L_best + i + 1)) / 20);
		circle(img, p1, 1, Scalar(255, 0, 0));
		line(img, p1, p2, Scalar(255, 0, 255));
	}

	IplImage *src;	//给图片添加标注
	src = &IplImage(img);
	CvFont font, font1;
	double hScale = 0.9;
	double vScale = 0.9;
	int lineWidth = 1;
	cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth);
	cvInitFont(&font1, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale - 0.3, vScale - 0.3, 0, lineWidth);
	cvPutText(src, "Length of the route", cvPoint(150, 25), &font, CV_RGB(0, 0, 0));
	cvPutText(src, "Length", cvPoint(30, 60), &font1, CV_RGB(0, 0, 255));
	cvPutText(src, "Iterations", cvPoint(400, 520), &font1, CV_RGB(0, 0, 255));
	namedWindow("Length of the route", CV_WINDOW_NORMAL);	//使窗口可以调节大小
	imshow("Length of the route", img);
	waitKey(0);
}
	
void draw_Lbest_aco(const int NC_max, double *L_best)
{
	Size size(560, 560);
	Mat img = Mat::zeros(size, CV_8UC3);
	img += Scalar(255, 255, 255);
	rectangle(img, Rect(30, 30, 500, 500), Scalar(0, 255, 0));
	for (int i = 0; i < NC_max - 1; i++)
	{
		Point p1(480.0 / (double)NC_max * (double)i + 30.0, 560 - *(L_best + i));
		Point p2(480.0 / (double)NC_max * (double)(i + 1) + 30.0, 560 - *(L_best + i + 1));
		circle(img, p1, 1, Scalar(255, 0, 0));
		line(img, p1, p2, Scalar(255, 0, 255));
	}

	IplImage *src;	//给图片添加标注
	src = &IplImage(img);
	CvFont font, font1;
	double hScale = 0.9;
	double vScale = 0.9;
	int lineWidth = 1;
	cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth);
	cvInitFont(&font1, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale - 0.3, vScale - 0.3, 0, lineWidth);
	cvPutText(src, "Profit", cvPoint(30, 60), &font1, CV_RGB(0, 0, 255));
	cvPutText(src, "Iterations", cvPoint(400, 520), &font1, CV_RGB(0, 0, 255));
	namedWindow("Length of the route", CV_WINDOW_NORMAL);	//使窗口可以调节大小
	imshow("Length of the route", img);
	waitKey(0);
}

猜你喜欢

转载自blog.csdn.net/weixin_42717395/article/details/88877236