我的opencv图像处理代码的这些部分。在其中,我生成两个动态数组来存储二进制图像中每个col / row的黑点总数。 以下是代码:
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat srcImg = imread("oura.bmp");
width = srcImg.cols - 2;
height = srcImg.rows - 2;
Mat srcGrey;
Mat srcRoi(srcImg, Rect(1, 1, width, height));
cvtColor(srcRoi, srcGrey, COLOR_BGR2GRAY);
int thresh = 42;
int maxval = 255;
threshold(srcGrey, srcRoiBina, thresh, maxval, THRESH_BINARY);
int *count_cols = new int[width] ();
int *count_rows = new int[height] ();
for (int i = 0; i < width; i++)
{
cout << count_cols[i] << endl;
}
for (int i = 0; i < height; i++)
{
uchar *data = srcRoiBina.ptr<uchar>(i);
for (int j = 0; j < width; j++)
{
if (data[j] == 0)
{
count_cols[j]++;
count_rows[i]++;
}
}
}
delete[] count_cols;
delete[] count_rows;
return 0;
}
我的问题是:如果我使用以下代码
int *count_cols = new int[width];
int *count_rows = new int[height];
memset(count_cols, 0, sizeof(count_cols));
memset(count_rows, 0, sizeof(count_rows));
for (int i = 0; i < width; i++)
{
cout << count_cols[i] << endl;
}
替换下面的相应代码,为什么动态数组不能初始化为零?似乎memset不起作用。
平台:Visual Stdio 2013 + opencv 3.0.0
你能帮帮我吗?此外,原始图像oura.bmp为2592 * 1944.因此动态数组count_cols的长度为2590(即2592-2)。有一些潜在的问题吗?
答案 0 :(得分:3)
count_cols
的类型为int*
,因此sizeof(count_cols)
将为8(64位)或4(32位)。您将要使用sizeof(int) * width
代替(对于行也是如此)。
答案 1 :(得分:2)
sizeof(count_rows)
返回指针的大小,而不是数组的大小。
请改用height * sizeof(int)
。同样适用于列。