Come in often deal with a large number of pictures, pictures of the operation of the name is really troublesome. Let's talk about the most commonly used file read and write operations.
After clicking the open link, the author writes very clearly. After understanding the article in this blog post, it is much easier.
Header files to include: <fstream>
Namespaces: STD
You can also try <fstream.h>
FStream provides three classes for implementing C + + operations on files. (file creation, read-write).
Ifstream-read from an existing file
Ofstream-Write content to a file
FStream-Open file for read and write
Supported file types
in fact, file types can be divided into two categories: text files and binaries .
A text file holds a readable character, while a binary file saves only binary data.
Using binary mode, you can manipulate images and other files.
In text mode, you can only read and write text files. Otherwise you will get an error.
Since I have stored the name of the picture in a TXT file,
Every time I read a line, I read a picture and then I do the work.
For example, I'm going to preprocess a batch of images, and the preprocessing method is mean reduction and mean variance of 1, then the picture is one to 0-255 and stored in another folder.
The code is as follows:
#include <iostream> #include <fstream> #include <string> #include <opencv2/opencv.hpp>using Namespace Std;using namespace Cv;int main () {Mat src,src0,img,lala,final,over;double average;ifstream fs;fs.open (" Twoclasstrnegativeimglist.txt ", ios::in); char img_name[128]; Const string Imgpath= "d:/twonegative/"; const string imgsavepath= "d:/processed2negative/"; while (Fs.getline (img_name ) {Src=imread (imgpath+img_name,-1); Src.convertto (SRC0,CV_64FC1); Meanstddev (Src0,img,lala); Final= ( Src-img.at<double> (0,0))/lala.at<double> (0,0); normalize (final, over, 0, 255, Norm_minmax, CV_8UC1); Imwrite (Imgsavepath+img_name,over);} Waitkey (); return 0;}
C + + realizes the mean reduction of high-volume images by 1 operation