Computer Vision - Experiment 1 - Image loading, display and output

计算机视觉 - 实验一 图像的载入、显示与输出

Computer Vision - Experiment 1 - Image loading, display and output

Experimental objectives and requirements

   (i) To master the installation of OpenCV in Windows through experiments;

   (ii) master the method of loading, displaying and outputting images through experiments

Experiment content

   (i) Installing OpenCV in Windows.

   (ii) Write programs for loading, displaying and outputting images.

Experimental instruments, equipment

  A computer with Windows 7 operating system and Visual Studio 2015 installed

Experimental principle

  Opencv uses the Mat class to implement image storage. Images are stored in a matrix format where each pixel has a position and can be referenced by the number of columns and rows. the Mat class is not only used to store images, but also different types of matrices of arbitrary size. The Mat class can be used to perform operations such as matrix addition, matrix multiplication, matrix creation, etc. The imread function is used to read the image. imwrite function is used to write the image. imshow function is used to display the image.

Experimental steps

   (i) Install OpenCV in Windows

  Download opencv source code in the URL https://sourceforge.net/projects/opencvlibrary/ and install it in Windows to install OpenCV.

   (ii) Configuring OpenCV in Visual Studio 2015;

   (iii) Write code to read the image using the imread function;

   (iv) Write code to write images using the imwrite function;

   (v) Write code to display the image using the imshow function.

Experimental notes

   (i) Configure OpenCV in VS after completing the installation of OpenCV;

   (ii) The functions and usage of imread, imwrite, and imshow functions.

Experimental results

   (i) Experimental code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;


int main( )
{
	//-------------------------------【一、图像的载入和显示】----------------------------
	//	描述:以下三行代码用于完成图像的载入和显示
	//---------------------------------------------------------------------------------
	
	cv::Mat girl = cv::imread("girl.jpg", 0); //载入图像到Mat
	namedWindow("【1】动漫图"); //创建一个名为 "【1】e动漫图"的窗口  
	imshow("【1】动漫图",girl);//显示名为 "【1】动漫图"的窗口  

	//-------------------------------【二、初级图像混合】---------------------------------
	//	描述:二、初级图像混合
	//---------------------------------------------------------------------------------
	//载入图片
	Mat image = imread("dota.jpg");
	Mat logo = imread("dota_logo.jpg");

	//载入后先显示
	namedWindow("【2】原画图");
	imshow("【2】原画图",image);

	namedWindow("【3】logo图");
	imshow("【3】logo图",logo);

	// 定义一个Mat类型,用于存放,图像的ROI
	Mat imageROI;
	//方法一
	imageROI= image(Rect(800,350,logo.cols,logo.rows));
	//方法二
	//imageROI= image(Range(350,350+logo.rows),Range(800,800+logo.cols));

	// 将logo加到原图上
	addWeighted(imageROI,0.5,logo,0.3,0.,imageROI);

	//显示结果
	namedWindow("【4】原画+logo图");
	imshow("【4】原画+logo图",image);

	//--------------------------------【三、图像的输出】----------------------------------
	//	描述:将一个Mat图像输出到图像文件
	//---------------------------------------------------------------------------------
	//输出一张jpg图片到工程目录下
	imwrite("由imwrite生成的图片.jpg", image);
	//imwrite("由imwrite生成的图片.jpg", image);

	waitKey(0);

	return 0;
}

   (ii) Show results

Image loading, display and output 1

Image loading, display and output 2

Image loading, display and output 3

Image loading, display and output 4

Image loading, display and output 5

Image loading, display and output 6

Experiment Summary

  The main content of this experiment is to write a program for loading, displaying and outputting images. The main functions used are cv::Mat girl=cv::imread(“xx.jpg”, 0); (load image to Mat, the first parameter xx.jpg is the name of the image, the second parameter 0 is the grayscale map); namedWindow(“window name”);; display function imshow(“window name”, xx);; load image function Mat image= imread(“xx.jpg”);; imageROI= image(Rect());; addWeighted();; imwrite(".jpg",image);;; etc. functions. This experiment allowed me to fully understand the above functions and the flow of the experiment. I learned how to use the above functions to load, display and input the most basic images by myself.

Built with Hugo
Theme Stack designed by Jimmy