Why identical images have different pixels value
I can't understand why Iplimage received from video and from file (received from this video) contain different pixels value. This code show it.
CvCapture* readerAvi = cvCreateFileCapture( "input.avi" );
// Grad first frame from input video stream
if(!cvGrabFrame(readerAvi)) {
std::cerr << "Could not grab AVI frame." << std::endl;
return 0;
}
IplImage* videoFrame = cvRe开发者_运维技巧trieveFrame(readerAvi);
if( !videoFrame )
break;
// Save values of some pixels in file
std::ofstream fout("frame_pixels.txt");
for(int y = 170; y < 270; ++y) {
for(int x = 30; x < 130; ++x) {
CvScalar s = cvGet2D(videoFrame, y, x );
fout << "x=" << x << " y=" << y << " " << s.val[0] << " " << s.val[1] << " " << s.val[2] << std::endl;
}
fout << "\n";
}
fout.close();
cvSaveImage("temp.jpg", videoFrame);
// Load saved image
videoFrame = cvLoadImage("temp.jpg");
fout.open("image_pixels.txt");
// Save values of some pixels in file
for(int y = 170; y < 270; ++y) {
for(int x = 30; x < 130; ++x) {
CvScalar s = cvGet2D(videoFrame, y, x );
fout << "x=" << x << " y=" << y << " " << s.val[0] << " " << s.val[1] << " " << s.val[2] << std::endl;
}
fout << "\n";
}
fout.close();
So why files frame_pixels.txt and image_pixels.txt contain near numbers but not the same?
I can't say for sure without seeing the data itself, but it's very likely to be down to compression undergone by the image. If the video came from a set of images, then each of those images will have gone through a second compression when the video was created - e.g. JPEG compression in the case of MPEG files (more details here). This will cause artefacts on the image. If the image is a still frame taken from the video, when it was saved, it may well have undergone compression.
Whilst the video frame and the still image may look comparable by eye, there's almost certainly going to be slight differences as a result of the way images are compressed.
I would guess lossy compression is the answer as JPEG is a lossy file format. Perhaps you could try capturing in a lossless format?
When you save the frame into "temp.jpg", the JPEG lossy compression algorithm reencodes the frame. The pixel values will be slightly altered by the lossy algorithm.
That's why when you read the file "temp.jpg" back some of the pixel values differ from the original pixel values that you obtained from the frame in the avi file.
OpenCv uses the ffmpeg library for the video decoding and libjpeg for the image decoding, probably implementations details contribute in making the result different.
精彩评论