C 边缘检测不工作!2D像素数组中的最后一个下标显示不正确
我为粗心的代码或格式错误的问题道歉,这是我的第一篇文章 这段代码的基本要点是使用sobel算子进行边缘检测。 我有一个嵌套在for循环中的for循环来迭代各个像素,还有一个嵌套的for循环来迭代较小的2D数组来应用sobel操作符,但是最后一行像素和一些右边缘没有正确解析。 非常感谢C 边缘检测不工作!2D像素数组中的最后一个下标显示不正确,c,image-processing,cs50,sobel,C,Image Processing,Cs50,Sobel,我为粗心的代码或格式错误的问题道歉,这是我的第一篇文章 这段代码的基本要点是使用sobel算子进行边缘检测。 我有一个嵌套在for循环中的for循环来迭代各个像素,还有一个嵌套的for循环来迭代较小的2D数组来应用sobel操作符,但是最后一行像素和一些右边缘没有正确解析。 非常感谢 #include <math.h> #include "helpers.h" //prototype functions void calcVert (int h, int w,
#include <math.h>
#include "helpers.h"
//prototype functions
void calcVert (int h, int w, int height, int width, RGBTRIPLE temp[height][width], int *blue, int *green, int *red);
void calcHori (int h, int w, int height, int width, RGBTRIPLE temp[height][width], int *blue, int *green, int *red);
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
int sobelBlueV;
int sobelGreenV;
int sobelRedV;
int sobelBlueH;
int sobelGreenH;
int sobelRedH;
RGBTRIPLE temp[height][width];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
temp[h][w].rgbtBlue = image[h][w].rgbtBlue;
temp[h][w].rgbtGreen = image[h][w].rgbtGreen;
temp[h][w].rgbtRed = image[h][w].rgbtRed;
}
}
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
/*
[-1] [0] [+1] [+1] [+2] [+1]
[-2] [0] [+2] [0 ] [0 ] [0 ]
[-1] [0] [+1] [-1] [-2] [-1]
*/
calcVert(h, w, height, width, temp, &sobelBlueV, &sobelGreenV, &sobelRedV);
calcHori(h, w, height, width, temp, &sobelBlueH, &sobelGreenH, &sobelRedH);
// ^2 and sqrt
int bluePoint = round(sqrt(pow(sobelBlueV, 2) + pow(sobelBlueH, 2)));
if (bluePoint > 255) bluePoint = 255;
int GreenPoint = round(sqrt(pow(sobelGreenV, 2) + pow(sobelGreenH, 2)));
if (GreenPoint > 255) GreenPoint = 255;
int RedPoint = round(sqrt(pow(sobelRedV, 2) + pow(sobelRedH, 2)));
if (RedPoint > 255) RedPoint = 255;
image[h][w].rgbtBlue = bluePoint;
image[h][w].rgbtGreen = GreenPoint;
image[h][w].rgbtRed = RedPoint;
}
}
}
void calcVert (int h, int w, int height, int width, RGBTRIPLE temp[height][width], int *blue, int *green, int *red)
{
//row 1: [-1] [0] [+1]
//row 2: [-2] [0] [+2]
//row 3: [-1] [0] [+1] vertical kernel
int cellCalc = 0;
int totalRed = 0;
int totalBlue = 0;
int totalGreen = 0;
//row 1
for (int relativeH = -1; relativeH <= 1; relativeH++)
{
for (int relativeW = -1; relativeW <= 1; relativeW++)
{
//overreach cases
if (h + relativeH < 0 && w + relativeW < 0)//top left corner
{
//set cell values
if (relativeH == 0 && relativeW == -1) cellCalc = 0;
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 0;
if (relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 1 && relativeW == 1) cellCalc = 1;
if (relativeH == 0 && relativeW ==1) cellCalc = 2;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (h + relativeH < 0 && w + relativeW >= width)//top right corner
{
//set cell values
if (relativeH == 0 && relativeW == -1) cellCalc = -2;
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = -1;
if (relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 1 && relativeW == 1) cellCalc = 0;
if (relativeH == 0 && relativeW ==1) cellCalc = 0;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (h + relativeH < 0)//top edge
{
//set cell values
if (relativeH == 0 && relativeW == -1) cellCalc = -2;
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = -1;
if (relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 1 && relativeW == 1) cellCalc = 1;
if (relativeH == 0 && relativeW ==1) cellCalc = 2;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (w + relativeW < 0 && h + relativeH >= height)//bottom left corner
{
//set cell values
if (relativeH == 0 && relativeW == -1) cellCalc = 0;
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 0;
if (relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 1;
if (relativeH == 1 && relativeW == 1) cellCalc = 0;
if (relativeH == 0 && relativeW ==1) cellCalc = 2;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (w + relativeW < 0)//left edge
{
//set cell values
if (relativeH == 0 && relativeW == -1) cellCalc = 0;
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 0;
if (relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 1;
if (relativeH == 1 && relativeW == 1) cellCalc = 1;
if (relativeH == 0 && relativeW ==1) cellCalc = 2;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (h + relativeH >= height && w + relativeW >= width)//bottom right corner
{
//set cell values
if (relativeH == 0 && relativeW == -1) cellCalc = -2;
if (relativeH == -1 && relativeW == -1) cellCalc = -1;
if (relativeH == 1 && relativeW == -1) cellCalc = 0;
if (relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 1 && relativeW == 1) cellCalc = 0;
if (relativeH == 0 && relativeW ==1) cellCalc = 0;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (w + relativeW >= width)//right edge
{
//set cell values
if (relativeH == 0 && relativeW == -1) cellCalc = -2;
if (relativeH == -1 && relativeW == -1) cellCalc = -1;
if (relativeH == 1 && relativeW == -1) cellCalc = -1;
if (relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 1 && relativeW == 1) cellCalc = 0;
if (relativeH == 0 && relativeW ==1) cellCalc = 0;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else //valid cases
{
//set cell values
if (relativeH == 0 && relativeW == -1) cellCalc = -2;
if (relativeH == -1 && relativeW == -1) cellCalc = -1;
if (relativeH == 1 && relativeW == -1) cellCalc = -1;
if (relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 1;
if (relativeH == 1 && relativeW == 1) cellCalc = 1;
if (relativeH == 0 && relativeW ==1) cellCalc = 2;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
}
}
}
void calcHori (int h, int w, int height, int width, RGBTRIPLE temp[height][width], int *blue, int *green, int *red)
{
//row 1: [+1] [+2] [+1]
//row 2: [ 0] [ 0] [ 0]
//row 3: [-1] [-2] [-1]
int cellCalc = 0;
int totalRed = 0;
int totalBlue = 0;
int totalGreen = 0;
//row 1
for (int relativeH = -1; relativeH <= 1; relativeH++)
{
for (int relativeW = -1; relativeW <= 1; relativeW++)
{
//overreach cases
if (h + relativeH < 0 && w + relativeW < 0)//top left corner
{
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == -1 && relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 0;
if (relativeH == 1 && relativeW == 0) cellCalc = 2;
if (relativeH == 1 && relativeW == 1) cellCalc = 1;
}
else if (h + relativeH < 0 && w + relativeW >= width)//top right corner
{
//set cell values
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == -1 && relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 1;
if (relativeH == 1 && relativeW == 0) cellCalc = 2;
if (relativeH == 1 && relativeW == 1) cellCalc = 0;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (h + relativeH < 0)//top edge
{
//set cell values
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == -1 && relativeW == 0) cellCalc = 0;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 1;
if (relativeH == 1 && relativeW == 0) cellCalc = 2;
if (relativeH == 1 && relativeW == 1) cellCalc = 1;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (w + relativeW < 0 && h + relativeH >= height)//bottom left corner
{
//set cell values
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == -1 && relativeW == 0) cellCalc = -2;
if (relativeH == -1 && relativeW == 1) cellCalc = -1;
if (relativeH == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 0;
if (relativeH == 1 && relativeW == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == 1) cellCalc = 0;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (w + relativeW < 0)//left edge
{
//set cell values
if (relativeH == -1 && relativeW == -1) cellCalc = 0;
if (relativeH == -1 && relativeW == 0) cellCalc = -2;
if (relativeH == -1 && relativeW == 1) cellCalc = -1;
if (relativeH == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 0;
if (relativeH == 1 && relativeW == 0) cellCalc = 2;
if (relativeH == 1 && relativeW == 1) cellCalc = 1;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (h + relativeH >= height && w + relativeW >= width)//bottom right corner
{
//set cell values
if (relativeH == -1 && relativeW == -1) cellCalc = -1;
if (relativeH == -1 && relativeW == 0) cellCalc = -2;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 0;
if (relativeH == 1 && relativeW == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == 1) cellCalc = 0;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else if (w + relativeW >= width)//right edge
{
//set cell values
if (relativeH == -1 && relativeW == -1) cellCalc = -1;
if (relativeH == -1 && relativeW == 0) cellCalc = -2;
if (relativeH == -1 && relativeW == 1) cellCalc = 0;
if (relativeH == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 1;
if (relativeH == 1 && relativeW == 0) cellCalc = 2;
if (relativeH == 1 && relativeW == 1) cellCalc = 0;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
else //valid cases
{
//set cell values
if (relativeH == -1 && relativeW == -1) cellCalc = -1;
if (relativeH == -1 && relativeW == 0) cellCalc = -2;
if (relativeH == -1 && relativeW == 1) cellCalc = -1;
if (relativeH == 0) cellCalc = 0;
if (relativeH == 1 && relativeW == -1) cellCalc = 1;
if (relativeH == 1 && relativeW == 0) cellCalc = 2;
if (relativeH == 1 && relativeW == 1) cellCalc = 1;
totalBlue += ((temp[h + relativeH][w + relativeW].rgbtBlue) * cellCalc);
totalGreen += ((temp[h + relativeH][w + relativeW].rgbtGreen) * cellCalc);
totalRed += ((temp[h + relativeH][w + relativeW].rgbtRed) * cellCalc);
*blue = totalBlue;
*green = totalGreen;
*red = totalRed;
}
}
}
}
#包括
#包括“helpers.h”
//原型函数
void calcVert(整数高、整数宽、整数高、整数宽、RGB三重温度[高度][宽度]、整数*蓝色、整数*绿色、整数*红色);
void calcHori(内部高度、内部宽度、内部高度、内部宽度、RGB三重温度[高度][宽度]、内部*蓝色、内部*绿色、内部*红色);
//检测边缘
空边(整数高度、整数宽度、RGB三重图像[高度][宽度])
{
内特索贝尔布鲁夫;
索贝尔格林夫国际酒店;
int sobelRedV;
int sobelBlueH;
内索贝尔格林;
int sobelRedH;
RGB三脚架温度[高度][宽度];
对于(int h=0;h255)蓝点=255;
int绿点=圆形(sqrt(pow(sobelGreenV,2)+pow(sobelGreenH,2));
如果(绿点>255)绿点=255;
int RedPoint=圆形(sqrt(pow(sobelRedV,2)+pow(sobelRedH,2));
如果(红点>255)红点=255;
图像[h][w].rgbtBlue=bluePoint;
图像[h][w]。rgbtGreen=绿点;
图像[h][w]。rgbtRed=红点;
}
}
}
无效calcVert(整数高、整数宽、整数高、整数宽、RGB三重温度[高度][宽度]、整数*蓝色、整数*绿色、整数*红色)
{
//第1行:[-1][0][+1]
//第二行:[-2][0][+2]
//第3行:[-1][0][+1]垂直内核
int-cellCalc=0;
int totalRed=0;
int totalBlue=0;
int totalGreen=0;
//第1行
for(int relativeH=-1;relativeH=height)//左下角
{
//设置单元格值
如果(relativeH==0&&relativeW==1)cellCalc=0;
如果(relativeH==-1&&relativeW=-1)cellCalc=0;
如果(relativeH==1&&relativeW==1)cellCalc=0;
如果(relativeW==0)cellCalc=0;
如果(relativeH==-1&&relativeW==1)cellCalc=1;
如果(relativeH==1&&relativeW==1)cellCalc=0;
如果(relativeH==0&&relativeW==1)cellCalc=2;
totalBlue+=((温度[h+relativeH][w+RelativeEW].rgbtBlue)*cellCalc);
总绿色+=((温度[h+相对湿度][w+相对湿度].rgbtGreen)*cellCalc);
totalRed+=((温度[h+relativeH][w+RelativeEW].rgbtRed)*cellCalc);
*蓝色=总蓝色;
*绿色=总绿色;
*红色=总红色;
}
else如果(w+relativeW<0)//左边缘
{
//设置单元格值
如果(relativeH==0&&relativeW==1)cellCalc=0;
如果(relativeH==-1&&relativeW=-1)cellCalc=0;
如果(relativeH==1&&relativeW==1)cellCalc=0;
如果(relativeW==0)cellCalc=0;
如果(relativeH==-1&&relativeW==1)cellCalc=1;
如果(relativeH==1&&relativeW==1)cellCalc=1;
如果(relativeH==0&&relativeW==1)cellCalc=2;
totalBlue+=((温度[h+relativeH][w+RelativeEW].rgbtBlue)*cellCalc);
总绿色+=((温度[h+相对湿度][w+相对湿度].rgbtGreen)*cellCalc);
totalRed+=((温度[h+relativeH][w+RelativeEW].rgbtRed)*cellCalc);
*蓝色=总蓝色;
*绿色=总绿色;
*红色=总红色;
}
如果(h+relativeH>=高度和&w+RelativeEW>=宽度)//右下角
{
//设置单元格值
如果(relativeH==0&&relativeW==1)cellCalc=-2;
如果(relativeH==-1&&relativeW=-1)cellCalc=-1;
如果(relativeH==1&&relativeW==1)cellCalc=0;
如果(relativeW==0)cellCalc=0;
如果(relativeH==-1&&relativeW==1)cellCalc=0;
如果(relativeH==1&&relativeW==1)cellCalc=0;
如果(relativeH==0&&relativeW==1)cellCalc=0;
totalBlue+=((温度[h+relativeH][w+RelativeEW].rgbtBlue)*cellCalc);
总绿色+=((温度[h+相对湿度][w+相对湿度].rgbtGreen)*cellCalc);
totalRed+=((温度[h+relativeH][w+RelativeEW].rgbtRed)*cellCalc);
*蓝色=总蓝色;
*绿色=总绿色;
*红色=总红色;
}
如果(w+RelativeEW>=宽度)//右边缘
{
//设置单元格值
如果(relativeH==0&&relativeW==1)cellCalc=-2;
如果(relativeH==-1&&relativeW=-1)cellCalc=-1;