c++ - 我应该如何纠正这个指针问题,它给我一个错误

标签 c++ pointers image-segmentation

假设 rgbapixel 是 rgbapixel.h 文件中的一类像素,所以它的公共(public)成员有绿色、蓝色、红色等颜色。 PNG 是 png.h 文件中的一类图像,所以它有图像宽度和高度作为私有(private)成员,然后它有两个返回宽度和高度的公共(public)函数。

在我的 main.cpp 中,这是代码;

// sets up the output image
PNG * setupOutput(int w, int h)
{
    PNG * image = new PNG(w, h);
    return image;
}
void sketchify()
{   
    // Load in.png
       PNG * original = new PNG;
    original->readFromFile("in.png");
    int width  = original->width();
    int height = original->height();

    // Create out.png
     // PNG * output;                  // i change this
    PNG * output = new PNG;
     setupOutput(width, height);

    // Loud our favorite color to color the outline
    RGBAPixel * myPixel = myFavoriteColor(192);

    // Go over the whole image, and if a pixel differs from that to its upper
    // left, color it my favorite color in the output
    for (int y = 1; y < height; y++)
    {
            for (int x = 1; x < width; x++)
            {
                    // Calculate the pixel difference
                    RGBAPixel * prev = (*original)(x-1, y-1);      // previous top lfet
                    RGBAPixel * curr = (*original)(x  , y  );        // current

                                        // subtracting to see diffrence between pixels
                    int diff = abs(curr->red   - prev->red  ) +
                                       abs(curr->green - prev->green) +
                                       abs(curr->blue  - prev->blue );

                    // If the pixel is an edge pixel,
                    // color the output pixel with my favorite color

                    RGBAPixel * currOutPixel = (*output)(x,y);
                    if (diff > 100)
                            currOutPixel = myPixel;          // something wrong
            }
    }
    // Save the output file
    output->writeToFile("out.png");
    // Clean up memory
    delete myPixel;
    delete output;
    delete original;

当我执行代码时,出现如下错误;

[EasyPNG]: Warning: attempted to access non-existent pixel (407, 306);
        Truncating request to fit in the range [0,0] x [0,0].

[EasyPNG]: Warning: attempted to access non-existent pixel (408, 306);
        Truncating request to fit in the range [0,0] x [0,0].

在我写“有问题”的地方,有人告诉我那里有错误。我没看到。 'mypixel' 和 currout 都成功声明为指针,所以我看不出该声明有何错误。如果我尝试更改它,则会出现编译错误。帮助

最佳答案

setupOutput(width, height); 没有做任何有用的事情。它在堆上创建一个新的 PNG 但返回的值被丢弃。你以两个问题结束:

  1. output 没有正确设置其宽度和高度。
  2. 存在内存泄漏。

您可以使用两种方法中的一种来解决这个问题。

  1. setupOutput(width, height)的返回值赋给output

    替换行:

    PNG * output = new PNG;
    setupOutput(width, height);
    

    PNG * output = setupOutput(width, height);
    
  2. 根本不要使用 setupOutput(width, height);。创建内嵌的 PNG

    替换行:

    PNG * output = new PNG;
    setupOutput(width, height);
    

    PNG * output = new PNG(width, height);
    

不保证这些更改会解决您的所有问题。

更新,回应 OP 的评论

线

                        currOutPixel = myPixel;          // something wrong

也没有做任何有用的事情。它只是覆盖局部变量 currOutPixel 指向的位置。假设可以分配 RGBAPixel 类型的对象,您需要:

                        *currOutPixel = *myPixel;

关于c++ - 我应该如何纠正这个指针问题,它给我一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25677204/

相关文章:

c++ - 尝试仅通过操作指针对链表进行排序

c++ - 在 C++ 中如何确定 Linux 系统 RAM 的数量?

c++ - 继承的模板化单例类 C++ 中的未定义构造函数

c - 我对 C 中的 (char*) 元素数组进行了三个循环。为什么第三个循环失败了?

c++ - 如何使用 OpenCV Watershed 获取对象之间的轮廓?

matlab - 使用 Matlab 进行图割

matlab - matlab 图像中的自主接缝检测

c++ - OpenglES 2.0 顶点着色器属性

pointers - 指向 Golang 中的结构

c - 为什么当 N 越界时 &a[N] 不调用 UB?