Opencv初探

Opencv初探,第1张

Opencv初探

文章目录

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

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

结果 2-2与示例2-1不同的是直接使用using namespace std2-3、一个简单的播放视频文件的OpenCV程序2-4加入了滑动条的基本浏览窗口

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

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

结果 2-8、在一个简单图像处理流程中结合图像金字塔 *** 作和Canny边缘检测器2-9、读写示例2-8中的像素值

结果 2-10、同一个对象可以读取视频文件也可以连接摄像头2-11、一个完整的读取彩色视频并转换为对数极坐标视频的程序


2-1、一个简单的加载并显示图像的OpenCV程序
#include
#include
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
#include
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
#include
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
#include

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
#include
#include
#include

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
#include

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
#include

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
#include

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
#include

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(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(y, x) << std::endl;

	x /= 4; y /= 4;

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

}

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

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

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
#include

#include
#include

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;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5711290.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存