0

我是这个网站的新手,这是我的第一个查询...我需要在 c++ 或 matlab 中实现简单的滑动窗口算法,请在这方面帮助我谢谢

4

3 回答 3

8

假设您需要一个用于图像处理的通用滑动窗口,在 Matlab 中您可以执行以下操作:

image = imread('image.png');
imageWidth = size(image, 2);
imageHeight = size(image, 1);

windowWidth = 32;
windowHeight = 32;

for j = 1:imageHeight - windowHeight + 1
    for i = 1:imageWidth - windowWidth + 1
        window = image(j:j + windowHeight - 1, i:i + windowWidth - 1, :);
        % do stuff with subimage
    end
end
于 2010-05-30T10:58:20.173 回答
4

If the function is a simple linear combination of pixel values in the neighborhood, such as an average, you can use CONV2 to make the convolution. There are also specialized functions, such as MEDFILT2 if you want to take the median of each sliding window.

If the function you want to apply to each neighborhood is more complex, you have two options:

  1. If you have enough memory, you can transform your image into a large array such that every column corresponds to one sliding window using IM2COL. Then you apply your function to every column and reshape.

  2. If you don't have that much memory, use NLFILTER to apply the function to each sliding window.

In any case, you may want to have a look at PADARRAY to pad your image before you run the convolution to avoid shrinking your image while reducing border effects.

于 2010-05-30T13:18:32.103 回答
2

对于 C++ 来说这样的事情怎么样,顺便说一句,下面的代码是为 OpenCV 编写的

vector<Rect> get_sliding_windows(Mat& image,int winWidth,int winHeight)
{
  vector<Rect> rects;
  int step = 16;
  for(int i=0;i<image.rows;i+=step)
  {
      if((i+winHeight)>image.rows){break;}
      for(int j=0;j< image.cols;j+=step)    
      {
          if((j+winWidth)>image.cols){break;}
          Rect rect(j,i,winWidth,winHeight);
          rects.push_back(rect);
      }
  } 
  return rects;
}
于 2012-11-20T03:10:23.373 回答