【发布时间】:2023-03-22 01:51:02
【问题描述】:
对于 CS50 Pset4 过滤器边缘部分,https://cs50.harvard.edu/x/2020/psets/4/filter/more/ 我有下面的代码,它返回错误
helpers.c:205:216:运行时错误:索引 601 超出类型“RGBTRIPLE [width]”的范围
在一行
RGBTRIPLE array[] = {larger_image[i][j], larger_image[i][j + 1], larger_image[i][j + 2],
larger_image[i + 1][j], larger_image[i + 1][j + 1], larger_image[i + 1][j + 2],
larger_image[i + 2][j], larger_image[i + 2][j + 1], larger_image[i + 2][j + 2]};
我的代码的逻辑是创建一个新的图像,larger_image,其轮廓的 RGB 值为零。这样,就可以很容易地计算出sobel算子。 然后我创建另一个类型为 RGBTRIPLE 的 9 个元素的数组,这样我就可以循环添加他盒子的 sumproduct。但是错误来了,我检查了 i 和 j 边界,似乎还可以...
有人可以帮我看看为什么这里的数组有错误吗?
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE(*edged)[width] = calloc(height, (width) * sizeof(RGBTRIPLE));
**// create another mirror image with size of height +2 and width + 2 of outline RGB numbers zero**
RGBTRIPLE(*larger_image)[width] = calloc(height + 2, (width + 2) * sizeof(RGBTRIPLE));
int i =0;
int j = 0;
float blue_final, green_final, red_final;
for (i = 1; i < height +1; i++)
{
for (j = 1; j < width + 1; j++)
{
larger_image[i][j] = image[i-1][j-1];
}
}
int gx[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
int gy[] = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
int blue_x = 0, blue_y = 0, green_x = 0, green_y = 0, red_x = 0, red_y = 0;
**//create an array of 9 elements easier for me to loop the sumproduct with gx/gy below, error here.**
RGBTRIPLE array[] = {larger_image[i][j], larger_image[i][j + 1], larger_image[i][j + 2],
larger_image[i + 1][j], larger_image[i + 1][j + 1], larger_image[i + 1][j + 2],
larger_image[i + 2][j], larger_image[i + 2][j + 1], larger_image[i + 2][j + 2]};
for (int k = 0; k < 9; k++)
{
blue_x += array[k].rgbtBlue * gx[k];
blue_y += array[k].rgbtBlue * gy[k];
green_x += array[k].rgbtGreen * gx[k];
green_y += array[k].rgbtGreen * gy[k];
red_x += array[k].rgbtRed * gx[k];
red_y += array[k].rgbtRed * gy[k];
}
blue_final = sqrt(pow(blue_x, 2) + pow(blue_y, 2));
if(blue_final > 255.0)
{
blue_final = 255.0;
}
red_final = sqrt(pow(red_x, 2) + pow(red_y, 2));
if(red_final > 255.0)
{
red_final = 255.0;
}
green_final = sqrt(pow(green_x, 2) + pow(green_y, 2));
if(green_final > 255.0)
{
green_final = 255.0;
}
edged[i][j].rgbtBlue = round(blue_final);
edged[i][j].rgbtGreen = round(green_final);
edged[i][j].rgbtRed = round(red_final);
}
}
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
image[i][j] = edged[i][j];
}
}
return;
}
【问题讨论】:
-
在哪里释放分配的内存?
-
与问题无关,但不需要您的
edged数组。在进行计算之前,您已经复制了初始图像。然后就可以直接将结果存入image。 -
是的,你是对的,我需要释放内存,不需要边缘......很好的帮助