MEMSET FUCTION在我的C 动态阵列初始化中不起作用
memset fuction does not work in my c++ dynamic array initialization
我的OpenCV图像处理代码的某些部分。在IT上,我生成了两个动态数组来存储二进制图像中每个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
你能帮我吗?
此外,原始图像outa.bmp是2592*1944。有一些潜在的问题吗?
count_cols
是int*
类型,因此sizeof(count_cols)
为8(64位)或4(32bit)。您需要使用sizeof(int) * width
(类似地用于行)。
sizeof(count_rows)
正在返回指针的大小,而不是数组的大小。
改用height * sizeof(int)
。同样适用于列。
相关文章:
- 阵列必须使用大括号封闭的初始器进行初始化
- 3D阵列C 的动态初始化
- 您应该如何在OpenGL 4.5中初始化立方体地图阵列
- 即使在C 中越过初始化阵列的极限后,结果也是如此
- 工会结构阵列的初始化太多
- 在C 中构造函数中初始化C阵列的错误
- 防止C 中阵列元素的构建/初始化
- 从其初始化的功能中使用的特定大小的阵列
- 阵列初始化结构
- SFINAE 在 VS10 中初始化阵列
- 静态成员的静态阵列:初始化顺序惨败的可能性
- DP阵列初始化的原因
- 阵列初始化器中的多余元素
- MEMSET FUCTION在我的C 动态阵列初始化中不起作用
- 静态2D阵列初始化
- 阵列初始化并找到字母频率
- Eclipse C 格式化阵列初始化器
- 阵列初始化问题
- C++最快的阵列初始化
- C++阵列初始化的氧气问题