Gardinal
Gardinal

Reputation: 112

How to do a non-linear perspective stretch with OpenCV?

I need to perform a kind of image stretching that will scale less at the top and more at the bottom. I don't what to lose parts of the image like warpPerspective. Is this kind of function available with OpenCV?

original image

desired result

Upvotes: 1

Views: 973

Answers (1)

Gardinal
Gardinal

Reputation: 112

I found a way using remap function and remapping each pixel by using an exponential function:

   cv::Mat mapX, mapY;
   mapX.create(m_nRemapHeight, m_nRemapWidth, CV_32FC1);
   mapY.create(m_nRemapHeight, m_nRemapWidth, CV_32FC1);

   for (int j = 0; j < m_nRemapHeight; j++)
   {
      for (int i = 0; i < m_nRemapWidth; i++)
      {
         mapX.at<float>(j, i) = (float)i;
         float t = (m_nRemapHeight * 0.5f);    
         float r = j + t;
         float fVal = ((r*r) / (m_nRemapHeight + t * 2));
         fVal -= ((t*t) / (m_nRemapHeight + t * 2));
         mapY.at<float>(j, i) = fVal;
      }
   }
   cv::remap(srcMat, dstMat, matX, matY, CV_INTER_LINEAR, 0);

Upvotes: 3

Related Questions