Search code examples
c++opencvpad

Take off pads with OpenCV


I have to translate from Matlab to C this code:

   % take off the pads
   x = (1 + padSize) : (rows - pad8Size);
   y = (1 + padSize) : (cols - padSize);
   rpad=rpad(x,y);

1st and 2nd create 2 array, but I don t know how I have to delete it from rpad Mat object It can be something like(subtract every element)

    for(int i=1+pad;i<=rows-pad;i++){
     for(int j=1+pad;i<=cols-pad;j++){
            subtract(rpad,x,rpad);
           subtract(rpad,y,rpad);}}

Or something like(delete the external element)

      int a=(rows-pad)-(1+pad);
      int b=(cols-pad)-(1+pad);
      rpad.create(img.rows - a,img.cols - b,original.type());
      img.copyTo(rpad);

Solution

  • Try

    cv::Rect roi(padSize, padSize, rpad.cols-2*padSize, rpad.rows-2*padSize);
    cv::Mat result = rpad(roi);
    

    And depending on whether you want continuous memory, you can choose to directly use result (discontinuous, usually okay for most OpenCV functions) or copy it to back to rpad (continuous)