Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在大图像中搜索小图像_C++_Image_Algorithm_Search - Fatal编程技术网

C++ 在大图像中搜索小图像

C++ 在大图像中搜索小图像,c++,image,algorithm,search,C++,Image,Algorithm,Search,我正在从一个大的图像中搜索灰度图像 这是我到目前为止所做的,只需从左到右,从上到下逐像素搜索,它是灰度的,所以我使用bool作为数据类型(1==黑色0==白色) #包括 使用名称空间std; 模板 结构数组{ 布尔数据[宽度][高度]; int width(){return width;} int height(){return height;} 无效随机填充(){ 对于(int row=0;row您可以将较大的图像解释为一个字节字符串,以便应用类似“Boyer–Moore字符串搜索算法”(Bo

我正在从一个大的图像中搜索灰度图像

这是我到目前为止所做的,只需从左到右,从上到下逐像素搜索,它是灰度的,所以我使用bool作为数据类型(1==黑色0==白色)

#包括
使用名称空间std;
模板
结构数组{
布尔数据[宽度][高度];
int width(){return width;}
int height(){return height;}
无效随机填充(){

对于(int row=0;row您可以将较大的图像解释为一个字节字符串,以便应用类似“Boyer–Moore字符串搜索算法”(Boyer–Moore string search algorithm))的字符串搜索算法来查找较小图像的第一行。找到该行后,匹配以下行


但是,如果未找到较小的图像(未与较大图像中的字节边界对齐),则必须使用移位的较小图像重复搜索(暂时忽略第一个和最后一个字节),直到找到匹配点或没有进一步的移位是合理的。

为什么没有std::vector?我只是给了您的代码一个预兆性的一瞥,但您的第一个检查应该是单个点(我将使用左上角)是否与您的目标点匹配。然后检查图像。(或者,如果你想更大胆一些,在检查左上角的像素后,接下来检查四个角。或者,你可以检查从左上角到色调/饱和度最远的点。或者……)谢谢,Boyer-Moore算法看起来很有趣。
#include <iostream>
using namespace std;

template <int WIDTH, int HEIGHT>
struct array {
    bool data[WIDTH][HEIGHT];
    int width() { return WIDTH; }
    int height() { return HEIGHT; }

    void random_fill() {
        for(int row=0; row<HEIGHT; row++) {
            for(int col=0; col<WIDTH; col++) {
                data[row][col] = (row*col+col*col) % 3 == 0 ? 1 : 0;
            }
        }
    }
    void display() {
        cout << "array content:" << endl;
        for(int row=0; row<HEIGHT; row++) {
            for(int col=0; col<WIDTH; col++) {
                cout << data[row][col] << " ";
            }
            cout << endl;
        }
    }
    void operator=(bool _data[WIDTH][HEIGHT]) {
        memcpy(data, _data, WIDTH*HEIGHT);
    }
};

struct point {
    int x;
    int y;
};

// test if a sub-rect of a big_rect matches a small rect
template <typename big_t, typename small_t>
bool rect_match(big_t& big_arr, int x_offset, int y_offset, small_t& small_arr) {
    int w = small_arr.width(),
        h = small_arr.height();
    for(int row=0; row<h; row++) {
        for(int col=0; col<w; col++) {
            if(big_arr.data[row+y_offset][col+x_offset] != small_arr.data[row][col])
                return false;
        }
    }
    return true;
}

// search for a small_rect in a big_rect
template <typename big_t, typename small_t>
point search(big_t& big_arr, small_t& small_arr) {
    point pt;
    for(int row=0; row<big_arr.height()-small_arr.height(); row++) {
        for(int col=0; col<big_arr.width()-small_arr.width(); col++) {
            if(rect_match(big_arr, col, row, small_arr)) {
                pt.x = col;
                pt.y = row;
                return pt;
            }
        }
    }

    pt.x = pt.y = -1;
    return pt;
}

int main() {
    array<10, 10> big_arr;
    big_arr.random_fill(); // fill the sample image with some "random" color
    big_arr.display(); 

    array<3, 3> small_arr;
    bool data[3][3] = {{1,0,1},{0,0,1},{0,1,1}};
    small_arr = data;
    small_arr.display(); 

    point pt = search(big_arr, small_arr);
    cout << "pt: (" << pt.x << ", " << pt.y << ")" << endl;

}