图片转码程序 通过遍历扫描文件路径下所有图片
windows下
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <vector>
#include <cstring>
#include <string>
#include <io.h>
using namespace std;
using namespace cv;
#define OPENING_PICS_NUM 51
#define ENDING_PICS_NUM 80
void changealpha(const char *inPath, const char *filename)
{
char tmp[100] = { 0 };
Mat mRead;
strcat(tmp, inPath);
strcat(tmp, filename);
//cout << "<<<<" << tmp << endl;
mRead = imread(tmp, CV_LOAD_IMAGE_UNCHANGED);
if (mRead.empty() != true) {
cout << mRead.size().height * mRead.size().width * 4;
for (int i = 0; i < mRead.size().height * mRead.size().width * 4; i+=4) {
if (mRead.data[i + 3] == 0x00) {
mRead.data[i + 0] = 0x00;
mRead.data[i + 1] = 0x00;
mRead.data[i + 2] = 0x00;
}
}
}
memset(tmp, 0, sizeof(tmp));
strcat(tmp, inPath);
strcat(tmp, "out\\");
strcat(tmp, filename);
cout << ">>>>" << tmp << endl;
imwrite(tmp, mRead);
}
void changeImagePNG32To24(const char *inPath, const char *filename) {
char tmp[100] = { 0 };
Mat mRead;
Mat png24;
strcat(tmp, inPath);
strcat(tmp, filename);
//cout << "<<<<" << tmp << endl;
vector<int> params;
params.push_back(CV_IMWRITE_PNG_COMPRESSION);
params.push_back(9);
mRead = imread(tmp, CV_LOAD_IMAGE_UNCHANGED);
/*if (mRead.empty() != true) {
for (int i = 0; i < mRead.size().height * mRead.size().width * 4; i += 4) {
float alpha = mRead.data[i + 3] / 255.000;
if (mRead.data[i + 0] != 0x00) {
mRead.data[i + 0] *= alpha* mRead.data[i + 0];
mRead.data[i + 1] *= alpha * mRead.data[i + 1];
mRead.data[i + 2] *= alpha * mRead.data[i + 2];
}
}
}*/
cvtColor(mRead, png24, CV_RGBA2RGB);
memset(tmp, 0, sizeof(tmp));
strcat(tmp, inPath);
strcat(tmp, "png24\\");
strcat(tmp, filename);
cout << ">>>>" << tmp << endl;
imwrite(tmp, png24, params);
}
void listFiles(const char * dir)
{
intptr_t handle;
_finddata_t findData;
char *p;
char tmp[100] = { 0 };
strcat(tmp, dir);
strcat(tmp, "*.*");
cout << tmp << endl;
handle = _findfirst(tmp, &findData); // 查找目录中的第一个文件
if (handle == -1)
{
cout << "Failed to find first file!\n";
return;
}
do
{
if (findData.attrib == _A_SUBDIR
|| strcmp(findData.name, ".") == 0
|| strcmp(findData.name, "..") == 0) {
}
else {
//cout << findData.name<< endl;
p = strchr(findData.name, '.');
{
if (strcmp(p, ".db") == 0)
continue;
}
changeImagePNG32To24(dir, findData.name);
}
} while (_findnext(handle, &findData) == 0); // 查找目录中的下一个文件
cout << "Done!\n";
_findclose(handle); // 关闭搜索句柄
}
int main(int argc, char** argv)
{
while (1)
{
char dir[200];
cout << "Enter a directory : ";
cin.getline(dir, 200);
strcat(dir, "\\");
listFiles(dir);
}
return 0;
}
linux下
//
// Created by czh on 18-9-3.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <opencv2/opencv.hpp>
void changeImagePNG32To24(const char *inPath, const char *filename) {
char tmp[100] = { 0 };
cv::Mat mRead;
cv::Mat png24;
strcat(tmp, inPath);
strcat(tmp, "/");
strcat(tmp, filename);
std::cout << "<<<<" << tmp << std::endl;
std::vector<int> params;
params.push_back(CV_IMWRITE_PNG_COMPRESSION);
params.push_back(9);
mRead = cv::imread(tmp, CV_LOAD_IMAGE_UNCHANGED);
/*if (mRead.empty() != true) {
for (int i = 0; i < mRead.size().height * mRead.size().width * 4; i += 4) {
float alpha = (float)mRead.data[i + 3] / 255.0;
if (mRead.data[i + 0] != 0x00) {
}
mRead.data[i + 0] = alpha * mRead.data[i + 0];
mRead.data[i + 1] = alpha * mRead.data[i + 1];
mRead.data[i + 2] = alpha * mRead.data[i + 2];
}
}*/
cvtColor(mRead, png24, CV_RGBA2RGB);
char cmd[50];
memset(tmp, 0, sizeof(tmp));
strcat(tmp, inPath);
strcat(tmp, "png24/");
sprintf(cmd, "mkdir %s", tmp);
std::cout << "#1" << cmd << std::endl;
system(cmd);
strcat(tmp, filename);
std::cout << ">>>>" << tmp << std::endl;
imwrite(tmp, png24, params);
}
void listFile(char *filePath) {
std::cout << "listFile";
char name[100];
struct stat stbuf;
DIR *fd;
dirent *dp;
if (stat(filePath, &stbuf) == -1) {
fprintf(stderr, "file size: open %s failed\n", filePath);
return;
}
std::cout << "open\n";
if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
if ((fd = opendir(filePath)) == NULL) {
fprintf(stderr, "dirwalk: can't open %s\n", filePath);
return;
}
std::cout << "read\n";
while ((dp = readdir(fd)) != NULL) { //读目录记录项
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
continue; //跳过当前目录以及父目录
}
if (strlen(filePath) + strlen(dp->d_name) + 2 > sizeof(name)) {
fprintf(stderr, "dirwalk : name %s %s too long\n", filePath, dp->d_name);
} else {
sprintf(name, "%s%s", filePath, dp->d_name);
//std::cout << name << std::endl;
changeImagePNG32To24(filePath, dp->d_name);
}
}
} else {
std::cout << filePath << " is not directory, open failed\n";
}
}
int main(int argc, char *argv[]) {
if (argc == 1) {
listFile((char *) ".");//未加参数执行时,从当前目录开始遍历
} else {
while (--argc > 0) {
listFile(*++argv);
}
}
return 0;
}