mercredi 30 octobre 2019

How to display copies of images captured by OpenCV VideoCapture correctly

I am trying to use the OpenCV VideoCapture class to read images sequentially from a specific folder, as shown in the code below, which is extracted from https://www.kevinhughes.ca/tutorials/reading-image-sequences-with-opencv. The reading works just fine and I can view the video streaming of the read images (imgSrc) correctly. The problem happens when I try to copy each frame into a new Mat object using nested for loops, the new image (imgDst) is different from the original one. I attached the results of the frame below ? Is there anything I am doing wrong so that I get this weird result?

#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;
}

Source image Destination image

Aucun commentaire:

Enregistrer un commentaire