opencv remap function:如何将像素值设置为(0,0,0)

时间:2014-04-02 14:10:32

标签: opencv

函数调用是:

remap( src, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0,0, 0) );

map_x and map_y defines as below:

for( int j = 0; j < src.rows; j++ )
{  for( int i = 0; i < src.cols; i++ ){

      if( i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75 )
      {
              map_x.at<float>(j,i) = 2*( i - src.cols*0.25 ) + 0.5 ;
              map_y.at<float>(j,i) = 2*( j - src.rows*0.25 ) + 0.5 ;
      }
      else{
              map_x.at<float>(j,i) = 0 ;
              map_y.at<float>(j,i) = 0 ;
       }
   }
}

Original Image:

Result Image:

您可以看到原始图像和结果图像。我想问一下: 如何将结果image(green part)的外部部分设置为black(r=0,g=0,b=0)

1 个答案:

答案 0 :(得分:0)

我认为您将边框上的所有像素映射到位置(0,0)处的原始像素。尝试替换以下

else {
    map_x.at<float>(j,i) = 0 ;
    map_y.at<float>(j,i) = 0 ;
}

由此

else {
    map_x.at<float>(j,i) = -1 ;
    map_y.at<float>(j,i) = -1 ;
}