Opencv初探

Posted yangyang_z

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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_gryOpenCV 初探

OpenCV入门初探---拿走就能用的 人脸检测小项目 源码分析

图像识别——《OpenCV3编程入门-毛星云》第二部分 初探core组件

视觉检测使用opencv编写一个图片缺陷检测流程

OpenCV 根据图像集检测图像

使用 OpenCV 进行图像检测