在大图像中搜索小图像
search for a small image in a big one
我正在从一张大图中搜索灰度图像。
这是我到目前为止所做的,简单地从左到右,从上到下逐像素搜索,它是灰度的,所以我使用bool作为数据类型(1==黑色0==白色)。
#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;
}
我正在寻找一些更好的算法,具有更好的性能。
任何建议吗?
谢谢。
您可以将较大的图像解释为字节串,以便应用字符串搜索算法(如' Boyer-Moore字符串搜索算法' (http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm))来查找较小图像的第一行。找到该行之后,匹配以下行。
但是,如果没有找到较小的图像(未与较大图像中的字节边界对齐),则必须使用移位的较小图像重复搜索(暂时忽略第一个和最后一个字节),直到找到匹配或无法进一步移位为止。
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有根的二进制搜索树.保留与其父级的链接
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 在C++中搜索嵌套多映射值
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 平均图像时图像损坏
- 二进制搜索树叶数问题
- 为什么二进制搜索在我的测试中不起作用
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 正在尝试重载二进制搜索树分配运算符
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 如何将图像数据从搜索窗口中移动到本地存储器OpenCL
- 用于子图像的大图像的C 搜索块
- 图像搜索(C )中的OPENCV图像
- 在 jpeg/bmp/pdf 图像中搜索直线、圆圈和文本
- 如何在程序的内存中搜索图像/png/jpeg/任何其他类型并显示它
- 在大图像中搜索小图像