27 #ifndef improc_imagePads_hpp
28 #define improc_imagePads_hpp
32 #include "../mxError.hpp"
56 template<
typename imOutT,
typename imInT>
60 typename imOutT::Scalar value
63 int nRows = imIn.rows() + 2*padSz;
64 int nCols = imIn.cols() + 2*padSz;
66 imOut = imOutT::Constant(nRows, nCols, value);
68 imOut.block(padSz, padSz, imIn.rows(),imIn.cols()) = imIn;
82 template<
typename imOutT,
typename imInT>
86 typename imOutT::Scalar value
89 int nRows = imIn.rows() + 2*padSz;
90 int nCols = imIn.cols() + 2*padSz;
93 imOut = imOutT::Constant(nRows, nCols, value);
95 imOut.block(padSz, padSz, imIn.rows(),imIn.cols()) = imIn;
110 template<
typename imOutT,
typename imInT>
116 int dim1 = imIn.rows();
117 int dim2 = imIn.cols();
119 imOut.resize(dim1+2*padSz, dim2 + 2*padSz);
121 imOut.block(padSz, padSz, dim1, dim2) = imIn;
124 for(
int i = 0; i< padSz; ++i)
126 for(
int j=0; j < imOut.cols(); ++j)
130 imOut(i,j) = imIn(0, 0);
131 imOut( imOut.rows()-1-i, j) = imIn( dim1-1, 0);
133 else if( j >= dim2 + padSz)
135 imOut(i,j) = imIn(0, dim2-1);
136 imOut( imOut.rows()-1-i, j) = imIn( dim1-1, dim2-1);
140 imOut( i, j ) = imOut( padSz, j );
141 imOut( imOut.rows() - padSz + i, j ) = imOut( padSz + dim1-1, j );
147 for(
int i=padSz; i < dim1+padSz; ++i)
149 for(
int j=0; j< padSz; ++j)
151 imOut(i,j) = imOut(i, padSz);
152 imOut(i, imOut.cols()-1-j) = imOut(i, imOut.cols()-1-padSz);
173 template<
typename imOutT,
typename imInT>
181 typedef imInT imMaskT;
183 int dim1 = imMask.rows();
184 int dim2 = imMask.cols();
186 imOut.resize(dim1, dim2);
188 int stR = 0.5*(dim1 - imIn.rows());
189 int stC = 0.5*(dim2 - imIn.cols());
194 mxError(
"padImage", MXE_INVALIDARG,
"imMask must have at least as many rows as imIn.");
199 mxError(
"padImage", MXE_INVALIDARG,
"imMask must have at least as many columns as imIn.");
203 imOut.block(stR, stC, imIn.rows(), imIn.cols()) = imIn;
207 imMaskT tMask = imMask;
209 std::vector<int> nmi, nmj;
211 for(
unsigned int n=0; n < padSz; ++n)
215 for(
int ii =0; ii< dim1; ++ii)
217 for(
int jj =0; jj<dim2; ++jj)
219 if(tMask(ii,jj) == 1)
continue;
221 typename imOutT::Scalar mv = 0;
226 for(
int i=-1; i<=1; ++i)
228 for(
int j=-1; j<=1; ++j)
230 if(i==0 && j==0)
continue;
231 if( ii + i < 0 || ii + i >= dim1)
continue;
232 if( jj + j < 0 || jj + j >= dim2)
continue;
234 if( tMask( ii+i, jj+j) == 1)
236 mv += imOut( ii+i, jj+j);
246 imOut(ii, jj) = mv/nv;
253 for(
size_t i=0; i< nmi.size(); ++i)
255 tMask( nmi[i], nmj[i]) = 1;
272 template<
typename imOutT,
typename imInT>
278 if(2*padSz > imIn.rows())
280 mxError(
"cutPaddedImage", MXE_INVALIDARG,
"padSz too large, must be < imIn.rows()/2");
284 if(2*padSz > imIn.cols())
286 mxError(
"cutPaddedImage", MXE_INVALIDARG,
"padSz too large, must be < imIn.cols()/2");
290 int nRows = imIn.rows() - 2*padSz;
291 int nCols = imIn.cols() - 2*padSz;
293 imOut = imIn.block(padSz, padSz, nRows, nCols);
int padImageRef(imOutT imOut, imInT imIn, unsigned int padSz, typename imOutT::Scalar value)
Pad an image with a constant value for reference types.
int cutPaddedImage(imOutT &imOut, const imInT &imIn, unsigned int padSz)
Cut down a padded image.
int padImage(imOutT &imOut, imInT &imIn, imInT &imMask, unsigned int padSz)
Pad an image by repeating the values at the edge of a 1/0 mask.