我正在尝试使用OpenCV VideoCapture类从特定文件夹中顺序读取图像,如下面的代码所示,该代码是从https://www.kevinhughes.ca/tutorials/reading-image-sequences-with-opencv中提取的。读取工作正常,我可以正确查看已读取图像(imgSrc)的视频流。当我尝试使用嵌套的for循环将每个帧复制到新的Mat对象时,就会发生问题,新图像(imgDst)与原始图像不同。我在下面附上了框架的结果。我在做错什么事情了,所以我得到了奇怪的结果吗?
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void help(char** argv)
{
cout << "\nThis program gets you started reading a sequence of images using cv::VideoCapture.\n"
<< "Image sequences are a common way to distribute video data sets for computer vision.\n"
<< "Usage: " << argv[0] << " <path to the first image in the sequence>\n"
<< "example: " << argv[0] << " right%%02d.jpg\n"
<< "q,Q,esc -- quit\n"
<< "\tThis is a starter sample, to get you up and going in a copy paste fashion\n"
<< endl;
}
int main(int argc, char** argv)
{
if(argc != 2)
{
help(argv);
return 1;
}
string arg = argv[1];
VideoCapture sequence(arg);
if (!sequence.isOpened())
{
cerr << "Failed to open Image Sequence!\n" << endl;
return 1;
}
Mat imgSrc; // source image
for(;;)
{
sequence >> imgSrc;
if(imgSrc.empty())
{
cout << "End of Sequence" << endl;
break;
}
Mat imgDst = cv::Mat::zeros(cv::Size(imgSrc.size().width,imgSrc.size().height),CV_16U);
// Copying the elements of imgSrc to imgDst
uint16_t* imgSrcPtr;
uint16_t* imgDstPtr;
for(int i = 0; i < imgSrc.rows; i++)
{
imgDstPtr = imgDst.ptr<uint16_t>(i);
imgSrcPtr = imgSrc.ptr<uint16_t>(i);
for(int j= 0; j < imgSrc.cols; j++)
imgDstPtr[j] = imgSrcPtr[j];
}
namedWindow("Source image ",WINDOW_AUTOSIZE );
namedWindow("Destination image ",WINDOW_AUTOSIZE );
imshow("Destination image ", imgDst);
waitKey(0;
return 0;
}
[使用image.type(),我发现VideoCapture类将灰度读取帧CV_16U转换为CV_8UC3(RGB24)格式,因此,将颜色空间简单转换为灰度可以解决问题。