W AI napisałem to zapytanie
template matching po polsku zwraca x y program c++ bez biblioteki opencv bez c++11 wczytaj obrazy jpg
otrzymałem prosty kod
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>
#include <climits>
// Funkcja obliczająca dopasowanie metodą sumy kwadratów różnic (SSD)
void findTemplate(const unsigned char* img, int imgW, int imgH,
const unsigned char* tmpl, int tmplW, int tmplH,
int& outX, int& outY) {
long long minDiff = LLONG_MAX;
outX = 0;
outY = 0;
for (int y = 0; y <= imgH - tmplH; ++y) {
for (int x = 0; x <= imgW - tmplW; ++x) {
long long currentDiff = 0;
for (int ty = 0; ty < tmplH; ++ty) {
for (int tx = 0; tx < tmplW; ++tx) {
int imgIdx = ((y + ty) * imgW + (x + tx)) * 3;
int tmplIdx = (ty * tmplW + tx) * 3;
// Suma różnic dla kanałów RGB
for (int c = 0; c < 3; ++c) {
long long diff = img[imgIdx + c] - tmpl[tmplIdx + c];
currentDiff += diff * diff;
}
}
}
if (currentDiff < minDiff) {
minDiff = currentDiff;
outX = x;
outY = y;
}
}
}
}
int main() {
int imgW, imgH, imgChannels;
int tmplW, tmplH, tmplChannels;
// Wczytywanie obrazów JPG
unsigned char* imgData = stbi_load("obraz.jpg", &imgW, &imgH, &imgChannels, 3);
unsigned char* tmplData = stbi_load("szablon.jpg", &tmplW, &tmplH, &tmplChannels, 3);
if (!imgData || !tmplData) {
std::cout << "Blad wczytywania obrazow!" << std::endl;
return 1;
}
std::cout << "Wymiary obrazu glownego: " << imgW << "x" << imgH << " px" << std::endl;
std::cout << "Wymiary szablonu: " << tmplW << "x" << tmplH << " px" << std::endl;
int bestX = 0, bestY = 0;
findTemplate(imgData, imgW, imgH, tmplData, tmplW, tmplH, bestX, bestY);
std::cout << "Znaleziono wspolrzedne -> X: " << bestX << ", Y: " << bestY << std::endl;
stbi_image_free(imgData);
stbi_image_free(tmplData);
return 0;
}
i funkcja findTemplate() się zawiesza i nie wiem, gdzie jest błąd. Czy możecie pomóc?