C++ 将1添加到向量<;无符号字符>;值-C+中的直方图+;

C++ 将1添加到向量<;无符号字符>;值-C+中的直方图+;,c++,vector,histogram,vision,C++,Vector,Histogram,Vision,我想这是一个非常简单的问题(我来自Java),但我不知道它是如何工作的 我只想将一个向量元素增加1。这样做的原因是,我想用图像值计算直方图。但无论我尝试什么,我都能为向量赋值。但不是增加一个 这是我的直方图函数: void histogram(unsigned char** image, int height, int width, vector<unsigned char>& histogramArray) { for (int i = 0; i

我想这是一个非常简单的问题(我来自Java),但我不知道它是如何工作的

我只想将一个向量元素增加1。这样做的原因是,我想用图像值计算直方图。但无论我尝试什么,我都能为向量赋值。但不是增加一个

这是我的直方图函数:

void histogram(unsigned char** image, int height,
        int width, vector<unsigned char>& histogramArray) {

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {


//          histogramArray[1] = (int)histogramArray[1] + (int)1;
// add histogram position by one if greylevel occured
            histogramArray[(int)image[i][j]]++;
        }
    }
// display output
    for (int i = 0; i < 256; i++) {
        cout << "Position: " << i << endl;
        cout << "Histogram Value: " << (int)histogramArray[i] << endl;
    }
}
有什么简单易行的方法吗?我认为迭代器在这一点上是不必要的,因为我知道exakt索引的位置,在那里我想要增加一些东西

编辑: 很抱歉,我应该更准确地回答我的问题,谢谢你迄今为止的帮助!上面的代码是有效的,但它显示的直方图的平均值与它应该显示的值不同(相差约90)。此外,直方图值与图形程序中的值有很大的不同,即使图像值完全相同!这就是为什么我研究了这个函数,发现如果我将直方图设置为零,然后只增加一个元素,什么都不会发生!这是上面的注释代码:

for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                histogramArray[1]++;
    // add histogram position by one if greylevel occured
                // histogramArray[(int)image[i][j]]++;
            }
        }
for(int i=0;i
因此位置1保持为0,而不是值height*width。因此,我认为正确的计算historgramaray[image[I][j]]++;也不能正常工作

你对此有什么解释吗?这是我的主要问题,对不起

为了完整起见,这是直方图的平均函数:

unsigned char meanHistogram(vector<unsigned char>& histogram) {
    int allOccurences = 0;
    int allValues = 0;
    for (int i = 0; i < 256; i++) {
        allOccurences += histogram[i] * i;
        allValues += histogram[i];
    }
    return (allOccurences / (float) allValues) + 0.5f;
}
无符号字符平均直方图(向量和直方图){
int alloccurrences=0;
int-allValues=0;
对于(int i=0;i<256;i++){
Alloccurrences+=直方图[i]*i;
所有值+=直方图[i];
}
返回(alloccurrences/(float)allValues)+0.5f;
}
我初始化图像如下:

unsigned char** image= new unsigned char*[width];
for (int i = 0; i < width; i++) {
image[i] = new unsigned char[height];
}
char** image = new char* [height];
for (int i = 0; i < height; ++i)
    image[i] = new char [width];
for (int i = 0; i < height; ++i)
    delete[] image[i];
delete[] image;
// Allocate:
char * image = new char [height * width];

// Free:
delete[] image;
image[i * width + j] = 42;
char x = image[i * width + j];
void histogram (
    unsigned char * image, int height, int width,
     // Note that the elements here are pixel-counts, not colors!
    vector<unsigned> & histogram
) {
    // Make sure histogram has enough room; you can do this outside as well.
    if (histogram.size() < 256)
        histogram.resize (256, 0);

    int pixels = height * width;
    for (int i = 0; i < pixels; ++i)
        histogram[image[i]]++;
}
unsigned char**image=新的unsigned char*[width];
对于(int i=0;i
但是初始化代码应该没有任何问题,因为所有其他的计算工作都很完美,我能够操纵并保护原始图像。但这是真的,我应该改变宽度和高度——因为我只有正方形的图像,到目前为止这并不重要。 直方图是这样创建的,然后函数调用如下:

vector<unsigned char> histogramArray(256);
histogram(array, adaptedHeight, adaptedWidth, histogramArray);
vector historogramaray(256);
直方图(数组、自适应高度、自适应宽度、直方图Marray);
那么你知道为什么这部分是历史性的吗;不要增加我的直方图?HistorograMarray[1]始终保持为0!Historogramaray[1]=2;它工作得很好。还有Historogramaray[(int)image[i][j]]++;似乎在计算一些东西,但正如我所说的,我认为它计算错了。
我非常感谢任何帮助!我使用2D数组的原因很简单,因为它是被要求的。我更喜欢1D版本,因为它更简单

您可以看到,代码中当前的问题是不是增加值而不是分配值;这是你索引图像的方式。您编写
直方图
函数和图像访问部分的方式对如何分配图像以使此代码正常工作提出了非常严格的限制

例如,假设您的直方图函数与上面编写的一样,这些图像分配策略都不起作用:(为了简洁起见,我使用了
char
而不是
unsigned char

第三种情况行不通的原因很难简单解释。只需说这样的2D数组不会隐式地衰减为指针到指针的指针,如果它这样做了,它将毫无意义。与你在一些书中读到的或者从一些人那里听到的相反,在C/C++中,数组和指针不是一回事

无论如何,要使直方图功能正常工作,必须按如下方式分配图像:

unsigned char** image= new unsigned char*[width];
for (int i = 0; i < width; i++) {
image[i] = new unsigned char[height];
}
char** image = new char* [height];
for (int i = 0; i < height; ++i)
    image[i] = new char [width];
for (int i = 0; i < height; ++i)
    delete[] image[i];
delete[] image;
// Allocate:
char * image = new char [height * width];

// Free:
delete[] image;
image[i * width + j] = 42;
char x = image[i * width + j];
void histogram (
    unsigned char * image, int height, int width,
     // Note that the elements here are pixel-counts, not colors!
    vector<unsigned> & histogram
) {
    // Make sure histogram has enough room; you can do this outside as well.
    if (histogram.size() < 256)
        histogram.resize (256, 0);

    int pixels = height * width;
    for (int i = 0; i < pixels; ++i)
        histogram[image[i]]++;
}
就这样,;没有讨厌的(去)分配循环,每个映像都是一个连续的内存块。当您想访问行
i
和列
j
(注意哪个是哪个)时,您可以这样做:

unsigned char** image= new unsigned char*[width];
for (int i = 0; i < width; i++) {
image[i] = new unsigned char[height];
}
char** image = new char* [height];
for (int i = 0; i < height; ++i)
    image[i] = new char [width];
for (int i = 0; i < height; ++i)
    delete[] image[i];
delete[] image;
// Allocate:
char * image = new char [height * width];

// Free:
delete[] image;
image[i * width + j] = 42;
char x = image[i * width + j];
void histogram (
    unsigned char * image, int height, int width,
     // Note that the elements here are pixel-counts, not colors!
    vector<unsigned> & histogram
) {
    // Make sure histogram has enough room; you can do this outside as well.
    if (histogram.size() < 256)
        histogram.resize (256, 0);

    int pixels = height * width;
    for (int i = 0; i < pixels; ++i)
        histogram[image[i]]++;
}
你可以这样计算柱状图:

unsigned char** image= new unsigned char*[width];
for (int i = 0; i < width; i++) {
image[i] = new unsigned char[height];
}
char** image = new char* [height];
for (int i = 0; i < height; ++i)
    image[i] = new char [width];
for (int i = 0; i < height; ++i)
    delete[] image[i];
delete[] image;
// Allocate:
char * image = new char [height * width];

// Free:
delete[] image;
image[i * width + j] = 42;
char x = image[i * width + j];
void histogram (
    unsigned char * image, int height, int width,
     // Note that the elements here are pixel-counts, not colors!
    vector<unsigned> & histogram
) {
    // Make sure histogram has enough room; you can do this outside as well.
    if (histogram.size() < 256)
        histogram.resize (256, 0);

    int pixels = height * width;
    for (int i = 0; i < pixels; ++i)
        histogram[image[i]]++;
}
void直方图(
无符号字符*图像,整数高度,整数宽度,
//请注意,这里的元素是像素计数,而不是颜色!
向量与直方图
) {
//确保柱状图有足够的空间;你也可以在室外进行。
if(直方图大小()<256)
直方图大小(256,0);
整数像素=高度*宽度;
用于(int i=0;i
我已经删除了打印代码,但它无论如何都不应该存在。注意,我使用了一个循环来遍历整个图像;这是分配1D阵列的另一个优点。同样,对于这个特殊的函数,你的图像是行主还是列主并不重要,因为我们通过像素的顺序并不重要;重要的是我们要通过所有的像素,而不是更多

更新:问题更新后,我认为上述所有讨论都没有意义,尽管如此!我相信问题可能在于直方图向量的声明。它应该是
无符号int
s的向量,而不是单个字节。您的问题似乎是,当您简化代码并只增加一个元素时,向量元素的值似乎保持为零,而当您运行实际代码时,向量元素的值与它们需要的值不一致。这可能是数字环绕的症状。如果图像中的像素数是256的倍数(例如32x32或1024x1024图像),则它们的总数自然为0 mod