I am looking for WIN32 program to copy part of the large 1920x1080px 4:2:0 .YUV file (cca. 43GB) into smaller .YUV files. All of the programs I have used, i.e. YUV players, can only copy/save 1 frame at the time. What is the easiest/appropriate method to cut YUV raw data to smaller YUV videos(images)? SOmething similar to ffmpeg command:
ffmpeg -ss [start_seconds] -t [duration_seconds] -i [input_file] [outputfile]
Here is the Minimum Working Example of the code, written in C++, if anyone will search for a simple solution:
// include libraries
#include <fstream>
using namespace std;
#define P420 1.5
const int IMAGE_SIZE = 1920*1080; // ful HD image size in pixels
const double IMAGE_CONVERTION = P420;
int n_frames = 300; // set number of frames to copy
int skip_frames = 500; // set number of frames to skip from the begining of the input file
char in_string[] = "F:\\BigBucksBunny\\yuv\\BigBuckBunny_1920_1080_24fps.yuv";
char out_string[] = "out.yuv";
//////////////////////
// main
//////////////////////
int main(int argc, char** argv)
{
double image_size = IMAGE_SIZE * IMAGE_CONVERTION;
long file_size = 0;
// IO files
ofstream out_file(out_string, ios::out | ios::binary);
ifstream in_file(in_string, ios::in | ios::binary);
// error cheking, like check n_frames+skip_frames overflow
//
// TODO
// image buffer
char* image = new char[(int)image_size];
// skip frames
in_file.seekg(skip_frames*image_size);
// read/write image buffer one by one
for(int i = 0; i < n_frames; i++)
{
in_file.read(image, image_size);
out_file.write(image, image_size);
}
// close the files
out_file.close();
in_file.close();
printf("Copy finished ...");
return 0;
}
If you have python available, you can use this approach to store each frame as a separate file:
src_yuv = open(self.filename, 'rb')
for i in xrange(NUMBER_OF_FRAMES):
data = src_yuv.read(NUMBER_OF_BYTES)
fname = "frame" + "%d" % i + ".yuv"
dst_yuv = open(fname, 'wb')
dst_yuv.write(data)
sys.stdout.write('.')
sys.stdout.flush()
dst_yuv.close()
src_yuv.close()
just change the capitalized variable into valid numbers, e.g
NUMBER_OF_BYTES for one frame 1080p should be 1920*1080*3/2=3110400
Or if you install cygwin you can use the dd tool, e.g. to get the first frame of a 1080p clip do:
dd bs=3110400 count=1 if=sample.yuv of=frame1.yuv
Method1:
If you are using gstreamer and you just want first X amount of yuv frames from large yuv files then you can use below method
gst-launch-1.0 filesrc num-buffers=X location="Your_large.yuv" ! videoparse width=x height=y format="xy" ! filesink location="FirstXframes.yuv"
Method2:
Calculate size of 1 frames and then use split utility to divide large files in small files.
Use
split -b size_in_bytes Large_file prefix
Related
So I created gif file from 5 png files with using MagickCoalesceImages call and store it on disk.
How I can read these files back from gif file ?
MagickReadImage does not help
Hard to help without seeing the code, but I can assume you created 5 images with something like...
MagickWand
* gif2png;
gif2png = NewMagickWand();
MagickReadImage(gif2png, "input.gif");
MagickWriteImages(gif2png, "output_%02d.png", MagickFalse);
gif2png = DestroyMagickWand(gif2png);
How I can read these files back from gif file?
You would use MagickReadImage to decode the image from the file, and MagickAddImage to append the decoded image onto a image-stack.
MagickWand
* png2gif,
* temp;
// Create a blank image-stack.
png2gif = NewMagickWand();
char filename[PATH_MAX]; // PATH_MAX provided by limits.h
// Iterate over images to append.
for (int i = 0; i < 5; ++i) {
sprintf(filename, "output_%02d.png", i);
// Read image from disk.
temp = NewMagickWand();
MagickReadImage(temp, filename);
// Add "frame" to stack.
MagickAddImage(png2gif, temp);
temp = DestroyMagickWand(temp);
}
MagickWriteImages(png2gif, "output.gif", MagickTrue);
png2gif = DestroyMagickWand(png2gif);
Warning: The above example omits basic error handling, and assumes the filename names are a sequential series.
Update
From the comments, if you wish to extract a single frame as a PNG file, there are a few ways.
Fastest way is to use MagickWriteImages
MagickWriteImages(img, "output_%02d.png", MagickFalse);
Or use the image stack iterators.
for (MagickSetFirstIterator(img); MagickHasNextImage(img); MagickNextImage(img)) {
MagickWriteImage(img, "output_%02d.png");
}
Or, if the PNG filenames are defined, and you need to map them.
const char * filenames[5] = {
"first.png",
"second.png",
"third.png",
"forth.png",
"fifth.png"
};
for (int i = 0; i < 5; ++i) {
MagickSetIteratorIndex(img, i);
MagickWriteImage(img, filenames[i]);
}
Without seeing the code, we can't offer much help, and can only guess what an acceptable solution would be.
I have the following code:
MagickWand *wand = NewMagickWand();
char* cmdargs[] = {
"compare",
"receipt-expected.png",
"-metric",
"psnr",
"difference.png",
"difference2.png",
NULL
};
int argcount = 6;
// Allocate memory for MagickCommand
ImageInfo * info = AcquireImageInfo();
ExceptionInfo* e = AcquireExceptionInfo();
// Execute command
char *metadata = NULL;
MagickBooleanType status = MagickCommandGenesis(info, CompareImageCommand, argcount, cmdargs, &metadata, e);
status is 0, which I assume it working because it has no error and the command works correctly in the CLI.
How do I get the metric it has produced? meta is NULL.
$ compare receipt-expected.png -metric psnr difference.png difference2.png
15.4169
Ideally you would access the API directly, rather than attempting to call a new ImageMagick process as a subprocess.
MagickWand * alpha, * beta, * result;
// ... Allocated & Init `alpha' & `beta'
double metric;
result = MagickCompareImages(alpha,
beta,
PeakSignalToNoiseRatioMetric,
&metric);
printf("psnr = %f\n", metric);
How do I get the metric it has produced?
You can not, as metadata is intended to hold additional IO information in a heap. In this instance, any information written to char ** metadata will be destroyed immediately after the internal command writes to standard output. See here for reference.
I noticed, that in the following program the created netcdf file is corrupted, i.e., executing ncdump -h out.nc produces errors.
#include <netcdf>
/**
* This file produces a corrupted nc output file.
* Compile with `g++ -std=c++11 -o test test.cpp -lnetcdf -lnetcdf_c++4
*/
// this is the first non-working
// chunk size. It does work with 1048576
// 1048576 is representable by exactly 20 bits.
#define CHUNK_SIZE 1048577
using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;
int main()
{
typedef std::vector<size_t> vs;
typedef std::vector<netCDF::NcDim> vd;
try
{
NcFile outFile = NcFile("out.nc", NcFile::replace);
// create the dimensions complying to the AMBER specs
NcDim frameDim = outFile.addDim("frame");
NcDim atomDim = outFile.addDim("atom");
NcDim spatialDim = outFile.addDim("spatial", 3);
NcDim radiusDim = outFile.addDim("radius",1);
// create the variables
NcVar coords = outFile.addVar("coordinates", ncFloat, vd({frameDim, atomDim, spatialDim}));
NcVar radii = outFile.addVar("radius", ncFloat, vd({frameDim, atomDim}));
// set up chunking
vs chunk_coords({1, CHUNK_SIZE, 3});
vs chunk_radii({1, CHUNK_SIZE, 1});
coords.setChunking(NcVar::nc_CHUNKED, chunk_coords);
radii.setChunking(NcVar::nc_CHUNKED, chunk_radii);
// set up compression
coords.setCompression(false, true, 1);
radii.setCompression(false, true, 1);
return 0;
}
catch(NcException& e)
{
return -1;
}
}
The out.nc becomes a valid and working netcdf file when ...
... the CHUNK_SIZE becomes less than 1048577
... CHUNKING is disabled
... the unused "radius" dimension is named differently or not added at all
Note, that the maximum number of working CHUNK_SIZE, 1048576 is the maximal integer number representable by 20 bits.
What causes this behaviour? It is easy to work around by renaming the radius dimension, but I am still curious in why this is in any way related to the chunking of HDF5/netcdf.
I am taking a computer graphics class, and I need to work with textures, but I can't use any library to do it. I am stuck on loading the rgb values of the images I need to use (the images can be in any format, jpg, raw, png, etc..) so my question is, which is the easiest way to get the rgb values of an image (of any format) without using any libraries to get this values?? Here is what I found already on the site:
unsigned char *data;
File *file;
file = fopen("image.png", "r");//
data = (unsigned char *)malloc(TH*TV*3); //TH and TV are both 50
fread(data, TH*TV*3, 1, file);
fclose(file);
int i;
for(i=0;i<TH*TV*3;i++){
//suposing I have a struct RGB for the rgb values
RGB.r = data[?];// how do I get the r value
RGB.g = data[?];// how do I get the g value
RGB.b = data[?];// how do I get the b value
}
Thanks
Rather than iterating through every byte that you read in, you want to iterate every pixel which consists of 3 bytes. So replace i++ with i+=3.
for(i=0;i<TH*TV*3;i+=3){
RGB.r = data[i];
RGB.g = data[i+1];
RGB.b = data[i+2];
}
Try to use some framework like OpenCV there are several options to get the colors or to manipulate an image.
Here I found this example code:
cv::Mat img = cv::imread("lenna.png");
for(int i=0; i<img.rows; i++) {
for(int j=0; j<img.cols; j++) {
// You can now access the pixel value with cv::Vec3b
std::cout << img.at<cv::Vec3b>(i,j)[0] << " ";
str::cout << img.at<cv::Vec3b>(i,j)[1] << " ";
str::cout << img.at<cv::Vec3b>(i,j)[2] << std::endl;
}
}
But please note that the code above is not very performance, but the code above should give you an idea how to read the pixels.
Presently Iam working in finding disparity of stereo pair. I have got a situation in creating 20 channel data set, When I declare array of 3 dimension it was giving error, Instead can I create image of 20 channels so that I can store data. If I can what are the additional conditions I have to include to get results without any error of memory allocation or sort of .... Creating an Image of 20 channels will be even comfortable for me...
The C++ interface of OpenCV presents cv::Mat, which replaces and improves the IplImage type of the C interface. This new type provides several constructors, including the one below which can be used to specify the desired number of channels through the param type:
Mat::Mat(int rows, int cols, int type)
Sample code:
#include <cv.h>
#include <highgui.h>
#include <iostream>
void test_mat(cv::Mat mat)
{
std::cout << "Channels: " << mat.channels() << std::endl;
}
int main(int argc, char* argv[])
{
cv::Mat mat20(1024, 768, CV_8UC(20));
test_mat(mat20);
return 0;
}
Opencv implements template class for small matrices whose type and size are known at compilation time:
template<typename _Tp, int m, int n> class Matx {...};
You can create a specified template of a partial case of Matx, which is cv::Vec like those already written in opencv for 1,2, or 3 "channels" like that:
typedef Vec<uchar, 3> Vec3b; // 3 channel -- written in opencv
typedef Vec<uchar, 20> Vec20b; // the one you need
And then, declare a Matrix of your new (20 channel of uchar) object:
cv::Mat_<Vec20b> myMat;
myMat.at<Vec20b>(i,j)(10) = .. // access to the 10 channel of pixel (i,j)