C++修改图片像素大小,求梯度,创建文件夹并读取文件数据,

来源:互联网 发布:淘宝图片推荐几款相机 编辑:程序博客网 时间:2024/06/05 23:08
#include<opencv2/opencv.hpp>
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<time.h>
#include<math.h>
#include<direct.h>
using namespace cv;
using namespace std;


static void read_imgList(const string& filename) {
    std::ifstream file(filename, ios::in);
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(CV_StsBadArg, error_message);
    }
    string cate;
string line;
int num;
int i=0;
ostringstream path;
    while (!file.eof()) {
if(!(file>>cate>>num)) break;
string path0="C:\\Users\\zhh\\Desktop\\newx2\\"+cate;
if(mkdir(path0.c_str())==0) 
{
  cout<<"创建成功"<<endl;
//  break;
}

for(i=0;i<num;i++)
{
file>>line;
Mat image =imread(line,1);
cout<<line<<endl;
Mat *image1;
image1=&image;
float b,g,r;
b=g=r=0;
Mat image2(image1->rows,image1->cols,CV_8UC3,Scalar(0,0,0));
int rows,cols;
for(rows=0;rows<image2.rows;rows++)
{
Vec3b *p=image2.ptr<Vec3b>(rows);
//Vec3b *p1=image1->ptr<Vec3b>(rows-1);
Vec3b *p2=image1->ptr<Vec3b>(rows);
for(cols=1;cols<image2.cols-1;cols++)
{  
b=p2[cols][0]/16;
g=p2[cols][1]/16;
r=p2[cols][2]/16;
p[cols][0]=b*b;
p[cols][1]=g*g;
p[cols][2]=r*r;
}
}
stringstream path;
path<<path0<<"\\"<<i<<".jpg"<<endl;
string path1;
path>>path1;
cout<<path1<<endl;
imwrite(path1,image2);
}
    }
}


//图片降低维度
/*static void read_imgList(const string& filename) {
    std::ifstream file(filename.c_str(), ifstream::in);
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(CV_StsBadArg, error_message);
    }
    string line;
int i=0;
ostringstream path;
    while (getline(file, line)) {


Mat image =imread(line,1);
cout<<line<<endl;
Mat reimage;
resize(image,reimage,Size(100,100),0,0,1);
stringstream path;
   path<<"C:\\Users\\zhh\\Desktop\\newimage\\"<<i<<".jpg"<<endl;
string path1;
path>>path1;
cout<<path1<<endl;

imwrite(path1,reimage);
        i++;
    }
}*/
int main()
{
clock_t start,end;
double t;
start =clock();
read_imgList("C:\\Users\\zhh\\Desktop\\train.txt");


   end = clock();
t =(double)(end-start)/(CLOCKS_PER_SEC);
cout<<t<<endl;
waitKey();
int i;
cin>>i;


system("paues");
//destroy();
return 0;
}
阅读全文
0 0
原创粉丝点击