Basic Learning notes-opencv (2): uniform image size method before haartraining

Source: Internet
Author: User

Generally speaking, we need to train a lot of data before machine learning. We always feel that training data is a sacred thing. How can we train it? The mind has always had such a question, but has never had time to experience it. Therefore, we need to learn how to train a. xml file when learning the Adaboost algorithm recently. The methods are the same. Once used, the training process will be similar in the future.

Instead, I tried to perform simple face data training. Instead, I downloaded the Yale University's face database on the Internet. It was created by the Computing Visual and control center of Yale University and contains 165 images from 15 volunteers, contains light, emotion, and posture. Download URL:

Http://cvc.yale.edu/projects/yalefaces/yalefaces.html. The figure is as follows:

(The bottom right corner is my transparent system-themed blue/white porcelain photo. Don't be surprised how it comes out ...)

Yale University face data is in BMP format, 80*80 pixels. Because this haartraining only aims to learn the process, and the algorithm training takes a very long time, less than 1000 of the images will take time in days. So to reduce the training time, we need to reduce the number of images. Here I plan to use 133 positive sample images and 22 other images for testing. On the other hand, I plan to reduce the image size, unify the 80*80 images to a size of 24*24 pixels.

(Here we have two tips: 1. Understand the concept of image size. For example, if the image size is 80*80, the image size is 80*80, not the resolution, but 80*80 pixels, that is, the width is 80 pixels, the length is also 80 pixels. Because a pixel width has been determined on a display device. Therefore, when an image is enlarged, its size pixels may change, for example, to 100*100 pixels, because its length and width change during the amplification. However, the resolution remains unchanged when the image is enlarged. In addition, the relationship between pixels and length CM also depends on the image resolution. For example, 1 CM = 28 pixels usually refer to 72 pixels/square inch. Therefore, if the pixels of a digital camera are too high, it will not be very helpful for the display of the image definition. The pixel height can only print larger and clearer photos.

2. In a window, if an image is in BMP format. I only need to change the suffix to another one, such as JPG. Right-click the file to view its properties in JPG format and open it. Is it true that it is in JPG format? Certainly not. Comparing BMP with the modified JPG, we will find that the sizes of the two are the same. Finally, use imfinfo of the MATLAB command to view the image information, which is also in BMP format. It indicates that modifying the suffix is misleading and you must be careful when performing image processing in the future .)

The next step is to reduce the size of the 165 images to 24x24 pixels. Of course, there is a stupid way to use the image viewing software (such as ACDSee) one by one conversion. However, if there are thousands of images, it will be troublesome. I don't know what batch processing tools are available. If so, you can share them.

So I plan to write a small program to complete the batch processing, and the image scaling will use the existing functions in opencv. The main purpose of writing this applet is to practice operations on file names, because I have never been familiar with these things before. I have practiced these things today and I still have some ideas.

Just now, we can see that the name of the human face data library, for example, "s1.bmp", represents the first face image of the first person. The last name to be adjusted is 1_s1n.bmp.

First, create the Yale and yale_small_size folders under the project file directory, and copy 165 face data images to the Yale folder. Finally, all the small-size images generated by the program are placed in the yale_small_size folder. As follows:

The program code is as follows:

1 // change_img_size.cpp: defines the entry point of the console application.
2 //
3 # include "stdafx. H" // This header file must be placed at the top; otherwise, it is easy to report errors.
4
5 # include "opencv2/imgproc. HPP"
6 # include "opencv2/highgui. HPP"
7
8 # include <iostream>
9 # include <stdio. h>
10
11 using namespace CV;
12 using namespace STD;
13
14 # define dst_img_width 24 // adjust the size and width of the image
15 # define src_img_heigh 24 // adjust the size and height of the image
16
17 int main (INT argc, char * argv [])
18 {
19 mat src_img;
20 int I, J;
21 string src_img_name = "Yale/", dst_img_name = "yale_small_size/"; // folder name of the source file and target file
22 char Chari [5], charj [5]; // because there are not many face data, subscript 5 is enough
23 For (I = 1; I <= 15; I ++) // 15 people's face data
24 {
25 For (j = 1; j <= 11; j ++) // each person's face has 11 different expressions.
26 {
27 ITOA (I, Chari, 10); // convert the variable to the character type. Here, Chari is the first address of the character array, but if it is defined as char * Chari = ""; then an error occurs, why?
28 ITOA (J, charj, 10 );
29
30 src_img_name + = Chari; // The original graph name format is, for example, the 6th figure of the 5th individual, 5_s6.bmp
31 src_img_name + = "_ s ";
32 src_img_name + = charj;
33 src_img_name + = ". BMP ";
34
35 src_img = imread (src_img_name, 1 );
36 mat dst_img_rsize (dst_img_width, src_img_heigh, src_img.type ());
37 resize (src_img, dst_img_rsize, dst_img_rsize.size (), 0, inter_linear );
38
39 dst_img_name + = Chari; // The name format of the converted graph is: instance, 5_s6n.bmp
40 dst_img_name + = "_ s ";
41 dst_img_name + = charj;
42 dst_img_name + = "n.bmp ";
43
44 imwrite (dst_img_name, dst_img_rsize );
45 src_img_name = "Yale/", dst_img_name = "yale_small_size/"; // clear the content in the 0-character array after each loop. The destination folder must be created in advance; otherwise, no effect will be obtained.
46
47}
48}
49 return 0;
50}

The following figure shows the generated normalized image (in the yale_small_size folder ):

 

Well, the very simple work has been completed, and there is no content. You may have time to write an interface later. You can adjust the image size to a specified pixel through batch processing to facilitate the preliminary processing of visual images in the future.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.