#include <fstream>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
//#include "opencv2/ximgproc.hpp"
#include <opencv2/imgproc.hpp>
//#include "opencv2/xphoto.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc/types_c.h>
int main(int argc, char* argv[])
{
// Read main folder address
std::vector<std::string> folderAddr;
folderAddr.push_back(argv[1]);
// Read all folder names
std::vector<cv::String> folderNames;
DIR* dir = opendir(folderAddr[0].c_str());
struct dirent* entry = readdir(dir);
while (entry != NULL) {
if (entry->d_type == DT_DIR) {
folderNames.push_back(entry->d_name);
}
entry = readdir(dir);
}
closedir(dir);
sort(folderNames.begin(), folderNames.end());
int num_folders = folderNames.size();
for (int folderId = 2; folderId < num_folders; folderId++) // index starts from 2 to ignore '.' and '..'
{
// Read image names
std::vector<cv::String> imgNames;
// folderAddr[0] + folderId
cv::String subfolderAddr = folderAddr[0] + "/" + folderNames[folderId];
std::cout << subfolderAddr << std::endl;
DIR* dir = opendir(subfolderAddr.c_str());
struct dirent* entry = readdir(dir);
while (entry != NULL) {
if (entry->d_type != DT_DIR) {
imgNames.push_back(entry->d_name);
}
entry = readdir(dir);
}
closedir(dir);
sort(imgNames.begin(), imgNames.end());
int num_imgs = imgNames.size();
printf("%d\n", num_imgs);
// Luminance buffer
//int width;
//int height;
//Buffer<uint8_t> imgs; // luma buffer
//Buffer<uint8_t> imgsUV; // Chroma buffer
//unsigned char* buffer[num_imgs];
//YUVInfo yuvInfo[num_imgs];
// int width = 4608;
// int height = 3456;
// int offset = 0;
//int width = 3264;
//int height = 2448;
//int offset = 3264*48;
//int offset = 0;
//int width = 4160;
//int height = 3120;
//int offset = 4160*16;
// int width = 4000;
// int height = 3000;
int width = 4032;
int height = 3000;
if (argv[2] != NULL)
width = atoi(argv[2]);
if (argv[3] != NULL)
height = atoi(argv[3]);
int offset = 0;//4032*8;
unsigned char* buffer;
for (int imgId = 0; imgId < num_imgs; imgId++)
{
cv::String tmpdir = subfolderAddr + "/" + imgNames.at(imgId); // First image as the reference
std::cout << tmpdir << std::endl;
int yLen = width * height;
int uvLen = width * height / 2;
FILE* pFile = fopen(tmpdir.c_str(), "rb");
if (pFile == NULL) { fputs("File error", stderr); exit(1); }
// allocate memory to contain the whole file:
buffer = (unsigned char*)malloc(yLen + uvLen + offset);
if (buffer == NULL) { fputs("Memory error", stderr); exit(2); }
// copy the file into the buffer:
long len = yLen + uvLen + offset;
size_t result = fread(buffer, sizeof(char), len, pFile);
std::cout << "result" << result << " len" << len << std::endl;
if (result != len) { fputs("Reading error", stderr); exit(3); }
// Save image
// Y
cv::Mat yuv[3];
yuv[0] = cv::Mat::zeros(cv::Size(width, height), CV_8UC1);
yuv[1] = cv::Mat::zeros(cv::Size(width, height), CV_8UC1);
yuv[2] = cv::Mat::zeros(cv::Size(width, height), CV_8UC1);
memcpy(yuv[0].data, buffer, height * width);
// UV
uint8_t* dataUV = buffer + width * height + offset;
cv::Mat U2 = cv::Mat::zeros(cv::Size(width / 2, height / 2), CV_8UC1);
cv::Mat V2 = cv::Mat::zeros(cv::Size(width / 2, height / 2), CV_8UC1);
unsigned char* pU = (unsigned char*)V2.data;
unsigned char* pV = (unsigned char*)U2.data;
for (int i = 0; i < height * width / 4; i++) {
*pU++ = *dataUV++;
*pV++ = *dataUV++;
}
cv::resize(U2, yuv[1], cv::Size(width, height)); // up-sampling by a factor of 2
cv::resize(V2, yuv[2], cv::Size(width, height)); // up-sampling by a factor of 2
// merge
std::vector<cv::Mat> channels;
channels.push_back(yuv[0]);
channels.push_back(yuv[1]);
channels.push_back(yuv[2]);
// channels.push_back(yuv[2]);
// channels.push_back(yuv[1]);
cv::Mat YCbCr;
merge(channels, YCbCr);
cv::Mat BGR;
cvtColor(YCbCr, BGR, CV_YCrCb2BGR);
// cvtColor(YCbCr, BGR, CV_YCrCb2RGB);
//cv::Rect rectCrop(0, 0, width, 3024);//!!!!!!!
//cv::Mat croppedImage(BGR, rectCrop);//!!!!!!!
//cv::String outputName = "/home/tcl980/snli/ImageProcessing/JingWeiNV21/png/" + folderNames[folderId] +"/"+ std::to_string(imgId) + ".png";
//cv::String outputName = "/home/tcl980/snli/ImageProcessing/JingWeiNV21/png/" + folderNames[folderId] +"/"+ imgNames.at(imgId) + ".png";~/snli/ImageProcessing/NBTestNV21B
//cv::String outputName = "/home/tcl980/snli/ImageProcessing/NBTestNV21C/png/" + folderNames[folderId] +"/"+ imgNames.at(imgId) + ".png";
//flip BGR
//cv::flip(BGR, BGR, -1);
cv::String outputName = subfolderAddr + "/png/" + imgNames.at(imgId) + ".png";
std::cout << outputName << std::endl;
cv::imwrite(outputName, BGR);
free(buffer);
}
}
std::cout << "Completed." << std::endl;
return 0;
}
配置
-
dirent.h
windows没有这个, 需要去下载, 然后放到vs的include目录
官网 -
opencv
-
cmd
.\yuvTojpg.exe \SuperResolution\sample 4096 3072 -
sample
01{ *.yuv png_folder}