我正在构建一个程序,用于在简单的二维数组中搜索,识别和标记整数值图的位置。
我手动追踪了第一个例子,它看起来准确无误。有了这个说我写的代码不能做我认为它做的事情或我的手跟踪是不准确的。
我认为我的代码很接近,我正在寻找一些调试帮助以及对一般风格的任何想法等。
最终将修改此算法以查找OCR字符像素的图形。我只是想在使用处理图像的代码复杂化之前证明我的算法实现是准确的。
输入数组可能如下所示:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 0 0
0 0 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
预期结果如下:
3 3 3 3 3 3
3 0 0 0 0 3
3 0 2 2 0 3
3 0 2 2 0 3
3 0 0 0 0 3
3 3 3 3 3 3
另一种类似的可能性是: 在:
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
出:
0 3 3 3 3 3 3 0 0 0 0 0
0 3 0 0 0 0 3 0 0 0 0 0
0 3 0 2 2 0 3 0 0 0 0 0
0 3 0 2 2 0 3 0 0 0 0 0
0 3 0 0 0 0 3 0 0 0 0 0
0 3 3 3 3 3 3 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 3 3 3 3 3 3 3 3 3 0
0 0 3 0 0 0 0 0 0 0 3 0
0 0 3 0 2 2 2 2 2 0 3 0
0 0 3 0 0 0 0 0 0 0 3 0
0 0 3 3 3 3 3 3 3 3 3 0
基本规则:
处理此阵列的最新尝试:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
产生此输出:
0 0 0 0 0 0 0 0
0 3 3 3 3 3 0 0
0 3 3 3 3 3 3 0
0 3 3 2 1 3 3 0
0 3 3 2 2 3 3 0
0 3 3 3 3 3 3 0
0 3 3 3 3 3 3 0
0 0 0 0 0 0 0 0
虽然单个数字图表效果很好:
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
产出输出:
3 3 3 3 3
3 0 0 0 3
3 0 2 0 3
3 0 0 0 3
3 3 3 3 3
这是我的代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "queue.h"
#define GS 8 /* GRID SIZE */
using namespace std;
void processCmdArgs (ifstream& input, int argc, char* argv[]);
void drawBoundingBox (int arr[][GS], int xLo, int yLo, int xHi, int yHi);
void checkNeighbors (int arr[][GS], bool vis[][GS], queue Q, point* p);
void print (int arr[][GS]);
int main( int argc, char* argv[] ) {
int xLo = 0;
int xHi = GS - 1;
int yLo = 0;
int yHi = GS - 1;
ifstream input; /* filestream to read in file to parse */
int arr[GS][GS]; /* declare array of vals to check for graph */
bool visited[GS][GS]; /* array of bools to track progress */
int count = 0; /* number of graphs found */
processCmdArgs(input, argc, argv);
/* populate array */
for (int i = 0; i < GS; i++) {
for (int j = 0; j < GS; j++) {
input >> arr[i][j];
}
}
input.close();
/*init visited */
for (int y = yLo; y < GS; y++) {
for (int x = xLo; x < GS; x++) {
visited[x][y] = false;
}
}
/* print array */
cout << "The array to find a graph is:\n";
print(arr);
/* find graph(s) in array */
queue Q;
for (int j = yLo; j < GS; j++) {
for (int k = xLo; k < GS; k++) {
if (arr[k][j] == 1) {
count++;
xLo = xHi = k;
yLo = yHi = j;
point *p = new point(k, j);
Q.insert(p);
delete p;
visited[k][j] = true;
while (!Q.isEmpty()) {
*p = Q.del(); /* does this really work? */
int x = p->getx();
int y = p->gety();
arr[x][y] = 2;
if (x < xLo) xLo = x;
if (y < yLo) yLo = y;
if (x > xHi) xHi = x;
if (y > yHi) yHi = y;
checkNeighbors(arr, visited, Q, p);
}
drawBoundingBox(arr, xLo, yLo, xHi, yHi);
}
else {
visited[k][j] = true;
}
}
}
cout << "The updated array is:\n";
print(arr);
cout << "The number of graphs in arr is " << count << endl;
return 0;
}
/*** END OF MAIN ***/
/*** START OF FUNCTIONS ***/
void processCmdArgs(ifstream& input, int argc, char* argv[]) {
/* Check command-line args first to avoid accessing nonexistent memory */
if (argc != 2) {
cerr << "Error: this program takes one command-line argument.\n";
exit(1);
}
/* Try to open the file using the provided filename */
input.open(argv[1]);
/* Exit with error if it doesn't open */
if (input.fail()) {
cerr << "Error: could not open " << argv[1] << ".\n";
exit(1);
}
}
void drawBoundingBox (int arr[][GS], int xLo, int yLo, int xHi, int yHi) {
// draw a box with (lowx-2,lowy-2) as NW and
// (highx + 2, highy + 2) as SE boundary
/* draw top and bottom of box */
for (int x = xLo - 2; x <= xHi + 2; x++) {
arr[x][yLo - 2] = 3;
arr[x][yHi + 2] = 3;
}
/* draw sides of box */
for (int y = yLo - 1; y <= yHi + 1; y++) {
arr[xLo - 2][y] = 3;
arr[xHi + 2][y] = 3;
}
}
void checkNeighbors (int arr[][GS], bool vis[][GS], queue Q, point* p) {
int pX = p->getx();
int pY = p->gety();
for (int y = pY - 1; y <= pY + 1; y++) {
for (int x = pX - 1; x <= pX + 1; x++) {
if (x == pX && y == pY) {/* easier than opposite boolean logic */ }
else {
if (vis[x][y] == false) vis[x][y] = true;
if (arr[x][y] == 1) {
point *n = new point(x, y);
Q.insert(n);
delete n;
}
}
}
}
}
void print (int arr[][GS]) {
/* print array */
for (int i = 0; i < GS; i++) {
for (int j = 0; j < GS; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
/*** END OF FUNCTIONS ***/
/*** START of QUEUE CLASS ***/
const int MSIZE = 1000;
class point {
private:
int x; int y;
public:
point(int p, int q) {
x = p; y = q;
}
int getx() {
return x;
}
int gety() {
return y;
}
};
class queue {
private:
point* Q[MSIZE];
int front, rear, size;
public:
queue() {
// initialize an empty queue
//front = 0; rear = 0; size = 0;
front = rear = size = 0;
for (int j = 0; j < MSIZE; ++j)
Q[j] = 0;
}
void insert(point* x) {
if (size != MSIZE) {
front++; size++;
if (front == MSIZE) front = 0;
Q[front] = x;
}
}
point del() {
if (size != 0) {
rear++; if (rear == MSIZE) rear = 0;
point temp(Q[rear]->getx(), Q[rear]->gety());
size--;
return temp;
}
}
void print() {
for (int j = 1; j <= size; ++j) {
int i = front - j + 1;
cout << "x = " << Q[i]->getx() << " y = " << Q[i]->gety() << endl;
}
cout << "end of queue" << endl;
}
bool isEmpty() {
return (size == 0);
}
};
/*** END of QUEUE CLASS ***/
答案 0 :(得分:2)
现在让我们来寻找错误......
现在它适用于一个图表。我不打算用两个来试试。
修改:
我必须吃掉我的一些话:你没有用[j][k]
索引,我只是因为你使用(k,j) <=> (x,y)
而感到困惑,并把它与实际的bug混淆了别处。现在我看到你正在用queue
做什么,但是你应该认真看待STL。
真正严重的错误在checkNeighbors(...)
的签名中。您按值传递Q
,而不是通过引用。修复此问题,代码适用于多个图表。
修改:
是的,另一个错误:queue
存储指向点而不是点的指针,没有特别的原因(参见上面的“6”),并且不知何故它将它们弄脏了。我没有找到确切的错误,而是更改queue
来处理点数,并获得了复杂图表的正确结果。