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=ℑ
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;
}
#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=ℑ
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
- C++修改图片像素大小,求梯度,创建文件夹并读取文件数据,
- Java读取并修改图片的像素值
- 图片像素、大小的修改
- Matlab读取文件夹中子文件夹中的图片并修改尺寸
- 上传图片并修改其大小(C#)
- 上传图片并修改其大小(C#)
- linux c创建文件夹,并在文件夹中创建文件
- Java读取图片像素和大小
- [C++]读取文件夹中图片,并灰度化
- linux c 读取并处理文件夹下的所有文件
- MFC读取图片文件并获得图片头和数据
- 在指定路径创建文件夹,读取图片文件,缩放图片,判断文件夹是否存在,
- 使用libpng读取PNG图片像素数据 .
- drawable文件夹图片在各个像素密度下的大小
- Android创建文件夹及文件并写入数据
- Android创建文件夹及文件并写入数据
- Android创建文件夹及文件并写入数据
- Android创建文件夹及文件并写入数据
- 盘点:恢复删除了的聊天记录方法汇总
- 用了这款自动化表格制作软件,跟手工表格说再见
- 图像识别的深度学习
- ExtJS基本点总结
- 谷歌 火狐 主页被篡改,修改文件名解决
- C++修改图片像素大小,求梯度,创建文件夹并读取文件数据,
- 约束优化方法之拉格朗日乘子法与KKT条件
- ubuntu17.04将左侧启动栏/任务栏,放到桌面底部
- SpringBoot(四):与dobbo的整合(注解形式与xml形式)
- (lintcode)第29题交叉字符串
- JVM垃圾回收策略和垃圾收集器
- Mac下卸载Nginx服务器
- 伪类和伪元素的区别(结论)
- 最大递增序列之和