Opencv初探


2-1、一个简单的加载并显示图像的OpenCV程序

#include<opencv2/opencv.hpp>
#include<string>
static int test()
{
	static std::string path = "grayImage.jpg";
	static std::string name = "Example1";
	cv::Mat img = cv::imread(path);
	if (img.empty()) return -1;
	cv::namedWindow(name, cv::WINDOW_AUTOSIZE);
	cv::imshow(name, img);
	cv::waitKey(0);
	cv::destroyWindow(name);
	return 0;
}
int main()
{
	test();
	system("pause");
	return 0;
}

结果

在这里插入图片描述

2-1、Canny边缘检测器输出一个单通道的(灰度)图像

#include<opencv2/opencv.hpp>
#include<string>
static void test()
{
	static std::string path("renwu1.jpg");
	static cv::Mat img_rgb, img_gry, img_cny;
	cv::namedWindow("Example RGB", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example Gray", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example Canny", cv::WINDOW_AUTOSIZE);

	img_rgb = cv::imread(path);
	cv::imshow("Example RGB", img_rgb);
	cv::cvtColor(img_rgb, img_gry, cv::COLOR_BGR2GRAY);
	cv::imshow("Example Gray", img_gry);

	cv::Canny(img_gry, img_cny, 10, 100, 3, true);
	cv::imshow("Example Canny", img_cny);
	cv::waitKey(0);
}

int main()
{
	test();
	system("pause");
	return 0;
}

结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2-2与示例2-1不同的是直接使用using namespace std

#include<opencv2/opencv.hpp>
#include<string>
using namespace std;
using namespace cv;

static int test()
{
	static string path = "grayImage.jpg";
	static string name = "Example1";
	Mat img = imread(path);
	if (img.empty()) return -1;
	namedWindow(name, WINDOW_AUTOSIZE);
	imshow(name, img);
	waitKey(0);
	destroyWindow(name);
	return 0;
}
int main()
{
	test();
	system("pause");
	return 0;
}

2-3、一个简单的播放视频文件的OpenCV程序

#include<opencv2/opencv.hpp>
#include<string>

static void test()
{
	static std::string name = "Example3";
	static std::string path = "yishenga.mp4";
	cv::namedWindow(name, cv::WINDOW_AUTOSIZE);
	cv::VideoCapture cap;
	cap.open(path);
	cv::Mat frame;
	for (;;) {
		cap >> frame;
		if (frame.empty()) break;
		cv::imshow(name, frame);
		if (cv::waitKey(33) >= 0) break;
	}
	return;
}

int main()
{
	test();
	system("pause");
	return 0;
}

2-4加入了滑动条的基本浏览窗口

#include<opencv2/opencv.hpp>
#include<iostream>
#include<string>
#include<fstream>

static int g_slider_position = 0;
static int g_run = 1, g_dontset = 0;  //start out in single step mode
static cv::VideoCapture g_cap;

static void onTrackbarSlide(int pos, void*) {
	g_cap.set(cv::CAP_PROP_POS_FRAMES, pos);
	if (!g_dontset)
		g_run = 1;
	g_dontset = 0;
}

static void test()
{
	static std::string name = "Example2_4";
	static std::string path = "yishenga.mp4";
	cv::namedWindow(name, cv::WINDOW_AUTOSIZE);
	g_cap.open(path);
	int frames = (int)g_cap.get(cv::CAP_PROP_FRAME_COUNT);
	int tmpw = (int)g_cap.get(cv::CAP_PROP_FRAME_WIDTH);
	int tmph = (int)g_cap.get(cv::CAP_PROP_FRAME_HEIGHT);

	std::cout << "Video has" << frames << "frames of dimensions("
		<< tmpw << "," << tmph << ")." << std::endl;
	cv::createTrackbar("Position", name, &g_slider_position, frames, onTrackbarSlide);
	cv::Mat frame;
	for (;;) {
		if (g_run != 0) {
			g_cap >> frame;
			if (frame.empty()) break;
			int current_pos = (int)g_cap.get(cv::CAP_PROP_POS_FRAMES);
			g_dontset = 1;
			cv::setTrackbarPos("Position", name, current_pos);
			cv::imshow(name, frame);
			g_run = 1;
		}
		
		char c = (char)cv::waitKey(10);
		if (c == 's')  //single step
		{
			g_run = 1;
			std::cout << "Single step,run = " << g_run << std::endl;
		}
		if (c == 'r') //run mode
		{
			g_run = -1;
			std::cout << "Run mode,run = " << g_run << std::endl;
		}
		if (c == 27)
			break;
	}
	return;
}

int main()
{
	test();
	system("pause");
	return 0;
}

结果

在这里插入图片描述

2-5、加载图像并且在显示之前平滑处理

#include<opencv2/opencv.hpp>
#include<string>

static void example2_5(const cv::Mat &image) {
	//Create some windows to show the input and output images in.
	static std::string input_name = "Example2_5-in";
	static std::string output_name = "Example2_5-out";
	cv::namedWindow(input_name, cv::WINDOW_AUTOSIZE);
	cv::namedWindow(output_name, cv::WINDOW_AUTOSIZE);

	//Create a window to show our input image
	cv::imshow(input_name, image);

	//Create an image to hold the smoothed output
	cv::Mat out;
	//Do the smoothing
	//(Note:Could use GaussianBlur(),blur(),medianBlur() or bilateralFilter().)
	//
	cv::GaussianBlur(image, out, cv::Size(5,5), 3, 3);
	cv::GaussianBlur(out, out, cv::Size(5, 5), 3, 3);

	//Show the smoothed image in the output window
	//

	cv::imshow(output_name, out);
	//Wait for the user to hit key,window will self destruct
	//
	cv::waitKey(0);
}

static void test()
{
	std::string path = "grayImage.jpg";

	cv::Mat image = cv::imread(path);
	example2_5(image);
}

int main()
{
	test();
	system("pause");
	return 0;
}

结果

在这里插入图片描述

2-6、使用pyrDown()来创建一个新的图像,其宽高均为原始图像的一半

#include<opencv2/opencv.hpp>
#include<string>

static void test()
{
	static std::string namewindow1("Example1");
	static std::string namewindow2("Example2");
	static std::string path = "grayImage.jpg";
	cv::Mat image1, image2;
	cv::namedWindow(namewindow1, cv::WINDOW_AUTOSIZE);
	cv::namedWindow(namewindow2, cv::WINDOW_AUTOSIZE);
	image1 = cv::imread(path);
	cv::imshow(namewindow1, image1);
	cv::pyrDown(image1, image2);
	cv::imshow(namewindow2, image2);

	cv::waitKey(0);
	return;
}

int main()
{
	test();
	system("pause");
	return 0;
}

结果

在这里插入图片描述

2-8、在一个简单图像处理流程中结合图像金字塔操作和Canny边缘检测器

#include<opencv2/opencv.hpp>
#include<string>

static void test()
{
	static std::string path("renwu1.jpg");
	static cv::Mat img_rgb, img_gry, img_cny;
	cv::namedWindow("Example RGB", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example Gray", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example Canny", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example pyrDown", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example pyrDown2", cv::WINDOW_AUTOSIZE);

	cv::namedWindow("Example Canny1", cv::WINDOW_AUTOSIZE);
	img_rgb = cv::imread(path);
	cv::imshow("Example RGB", img_rgb);
	cv::cvtColor(img_rgb, img_gry, cv::COLOR_BGR2GRAY);
	//............................

	cv::imshow("Example Gray", img_gry);

	cv::Canny(img_gry, img_cny, 10, 100, 3, true);
	cv::imshow("Example Canny", img_cny);

	//................2-8...................
	cv::Mat img_pyr,img_pyr2, img_cny1;
	cv::pyrDown(img_gry, img_pyr);
	cv::imshow("Example pyrDown", img_pyr);
	cv::pyrDown(img_pyr, img_pyr2);
	cv::imshow("Example pyrDown2", img_pyr2);

	cv::Canny(img_pyr2, img_cny1, 10, 100, 3, true);
	cv::imshow("Example Canny1", img_cny1);
	cv::waitKey(0);
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

2-9、读写示例2-8中的像素值

#include<opencv2/opencv.hpp>
#include<string>

static void test()
{
	static std::string path("renwu1.jpg");
	static cv::Mat img_rgb, img_gry, img_cny;
	cv::namedWindow("Example RGB", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example Gray", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example Canny", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example pyrDown", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Example pyrDown2", cv::WINDOW_AUTOSIZE);

	cv::namedWindow("Example Canny1", cv::WINDOW_AUTOSIZE);
	img_rgb = cv::imread(path);
	cv::imshow("Example RGB", img_rgb);
	cv::cvtColor(img_rgb, img_gry, cv::COLOR_BGR2GRAY);
	//............................

	cv::imshow("Example Gray", img_gry);

	cv::Canny(img_gry, img_cny, 10, 100, 3, true);
	cv::imshow("Example Canny", img_cny);

	//................2-8...................
	cv::Mat img_pyr, img_pyr2, img_cny1;
	cv::pyrDown(img_gry, img_pyr);
	cv::imshow("Example pyrDown", img_pyr);
	cv::pyrDown(img_pyr, img_pyr2);
	cv::imshow("Example pyrDown2", img_pyr2);

	cv::Canny(img_pyr2, img_cny1, 10, 100, 3, true);
	cv::imshow("Example Canny1", img_cny1);

	int x = 16, y = 32;
	cv::Vec3b intensity = img_rgb.at<cv::Vec3b>(y, x);
	uchar blue = intensity[0];
	uchar green = intensity[1];
	uchar red = intensity[2];
	std::cout << "At(x,y) = (" << x << "," << y << "):(blue,green,red) = (" <<
		(unsigned int)blue << "," << (unsigned int)green << "," <<
		(unsigned int)red << ")" << std::endl;
	std::cout << "Gray pixel there is:" << 
		(unsigned int)img_gry.at<uchar>(y, x) << std::endl;

	x /= 4; y /= 4;

	std::cout << "Pyramid2 pixel there is:" <<
		(unsigned int)img_pyr2.at<uchar>(y, x) << std::endl;
	img_cny1.at<uchar>(x, y) = 128;
	cv::waitKey(0);

}

int main()
{
	test();
	system("pause");
	return 0;
}

结果

在这里插入图片描述

2-10、同一个对象可以读取视频文件也可以连接摄像头

#include<opencv2/opencv.hpp>
#include<string>
#include<iostream>

int main(int argc,char** argv)
{
	
	cv::namedWindow("Example2_10", cv::WINDOW_AUTOSIZE);
	cv::VideoCapture cap;
	if (argc == 1) {
		cap.open(0);
	}
	else {
		cap.open(argv[1]);
	}
	if (!cap.isOpened()) {
		std::cerr << "Couldn't open capture." << std::endl;
		return -1;
	}
	system("pause");
	return 0;
}

2-11、一个完整的读取彩色视频并转换为对数极坐标视频的程序

#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>

#include<iostream>
#include<string>

static void test()
{
	std::string path("yishenga.mp4");
	std::string path1("yi.mp4");
	cv::namedWindow("Example2_11", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("Log_Polar", cv::WINDOW_AUTOSIZE);
	//Note:could capture from a camera by giving a camera id as an int
	cv::VideoCapture capture(path);
	double fps = capture.get(cv::CAP_PROP_FPS);
	cv::Size size(
		(int)capture.get(cv::CAP_PROP_FRAME_WIDTH),
		(int)capture.get(cv::CAP_PROP_FRAME_HEIGHT)
	);

	cv::VideoWriter writer;
	writer.open(path1,cv::CAP_OPENCV_MJPEG, fps, size);
	cv::Mat logpolar_frame, bgr_frame;
	for (;;) {
		capture >> bgr_frame;
		if (bgr_frame.empty()) break;//end if done
		cv::imshow("Example2_11", bgr_frame);
		cv::logPolar(
			bgr_frame,                        //Input color frame
			logpolar_frame,					  //Output log-polar frame
			cv::Point2f(					  //Centerpoint for log-polar transformation
				bgr_frame.cols/2 ,			  //x
				bgr_frame.rows/2 			  //y
			),
			80, 
			cv::WARP_FILL_OUTLIERS
		);
		cv::imshow("Log_Polar", logpolar_frame);
		writer << logpolar_frame;
		char c = cv::waitKey(10);
		if (c == 27) break;
	}
	capture.release();
}

int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值