cs50 pset4滤波器边缘检测
我写了一篇新文章,对我的文章进行了重大修改,但现在的问题是,我的图像返回时与输入的图像相同。我相信newimage变量和旧image变量交换是有问题的,但我不知道为什么cs50 pset4滤波器边缘检测,c,filter,cs50,edge-detection,C,Filter,Cs50,Edge Detection,我写了一篇新文章,对我的文章进行了重大修改,但现在的问题是,我的图像返回时与输入的图像相同。我相信newimage变量和旧image变量交换是有问题的,但我不知道为什么 void edges(int height, int width, RGBTRIPLE image[height][width]) { int gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}; int gy[3][3] = {{-1, -2, -1}, {0, 0,
void edges(int height, int width, RGBTRIPLE image[height][width])
{
int gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
RGBTRIPLE newimage[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int redx = 0;
int greenx = 0;
int bluex = 0;
int redy = 0;
int greeny = 0;
int bluey = 0;
for (int k = i - 1; k <= i + 1 && k < height; k++)
{
for (int m = j - 1; m <= j + 1 && m < width; m++)
{
if (k != -1 && m != -1)
{
redx += image[k][m].rgbtRed * gx[k-(i-1)][m-(j-1)];
greenx += image[k][m].rgbtGreen * gx[k-(i-1)][m-(j-1)];
bluex += image[k][m].rgbtBlue * gx[k-(i-1)][m-(j-1)];
redy += image[k][m].rgbtRed * gy[k-(i-1)][m-(j-1)];
greeny += image[k][m].rgbtGreen * gy[k-(i-1)][m-(j-1)];
bluey += image[k][m].rgbtBlue * gy[k-(i-1)][m-(j-1)];
}
}
}
int finalred = round(sqrt((redx * redx) + (redy * redy)));
int finalgreen = round(sqrt((greenx * greenx) + (greeny * greeny)));
int finalblue = round(sqrt((bluex * bluex) + (bluey * bluey)));
if (finalred > 255)
{
finalred = 255;
}
if (finalgreen > 255)
{
finalgreen = 255;
}
if (finalblue > 255)
{
finalblue = 255;
}
newimage[i][j].rgbtRed = finalred;
newimage[i][j].rgbtGreen = finalgreen;
newimage[i][j].rgbtBlue = finalblue;
}
}
image = newimage;
return;
}
void边(整数高度、整数宽度、RGB三重图像[高度][宽度])
{
int gx[3][3]={-1,0,1},{-2,0,2},{-1,0,1};
int-gy[3][3]={{-1,-2,-1},{0,0,0},{1,2,1};
RGBTRIPLE新图像[高度][宽度];
对于(int i=0;i255)
{
最终蓝=255;
}
newimage[i][j].rgbtRed=finalred;
newimage[i][j].rgbtGreen=finalgreen;
newimage[i][j].rgbtBlue=finalblue;
}
}
图像=新图像;
返回;
}
你认为image=newimage
有什么作用?@EricPostpischil使原始图像=编辑后的图像。因此,我将newimage作为临时边缘检测图像,以便在循环过程中使用原始图像的颜色。image
是一个参数。该参数是一个局部变量。当函数启动时,它被初始化为调用例程作为参数传递的值image=newImage
仅更改局部变量的值。它不会改变调用例程中的任何内容。您必须找到其他方法返回指针或其他方法返回图像中的数据。@EricPostFischil您能给我一些提示吗,因为我不知道。在您所处的级别,只需在例程结束时将数据从newimage
复制到image
。如果您知道如何使用memcpy
,您可以使用它。否则,只需使用两个循环来复制每个像素。您认为image=newimage
有什么作用?@EricPostpischil使原始图像=编辑后的图像。因此,我将newimage作为临时边缘检测图像,以便在循环过程中使用原始图像的颜色。image
是一个参数。该参数是一个局部变量。当函数启动时,它被初始化为调用例程作为参数传递的值image=newImage
仅更改局部变量的值。它不会改变调用例程中的任何内容。您必须找到其他方法返回指针或其他方法返回图像中的数据。@EricPostFischil您能给我一些提示吗,因为我不知道。在您所处的级别,只需在例程结束时将数据从newimage
复制到image
。如果您知道如何使用memcpy
,您可以使用它。否则,只需使用两个循环来复制每个像素。