C语言模板匹配程序,不依赖第三方库
时间: 2024-02-24 08:54:05 浏览: 33
如果你不想依赖第三方库,你可以使用C语言的图像处理库,如stb_image和stb_image_write,来实现模板匹配。下面是一个简单的C语言模板匹配程序示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
int main()
{
// Load source and template images
int src_width, src_height, src_channels;
unsigned char *src_data = stbi_load("source.jpg", &src_width, &src_height, &src_channels, 0);
int tpl_width, tpl_height, tpl_channels;
unsigned char *tpl_data = stbi_load("template.jpg", &tpl_width, &tpl_height, &tpl_channels, 0);
// Convert source and template images to grayscale
unsigned char *src_gray = (unsigned char *)malloc(src_width * src_height);
unsigned char *tpl_gray = (unsigned char *)malloc(tpl_width * tpl_height);
for (int i = 0; i < src_width * src_height; i++) {
int r = src_data[i * src_channels];
int g = src_data[i * src_channels + 1];
int b = src_data[i * src_channels + 2];
src_gray[i] = (unsigned char)(0.299 * r + 0.587 * g + 0.114 * b);
}
for (int i = 0; i < tpl_width * tpl_height; i++) {
int r = tpl_data[i * tpl_channels];
int g = tpl_data[i * tpl_channels + 1];
int b = tpl_data[i * tpl_channels + 2];
tpl_gray[i] = (unsigned char)(0.299 * r + 0.587 * g + 0.114 * b);
}
// Compute correlation between source and template images
int result_width = src_width - tpl_width + 1;
int result_height = src_height - tpl_height + 1;
float *result_data = (float *)malloc(result_width * result_height * sizeof(float));
for (int y = 0; y < result_height; y++) {
for (int x = 0; x < result_width; x++) {
float sum = 0.0f;
for (int j = 0; j < tpl_height; j++) {
for (int i = 0; i < tpl_width; i++) {
int src_x = x + i;
int src_y = y + j;
int tpl_x = i;
int tpl_y = j;
unsigned char src_pixel = src_gray[src_y * src_width + src_x];
unsigned char tpl_pixel = tpl_gray[tpl_y * tpl_width + tpl_x];
sum += (float)src_pixel * (float)tpl_pixel;
}
}
result_data[y * result_width + x] = sum;
}
}
// Find maximum correlation value and corresponding position
float max_val = result_data[0];
int max_x = 0, max_y = 0;
for (int y = 0; y < result_height; y++) {
for (int x = 0; x < result_width; x++) {
float val = result_data[y * result_width + x];
if (val > max_val) {
max_val = val;
max_x = x;
max_y = y;
}
}
}
// Draw rectangle around matched region in source image
for (int y = 0; y < src_height; y++) {
for (int x = 0; x < src_width; x++) {
if (x >= max_x && x < max_x + tpl_width && y >= max_y && y < max_y + tpl_height) {
src_data[(y * src_width + x) * src_channels] = 0;
src_data[(y * src_width + x) * src_channels + 1] = 0;
src_data[(y * src_width + x) * src_channels + 2] = 255;
}
}
}
// Save marked source image
stbi_write_jpg("result.jpg", src_width, src_height, src_channels, src_data, 100);
// Free memory
stbi_image_free(src_data);
stbi_image_free(tpl_data);
free(src_gray);
free(tpl_gray);
free(result_data);
return 0;
}
```
上面的程序将原始图像和模板图像加载到内存中,将它们转换为灰度图像,并执行模板匹配,查找匹配位置,并在原始图像上标记匹配位置。最后,它将保存标记后的原始图像。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)