Accessing array in loop

As I'm new in CPP it might be an easy question or not. However, I try to access an array of int but get a different result before and within a loop. Could any body give me a hint what I'm doing wrong, please?

1
2
3
4
5
6
7
  int i = 46
  qDebug() << "Before" << i << row_val[i] << row_val[46] << sizeof(row_val) / sizeof(row_val[0]);
  i=0;
  while ((row_val[i] < row_max/5) && (i < detected_edges.rows)) {
    i++;
    if (i==46) {qDebug() << "During" << i << row_val[i] << row_val[46];}
  }


I get following output:
Before 46 66 66 4912
During 46 0 66

Thanks
Last edited on
What are the values of your variables beside i?
detected_edges.rows (4911) and row_max (322) are int's but it doesn't have an impact when I delete them.
Your error seems strange. What will be printed if you change qDebug() by std::cerr?
Could yo make a short runnable code piece from your failing part and post it here? Do you use the Qt-Framework? Try to make a test example without using the Qt facilities.
I'm not sure what you mean with ""without using QT facilities". However, cout gives same result.

1
2
3
4
5
6
7
8
9
10
11
  qDebug() << "BeforeQ" << i << row_val[i] << row_val[46] << sizeof(row_val) / sizeof(row_val[0])  << row_max << detected_edges.rows;
  std::cout <<  "BeforeC " << i << " " << row_val[i] << " " << row_val[46] << " " << sizeof(row_val) / sizeof(row_val[0])  << " " << row_max << " " << detected_edges.rows << std::endl;
  // Get Y_Min
  i=0;
  while (row_val[i] < 60) {
    i++;
    if (i==46) {
      qDebug() << "DuringQ" << i << row_val[i] << row_val[46];
      std::cout<< "DuringC " << i << " " << row_val[i] << " " << row_val[46] << std::endl;
    }
  }


BeforeC 46 66 66 4912 322 4911
DuringC 46 0 66
RwoMax: 322
BeforeQ 46 66 66 4912 322 4911
DuringQ 46 0 66
Last edited on
Could you post (at least) the whole function?
Sure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class OpenCV_Func {
  public:
    static void AutocropCany(cv::Mat& image, int hold=50, int lowThreshold =50, int ratio = 5, int kernel_size = 5);

    struct PointValue {
      long r, g, b;
    };
};

void OpenCV_Func::AutocropCany(cv::Mat& image, int hold, int lowThreshold, int ratio, int kernel_size) {
  cv::Mat image_gray, detected_edges;

  // Convert the image to grayscale
  cvtColor( image, image_gray, CV_BGR2GRAY );

  // Reduce noise with a kernel 3x3
  blur( image_gray, detected_edges, Size(kernel_size,kernel_size) );

  // Canny detector
  Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
  // Column and Row summary
  int row_val[detected_edges.rows+1], col_val[detected_edges.cols+1];
  memset(row_val, 0, sizeof(row_val));
  memset(col_val, 0, sizeof(col_val));
  int variant = 5;
  uint8_t* pixelPtr = (uint8_t*) detected_edges.data;

  int row_max = 0, col_max = 0;
  int crossSize = 5;
  for(int row = 0; row<detected_edges.rows; row++) {
    int colCounter = 0;
    for (int col =0; col<detected_edges.cols; col++) {
      if (pixelPtr[row * detected_edges.cols + col] == 0) {
        colCounter = 0;
      } else {
        if (++colCounter >= crossSize) row_val[row] = row_val[row]+1;
      }
    }
    row_max = max(row_max, row_val[row]);
  }

  for(int i=variant; i<detected_edges.rows-variant; i++) {
    int offset = detected_edges.cols * i;
    for(int j=variant; j<detected_edges.cols-variant; j++) {
      for (int count = -variant; count <= variant; count++) {
        col_val[i] *= pixelPtr[detected_edges.cols * (i+variant) + j]>0;
      }
      col_val[i] /=5;
    }
  }

  // Column and Row maximum
  for (int i=0; i<detected_edges.cols-1; i++) col_max = max(col_max, col_val[i]);

  int x_min = detected_edges.cols, x_max = 0;
  int y_min = detected_edges.rows, y_max = 0;

  // Get X_Min
  int i=0;
  while (col_val[i] < col_max/5 && i < detected_edges.cols) i++;
  x_min = max(i-hold, 0);

  // Get X_Max
  i=detected_edges.cols;
  while (col_val[i] < col_max/5 && i > 0) i--;
  x_max = min(i+hold, detected_edges.cols-1);

  i=46;
  qDebug() << "BeforeQ" << i << row_val[i] << row_val[46] << sizeof(row_val) / sizeof(row_val[0])  << row_max << detected_edges.rows;
  std::cout <<  "BeforeC " << i << " " << row_val[i] << " " << row_val[46] << " " << sizeof(row_val) / sizeof(row_val[0])  << " " << row_max << " " << detected_edges.rows << std::endl;
  // Get Y_Min
  i=0;
  while (row_val[i] < 60) {
    i++;
    if (i==46) {
      qDebug() << "DuringQ" << i << row_val[i] << row_val[46];
      std::cout<< "DuringC " << i << " " << row_val[i] << " " << row_val[46] << std::endl;
    }
  }
...
}

Topic archived. No new replies allowed.