Skip to content

Instantly share code, notes, and snippets.

@t-abe
Created May 6, 2011 09:50
Show Gist options
  • Save t-abe/958700 to your computer and use it in GitHub Desktop.
Save t-abe/958700 to your computer and use it in GitHub Desktop.
#include <useopencv.h>
template <typename T>
class BoxMat {
public:
const cv::Mat_<T>& mat;
int offsetX, offsetY;
int rows, cols;
BoxMat(const cv::Mat_<T>& mat, const int offsetY, const int offsetX, const cv::Size size)
: mat(mat), offsetY(offsetY), offsetX(offsetX) , rows(size.height), cols(size.width)
{};
T operator() (const int y, const int x) const {
assert(y < rows && x < cols);
return
(offsetX + x >= mat.cols || offsetY + y >= mat.rows) ?
T() : mat(offsetY + y, offsetX + x);
}
};
template <typename SrcType, typename DstType, class Func>
void box_filter(const cv::Mat_<SrcType>& src, cv::Mat_<DstType>& dst, const cv::Size& size, Func& f){
assert(src.cols == dst.cols && src.rows == dst.rows);
for(int y=0; y < src.rows; ++y){
for(int x=0; x < src.cols; ++x){
dst(y, x) = f( BoxMat<SrcType>(src, y, x, size) );
}
}
}
int main()
{
cv::Mat_<uchar> lena = cv::imread("c:/lena.bmp", 0);
cv::Mat_<float> dlena(lena.size());
box_filter(lena, dlena, cv::Size(2, 1), [](const BoxMat<uchar>& m)->float {
return std::abs(m(0, 0) - m(0, 1)) / 255.0f;
});
cv::imshow("src", lena);
cv::imshow("dst", dlena);
cv::waitKey();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment