简体   繁体   English

OpenCV-remap()-获得黑色像素

[英]OpenCV - remap() - getting black pixels

I'm using an STMap to map a .jpg image using remap() . 我正在使用STMap使用remap() .jpg图像。

I loaded my STMap, split the channels and converted each channel matrix to CV_32FC1 . 我加载了STMap,拆分了通道,然后将每个通道矩阵转换为CV_32FC1 I checked them and it worked - each matrix displays correctly and all of its values are between 0.0 and 1.0. 我检查了一下,它起作用了-每个矩阵正确显示,并且所有值都在0.0到1.0之间。

However, when i try to use the remap() function: 但是,当我尝试使用remap()函数时:

Mat dst;
remap(image4, dst,map_x,map_y,INTER_LINEAR,BORDER_CONSTANT,Scalar(0,0,0));
imshow( "Result", dst );

It just displays a black image. 它只是显示黑色图像。

  1. image4 = my .jpg image image4 =我的.jpg图片
  2. map_x = grayscale CV_32FC1 (red channel of the original STMap) map_x =灰度CV_32FC1(原始STMap的红色通道)
  3. map_y = grayscale CV_32FC1 (green channel of the original STMap) map_y =灰度CV_32FC1(原始STMap的绿色通道)

What could be the problem? 可能是什么问题呢?

Thanks! 谢谢!

Black image when using cv::remap is due to using offsets instead of absolute locations in the passed map(s). 使用cv::remap时出现黑色图像是由于使用了偏移而不是传递的地图中的绝对位置。

Optical flow algorithms usually export motion vectors, not absolute positions, whereas cv::remap expects the absolute coordinate (subpixel) to sample from. 光流算法通常输出运动矢量,而不是绝对位置,而cv::remap期望从中采样绝对坐标(子像素)。

To convert between the two, starting with a CV_32FC2 flow matrix we can do something like this: 为了在两者之间转换,从CV_32FC2流矩阵开始,我们可以执行以下操作:

// Convert from offsets to absolute locations.
Mat mapx(flow.size(), CV_32FC1);
Mat mapy(flow.size(), CV_32FC1);
for (int row = 0; row < flow.rows; row++)
{
  for (int col = 0; col < flow.cols; col++)
  {
    Point2f f = flow.at<Point2f>(row, col);
    mapx.at<float>(row, col) = col + f.x;
    mapy.at<float>(row, col) = row + f.y;
  }
}

Then mapx and mapy can be used in remap . 然后可以在remap使用mapxmapy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM