Use cvresize of opencv to scale all the images in the folder to a uniform size.

Source: Internet
Author: User

Before training a classifier, one step is to create a sample. Before creating a classifier, you must scale the positive sample and the secondary sample to a uniform size. How can you batch scale the images in the entire folder? The procedure is as follows:

Run in vc6.0 + opencv2.1,

The following error occurs in vs2010:

Error c2440: 'initializing': cannot convert from 'wchar [260] 'to 'std: basic_string <_ ELEM, _ traits, _ ax>'

Solution: This is caused by the character encoding used by the project. When Unicode is used, lpstr P = Str. getbuffer (LEN + 1) returns this error. The solution is to recompile configureation Properties> General> Character Set = not set in project properties.

 

# Include <iostream>
# Include <windows. h>
# Include <string>
# Include <cv. h>

# Include

Using namespace STD;
Long cc = 0; // The Name Of The stored image 1.jpg 2.jpg ....
Void findfile (string filename, const string filter );

Void setsize (string filename) // scale the image using the cvresize function of opencv
{
Iplimage * src = 0; // source image pointer
Iplimage * DST = 0; // target image pointer
Float scale = 0.618; // The scaling factor is 0.618 times.
Cvsize dst_cvsize; // target image size


CC ++;
Char temp [64];
String str1;
String str2;
Sprintf (temp, "% d", CC );
Str1 = temp;
 

Src = cvloadimage (filename. c_str ());//
Dst_cvsize.width = 32; // The width of the target image is fixed to 32 pixels.
Dst_cvsize.height = 24; // Src-> height * (float) dst_cvsize.width/src-> width); // zoom in at a high ratio
DST = cvcreateimage (dst_cvsize, Src-> depth, Src-> nchannels); // construct the Target Image
 
Cvresize (SRC, DST, cv_inter_linear); // scales the source image to the target image.
Sprintf (temp, "E: // PIC // neg ");
Str2 = temp;
Filename = str2 + "//" + str1 + ". jpg"; // str2 = "E: // PIC //... "
Cvsaveimage (filename. c_str (), DST); // The saved location after scaling
 
/*
Cvnamedwindow ("src", cv_window_autosize); // create a window for displaying the source Image
Cvnamedwindow ("DST", cv_window_autosize); // create a window for displaying the Target Image
Cvshowimage ("src", Src); // display the source Image
Cvshowimage ("DST", DST); // display the Target Image
Cvreleaseimage (& SRC); // release the memory occupied by the source Image
Cvreleaseimage (& DST); // release the memory occupied by the target image
Cvdestroyallwindows ();
*/
Cvwaitkey (-1); // wait for user response
 
}

Void findfile (string filename, string filter) // traverses all JPG files in the folder.
{
Win32_find_data findfiledate; // data buffer
Handle hfind = invalid_handle_value;
If (filename [filename. Length ()-1]! = '//')
Filename + = '//';
String lpfilename = filename + "//" + filter; // if the function fails, the return value is invalid_handle_value
Hfind = findfirstfile (lpfilename. c_str (), & findfiledate );
If (hfind = invalid_handle_value) return;
While (true)
{// Determine whether it is a file or a folder
If (findfiledate. dwfileattributes> = 32)
{
Cout <"file:" <findfiledate. cfilename;
Cout <'/t' <"&" <FILENAME <"77" <lpfilename <Endl;
Lpfilename = filename + "//" + findfiledate. cfilename;
Setsize (lpfilename); // scale the image
}
// For folders
Else
{
// If it is not a parent directory
String fname = findfiledate. cfilename;
If (fname! = "." & Fname! = "..")
{
Lpfilename = filename + fname;
Findfile (lpfilename, filter );

}
}
// If the next query succeeds, a value other than 0 is returned; otherwise, 0 is returned.
Int result = findnextfile (hfind, & findfiledate );
If (result = 0) break;
}
Findclose (hfind );
// Release the handle
Return;
}

Int main (INT argc, char * argv [])
{
String directory = "E: // original (1) // original // neg // ch00_20110121092157"; // directory of the file to be found
String filter = "*. jpg"; // file with the suffix. jpg
Findfile (directory, filter );
Return 0;
}

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.