矩阵中子矩阵匹配异常求助:仅单个矩阵找到匹配结果
矩阵子匹配问题排查
我需要在N×N整数方阵中查找子矩阵,要求子矩阵与大矩阵对应区域的数值及顺序完全一致(类似像素裁剪)。矩阵与子矩阵均从文件读取,初始化工作正常,但find、matching、distance函数存在问题:除单个矩阵外,其余均未找到匹配结果。测试少量数据时功能正常,我期望能找到更多匹配结果。
相关代码
typedef struct { int size; int *data; } Matrix; int distance(int p, int o) { //printf("p %d o %d 0:%f 1: %f 2:%d\n", p, o, (double)(p - o) / p, ceil((double)(p - o) / p), abs(ceil((double)(p - o) / p))); return abs(ceil((double)(p - o) / p)); } int matching(Matrix pic, int y, int x, Matrix obj, float value) { int sum = 0; int colStart = x; for (int i = 0; i < obj.size; i++, y++) { for (int j = 0; j < obj.size; j++, x++) { sum += distance(pic.data[(y) * pic.size + (x)], obj.data[i * obj.size + j]); if (sum > value) return 0; } x = colStart; } return sum < value; } int *find(Matrix p, Matrix *matOfObjects, int size, int (*pos)[3][2], float value) { int count = 0; int *matchObjs = (int *)malloc(3 * sizeof(int)); if (matchObjs == NULL) { printf("Error: failed to allocate memory for array\n"); exit(1); } for (int k = 0; k < size; k++) { for (int i = 0; i <= p.size - matOfObjects[k].size; i++) { for (int j = 0; j <= p.size - matOfObjects[k].size; j++) { if (count > 2) return matchObjs; else if (j + matOfObjects[k].size > p.size || i + matOfObjects[k].size > p.size) // out of bound of picture check continue; else if (matching(p, i, j, matOfObjects[k], value)) { matchObjs[count] = k + 1; (*pos)[count][0] = i; (*pos)[count][1] = j; count++; } } } } if (count > 2) return matchObjs; else { free(matchObjs); return NULL; } } int main() { int size, numOfps, numOfobjs; float matchingV; FILE *fp = fopen(FILENAME, "r"); // Open file for reading if (fp == NULL) { printf("Error: failed to open file '%s'\n", FILENAME); exit(1); } fscanf(fp, "%f %d", &matchingV, &numOfps); Matrix *arrP = readMatrixes(&fp, numOfps); fscanf(fp, "%d", &numOfobjs); Matrix *arrO = readMatrixes(&fp, numOfobjs); fclose(fp); // Close file int foundObjs[3]; int positions[3][2]; for (int i = 0; i < numOfps; i++) { int *arr = find(arrP[i], arrO, numOfobjs, &positions, matchingV); if (arr != NULL) { memcpy(foundObjs, find(arrP[i], arrO, numOfobjs, &positions, 0.100000), 3 * sizeof(int)); if (!foundObjs) { printf("Error: failed to copy array\n"); exit(1); } printf("Picture %d: found Objects:", i + 1); for (int j; j < 3; j++) printf(" %d Position(%d,%d)", foundObjs[j], positions[j][0], positions[j][1]); printf("\n\n"); } else printf("Picture %d: No three different Objects were found\n", (i + 1)); free(arr); } // write_matrix_to_file("output.txt", numOfobjs, arrO); freeMatrix(arrP, numOfps); // Free memory freeMatrix(arrO, numOfobjs); // Free memory /*testing------------------------------------------------ Matrix mat; Matrix sub; int m[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int m2[4] = { 2, return count > 2 ? matchObjs : NULL; { for (int j = 0; j <= mat.size - sub.size; j++) { printf("check %d\n", j); if (matching(mat, i, j, sub, 0.100000)) printf("%d %d true\n", i, j); else puts("false"); } } for (int i = 0; i < sub.size; i++) for (int j = 0; j < sub.size; j++) printf("%d\n", sub.data[i * sub.size + j]); */ return 0; }
当前输出
Picture 1: No three different Objects were found Picture 2: No three different Objects were found Picture 3: No three different Objects were found Picture 4: No three different Objects were found Picture 5: found Objects: 1 Position(20,20) 1 Position(300,300) 5 Position(200,200)
内容的提问来源于stack exchange,提问作者IdoS99
相关产品推荐
相关产品推荐

