学习opencv3--第六章答案

1.

#include 
#include 

using namespace std;

int main(int argc, const char** argv)
{
	cv::Mat src;
	src = cv::imread("C:/Users/duoduoda/Desktop/dog.jpg");
	//cv::circle()绘制一个简单的圆
	cv::circle(src, cv::Point(src.cols / 2, src.rows / 2), 100, cv::Scalar(0, 0, 255));
	//cv::ellipse()画一个椭圆(可以倾斜,或者只有部分圆弧)
	cv::ellipse(src, cv::Point(100, 200), cv::Size(50, 100), 45.0, 0.0, 180.0, cv::Scalar(255, 0, 0));
	//cv::fillConvexPoly()画一个填充的简单多边形
	cv::Point one_pts[5];
	one_pts[0] = cv::Point(100, 100);
	one_pts[1] = cv::Point(100, 300);
	one_pts[2] = cv::Point(300, 400);
	one_pts[3] = cv::Point(400, 400);
	one_pts[4] = cv::Point(400, 100);
	cv::fillConvexPoly(src, one_pts, 5, cv::Scalar(0, 255, 0));
	//cv::fillPoly()画一个填充的任意多边形
	cv::Point pts[1][5];
	pts[0][0] = cv::Point(100, 100);
	pts[0][1] = cv::Point(100, 200);
	pts[0][2] = cv::Point(200, 300);
	pts[0][3] = cv::Point(300, 200);
	pts[0][4] = cv::Point(200, 100);
	int npt[] = { 5 };
	const cv::Point *ppts[] = { pts[0] };
	cv::fillPoly(src, ppts, npt, 1, cv::Scalar(200, 200, 200));
	//cv::line()画一个简单直线
	cv::line(src, cv::Point(300, 300), cv::Point(500, 500), cv::Scalar(0, 0, 255));
	//cv::rectangle()画一个简单矩形
	cv::rectangle(src, cv::Rect(300, 300, 100, 100), cv::Scalar(100, 100, 100));
	//cv::polyLines()画多重折线
	cv::Point dots[1][5];
	dots[0][0] = cv::Point(300, 300);
	dots[0][1] = cv::Point(400, 400);
	dots[0][2] = cv::Point(400, 300);
	dots[0][3] = cv::Point(300, 200);
	dots[0][4] = cv::Point(200, 100);
	const cv::Point *aa[] = { dots[0] };
	int bb[] = { 5 };
	cv::polylines(src, aa, bb, 1, 1, cv::Scalar(0, 0, 100));
	cv::imshow("input", src);
	cv::waitKey(0);
	return 0;
}

2.

#include 
#include 

using namespace std;

int main(int argc, const char** argv)
{
	cv::Mat src,gray_src;
	src = cv::imread("C:/Users/duoduoda/Desktop/dog.jpg");
	cv::cvtColor(src, gray_src, cv::COLOR_BGR2BGRA);
	cv::putText(gray_src, "Hello World", cv::Point(400, 400), 3, 3.0, cv::Scalar(0, 0, 255));
	cv::imshow("input", gray_src);
	cv::waitKey(0);
	return 0;
}

3.

#include 
#include 

using namespace std;

int main(int argc, const char** argv)
{
	cv::namedWindow("video", cv::WINDOW_AUTOSIZE);
	cv::VideoCapture cap;
	cap.open("C:/Users/duoduoda/Desktop/sequence.avi");
	if (!cap.isOpened())
	{
		cout << "could not open video" << endl;
		return -1;
	}
	int fps = cap.get(cv::CAP_PROP_FPS);
	cv::Mat frame;
	while (true)
	{
		cap >> frame;
		if (frame.empty())
			break;
		cv::putText(frame, "FPS"+to_string(fps), cv::Point(20, 20), 1, 1.0, cv::Scalar(0, 0, 255));
		cv::waitKey(1000 / fps);//帧率就是一秒有几张图片
		cv::imshow("video", frame);
	}
	cv::waitKey(0);
	return 0;
}

5.

#include 
#include 

using namespace std;

int main(int argc, const char** argv)
{
	cv::Mat src;
	src = cv::imread("C:/Users/duoduoda/Desktop/dog.jpg");
	if (!src.data)
	{
		cout << "could not read image..." << endl;
		return -1;
	}
	cv::namedWindow("input", cv::WINDOW_AUTOSIZE);
	//a.
	cv::LineIterator it1(src, cv::Point(10, 10), cv::Point(100, 10), 4);
	cv::LineIterator it2(src, cv::Point(10, 10), cv::Point(100, 10), 4);
	int count_1 = 0, count_2 = 0;
	for (int i = 0; i < it1.count; i++)
		count_1++;
	for (int i = 0; i < it2.count; i++)
		count_2++;
	cout << "4 line1 count:" << count_1 << "  8 line2 count:" << count_2 << endl;
	//b.
	count_1 = 0, count_2 = 0;
	it1 = cv::LineIterator(src, cv::Point(10, 10), cv::Point(100, 100), 4);
	it2 = cv::LineIterator(src, cv::Point(10, 10), cv::Point(100, 100), 8);
	for (int i = 0; i < it1.count; i++)
		count_1++;
	for (int i = 0; i < it2.count; i++)
		count_2++;
	cout << "4 line1 count:" << count_1 << "  8 line2 count:" << count_2 << endl;
	//c.
	count_1 = 0, count_2 = 0;
	it1 = cv::LineIterator(src, cv::Point(10, 10), cv::Point(100, 100), 4);
	it2 = cv::LineIterator(src, cv::Point(10, 10), cv::Point(100, 100), 8);
	for (int i = 0; i < it1.count; i++)
		count_1++;
	for (int i = 0; i < it2.count; i++)
		count_2++;
	cout << "4 line1 count:" << count_1 << "  8 line2 count:" << count_2 << endl;
	cout << "4 line1 length:" << sqrt(90*90+90*90) << "  8 line2 length:" << sqrt(90 * 90 + 90 * 90) << endl;

	cv::waitKey(0);
	return 0;
}

 

你可能感兴趣的:(opencv)