功能:利用opencv实现图像滑动窗口操作(即利用已知尺寸的窗口遍历整幅图像,形成许多子图像)
vs2013+opencv3.1
2016.10

函数实现:

slidingWnd.h

#ifndef SLIDINGWND_H_
#define SLIDINGWND_H_
//简单的滑动窗口的形成
#include<iostream>
#include<opencv2\opencv.hpp>
using namespace std;
using namespace cv;
//基于矩形窗口的图像滑动窗口操作,返回值为滑动窗口的数目
//@src 输入图像
//@wnd 输出结果
//@wndSize 滑动窗口的大小
//@ x_percent 滑动窗口在x方向步长的百分比,x_step=x_percent*wndSize.width
//@ y_percent 滑动窗口在y方向步长的百分比,y_step=y_percent*wndSize.height
int slidingWnd(Mat& src, vector<Mat>& wnd, Size& wndSize, double x_percent, double y_percent)
{
    int count = 0;  //记录滑动窗口的数目
    int x_step = cvCeil(x_percent*wndSize.width);
    int y_step = cvCeil(y_percent*wndSize.height);
    /*String wndName = "F:\\wnd\\";
    char temp[1000];*/
    int64 count1 = getTickCount();
    double freq = getTickFrequency();
    //利用窗口对图像进行遍历
    for (int i = 0; i < src.cols- wndSize.width; i+=y_step)
    {
        for (int j = 0; j < src.rows- wndSize.height; j+=x_step)
        {
            Rect roi(Point(j, i), wndSize);
            Mat ROI = src(roi);
            wnd.push_back(ROI);
            count++;
        }
    }
    int64 count2 = getTickCount();
    double time = (count2 - count1) / freq;
    cout << "Time=" << time * 100 << "ms"<<endl;
    cout << count << endl;
    return count;
}

#endif // !SLIDINGWND_H_

main.cpp

#include<iostream>
#include<opencv2\opencv.hpp>
#include"slidingWnd.h"
using namespace std;
using namespace cv;

void main()
{
    String imgName = "F:\\lena_gray.jpg";
    Mat src = imread(imgName);
    cvtColor(src, src, COLOR_RGB2GRAY);

    vector<Mat> wnd;
    int count=slidingWnd(src, wnd, Size(30, 30),0.3,0.3);
    imshow("src", src);
    waitKey(0);
}

OpenCV--实现图像滑动窗口截取子图操作
转载自:
博主:XAUT_ee
博文地址:https://blog.csdn.net/jiamuju84/article/details/52893320
来源:CSDN

相关文章: