OpenCV寒假学习day05

OpenCV寒假学习day05

1.图像翻转

代码

#include
#include

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	Mat src = imread("D:/vcprojects/images/test.png");
	if (src.empty()) {
		printf("could not load image...\n");
		return -1;
	}
	imshow("input", src);

	Mat dst;
	// X Flip 倒影
	flip(src, dst, 0);
	imshow("x-flip", dst);

	// Y Flip 镜像
	flip(src, dst, 1);
	imshow("y-flip", dst);

	// XY Flip 对角
	flip(src, dst, -1);
	imshow("xy-flip", dst);

	waitKey(0);
	return 0;
}


运行结果
OpenCV寒假学习day05_第1张图片

重要函数
flip(src,dst,flipcode) 将图像进行反转(flip=0绕x轴,flip=1绕y轴,flip=-1绕xy轴.)

应用

用于摄像头图像翻转。

2.图像插值

代码

#include
#include

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	Mat src = imread("D:/vcprojects/images/test.png");
	if (src.empty()) {
		printf("could not load image...\n");
		return -1;
	}
	imshow("input", src);

	int h = src.rows;
	int w = src.cols;
	float fx = 0.0, fy = 0.0;
	Mat dst = Mat::zeros(src.size(), src.type());
	resize(src, dst, Size(w * 2, h * 2), fx = 0, fy = 0, INTER_NEAREST);
	imshow("INTER_NEAREST", dst);

	resize(src, dst, Size(w * 2, h * 2), fx = 0, fy = 0, INTER_LINEAR);
	imshow("INTER_LINEAR", dst);

	resize(src, dst, Size(w * 2, h * 2), fx = 0, fy = 0, INTER_CUBIC);
	imshow("INTER_CUBIC", dst);

	resize(src, dst, Size(w * 2, h * 2), fx = 0, fy = 0, INTER_LANCZOS4);
	imshow("INTER_LANCZOS4", dst);

	waitKey(0);
	return 0;
}


重要函数 功能
resize(src,dst,size,fx,fy,interpolation) 缩小或者放大函数至某一个大小

具体含义及使用
INTER_NEAREST - 最邻近插值
INTER_LINEAR - 双线性插值
INTER_CUBIC - 4x4像素邻域内的双立方插值
INTER_LANCZOS4 - 8x8像素邻域内的Lanczos插值

应用

暂时不知道用于什么,欢迎大佬告知。

3.几何图形绘制

代码

#include
#include

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	Mat image = Mat::zeros(Size(512, 512), CV_8UC3);
	Rect rect(100, 100, 200, 200);
	rectangle(image, rect, Scalar(255, 0, 0), 2, LINE_8, 0);
	circle(image, Point(256, 256), 50, Scalar(0, 0, 255), 2, LINE_8, 0);
	ellipse(image, Point(256, 256), Size(150, 50), 360, 0, 360, Scalar(0, 255, 0), 2, LINE_8, 0);
	imshow("image", image);
	waitKey(0);

	RNG rng(0xFFFFFF);
	image.setTo(Scalar(0, 0, 0));

	for (int i = 0; i < 100000; i++) {
		//image.setTo(Scalar(0, 0, 0));
		int x1 = rng.uniform(0, 512);
		int y1 = rng.uniform(0, 512);
		int x2 = rng.uniform(0, 512);
		int y2 = rng.uniform(0, 512);

		int b = rng.uniform(0, 256);
		int g = rng.uniform(0, 256);
		int r = rng.uniform(0, 256);
		line(image, Point(x1, y1), Point(x2, y2), Scalar(b, g, r), 1, LINE_AA, 0);
		rect.x = x1;
		rect.y = y1;
		rect.width = x2 - x1;
		rect.height = y2 - y1;
		//rectangle(image, rect, Scalar(b, g, r), 1, LINE_AA, 0);
		imshow("image", image);
		char c = waitKey(20);
		if (c == 27)
			break;

		imshow("image", image);
	}
	waitKey(0);
	return 0;
}


本次函数的用法大同小异,因此不再一一例举。

4.问题

暂无

5.总结

更多的OpenCV函数的使用,慢慢学习,加油!

你可能感兴趣的:(OpenCV寒假学习day05)