Filter 为什么过滤器(更多)CS50中的边3x3和4x4图像有错误? #包括“helpers.h” #包括 //将图像转换为灰度 无效灰度(整数高度、整数宽度、RGB三重图像[高度][宽度]) { 浮动rgbGray; 对于(int i=0;i

Filter 为什么过滤器(更多)CS50中的边3x3和4x4图像有错误? #包括“helpers.h” #包括 //将图像转换为灰度 无效灰度(整数高度、整数宽度、RGB三重图像[高度][宽度]) { 浮动rgbGray; 对于(int i=0;i,filter,cs50,Filter,Cs50,为什么过滤器(更多)CS50中的边3x3和4x4图像有错误? #包括“helpers.h” #包括 //将图像转换为灰度 无效灰度(整数高度、整数宽度、RGB三重图像[高度][宽度]) { 浮动rgbGray; 对于(int i=0;i

为什么过滤器(更多)CS50中的边3x3和4x4图像有错误?
#包括“helpers.h”
#包括
//将图像转换为灰度
无效灰度(整数高度、整数宽度、RGB三重图像[高度][宽度])
{
浮动rgbGray;
对于(int i=0;i255)
{
RGB=255;
}
返回RGB;
}
//将图像转换为深褐色
void-sepia(内部高度、内部宽度、RGB三重图像[高度][宽度])
{
int-sepiblue;
国际秘书处;
int-sepiaGreen;
对于(int i=0;i=高度| | l>=宽度)
{
继续;
}
如果(颜色位置==0)
{
sum+=图像[k][l].rgbtRed;
}
else if(颜色位置==1)
{
sum+=图像[k][l].rgbt绿色;
}
其他的
{
sum+=图像[k][l].rgbtBlue;
}
计数器++;
}
}
返回回合(总和/计数器);
}
//模糊图像
无效模糊(整数高度、整数宽度、RGB三重图像[高度][宽度])
{
RGB三份拷贝[高度][宽度];
对于(int i=0;i#include "helpers.h"
#include <math.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    float rgbGray;

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

            rgbGray = round( (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed)/ 3.00);

            image[i][j].rgbtBlue = rgbGray;
            image[i][j].rgbtGreen = rgbGray;
            image[i][j].rgbtRed = rgbGray;

        }
    }
    return;
}

int limit(int RGB)
{
    if (RGB > 255)
    {
        RGB = 255;
    }
    return RGB;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    int sepiaBlue;
    int sepiaRed;
    int sepiaGreen;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            sepiaBlue = limit(round(0.272 * image[i][j].rgbtRed + 0.534 * image[i][j].rgbtGreen + 0.131 * image[i][j].rgbtBlue));
            sepiaGreen = limit(round(0.349 * image[i][j].rgbtRed + 0.686 * image[i][j].rgbtGreen + 0.168 * image[i][j].rgbtBlue));
            sepiaRed = limit(round(0.393 * image[i][j].rgbtRed + 0.769 * image[i][j].rgbtGreen + 0.189 * image[i][j].rgbtBlue));

            image[i][j].rgbtBlue = sepiaBlue;
            image[i][j].rgbtGreen = sepiaGreen;
            image[i][j].rgbtRed = sepiaRed;
        }
    }
    return;
}

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int temp[3];
     for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width / 2; j++) {

            /** Swap pixels from left to right */
            temp[0] = image[i][j].rgbtBlue;
            temp[1] = image[i][j].rgbtGreen;
            temp[2] = image[i][j].rgbtRed;

            image[i][j].rgbtBlue = image[i][width - j - 1].rgbtBlue;
            image[i][j].rgbtGreen = image[i][width - j - 1].rgbtGreen;
            image[i][j].rgbtRed = image[i][width - j - 1].rgbtRed;

            image[i][width - j - 1].rgbtBlue = temp[0];
            image[i][width - j - 1].rgbtGreen = temp[1];
            image[i][width - j - 1].rgbtRed = temp[2];

        }
    }
    return;
}

int getBlur(int i, int j, int height, int width, RGBTRIPLE image[height][width] , int color_position)
{
    float counter = 0;
    int sum = 0;

    /** Start from 1 row before it and end at 1 row after it- total of 3rows */
    for (int k = i - 1; k <  (i + 2); k++)
    {
        /** Start from 1 block before it and end at 1 block after it- total of 3blocks */
        for (int l = j - 1; l < (j + 2); l ++)
        {
            if(k < 0 || l < 0 || k >= height || l >= width)
            {
                continue;
            }
            if (color_position == 0)
            {
                sum += image[k][l].rgbtRed;
            }
            else if (color_position == 1)
            {
                sum += image[k][l].rgbtGreen;
            }
            else
            {
                sum += image[k][l].rgbtBlue;
            }
            counter++;

        }
    }
    return round(sum /counter);
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            copy[i][j] = image[i][j];
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = getBlur(i, j, height, width, copy, 0);
            image[i][j].rgbtGreen = getBlur(i, j, height, width, copy, 1);
            image[i][j].rgbtBlue = getBlur(i, j, height, width, copy, 2);
        }
    }
    return;
}

// Detect edges
int getEdge(int i, int j, int height, int width, RGBTRIPLE image[height][width], int color)
{
    float suma = 0.0;
    float sumb = 0.0;
    for (int k = (i - 1); k <= (i + 1); k++)
    {
        if ( k < 0 || k > height)
        {
            continue;
        }
        for (int l = (j - 1); l <= (j + 1); l++)
        {
            if (l < 0 || l > width)
            {
                continue;
            }
            switch (color)
            {
                case 0:
                    // Gx
                    if ((l == (j - 1)) && (k == (i - 1)))
                    {
                        suma += (image[k][l].rgbtRed * -1);
                    }
                    else if (l == j)
                    {
                        suma += (image[k][l].rgbtRed * 0);
                    }
                    else if ((l == (j + 1)) && (k == (i - 1)))
                    {
                        suma += (image[k][l].rgbtRed * 1);
                    }
                    else if ((l == (j - 1)) && (k == i))
                    {
                        suma += (image[k][l].rgbtRed * -2);
                    }
                    else if ((l == (j + 1)) && (k == i))
                    {
                        suma += (image[k][l].rgbtRed * 2);
                    }
                    else if ((l == (j - 1)) && (k == (i + 1)))
                    {
                        suma += (image[k][l].rgbtRed * -1);
                    }
                    else if ((l == (j + 1)) && (k == (i + 1)))
                    {
                        suma += (image[k][l].rgbtRed * 1);
                    }
                    // Gy
                    if ((l == (j - 1)) && (k == (i - 1)))
                    {
                        sumb += (image[k][l].rgbtRed * -1);
                    }
                    else if (k == i)
                    {
                        sumb += (image[k][l].rgbtRed * 0);
                    }
                    else if ((l == (j + 1)) && (k == (i - 1)))
                    {
                        sumb += (image[k][l].rgbtRed * -1);
                    }
                    else if ((l == j) && (k == (i - 1)))
                    {
                        sumb += (image[k][l].rgbtRed * -2);
                    }
                    else if ((l == (j + 1)) && (k == (i + 1)))
                    {
                        sumb += (image[k][l].rgbtRed * 1);
                    }
                    else if ((l == (j - 1)) && (k == (i + 1)))
                    {
                        sumb += (image[k][l].rgbtRed * 1);
                    }
                    else if ((l == j) && (k == (i + 1)))
                    {
                        sumb += (image[k][l].rgbtRed * 2);
                    }
                    break;
                case 1:
                    // Gx
                    if ((l == (j - 1)) && (k == (i - 1)))
                    {
                        suma += (image[k][l].rgbtGreen * -1);
                    }
                    else if (l == j)
                    {
                        suma += (image[k][l].rgbtGreen * 0);
                    }
                    else if ((l == (j + 1)) && (k == (i - 1)))
                    {
                        suma += (image[k][l].rgbtGreen * 1);
                    }
                    else if ((l == (j - 1)) && (k == i))
                    {
                        suma += (image[k][l].rgbtGreen * -2);
                    }
                    else if ((l == (j + 1)) && (k == i))
                    {
                        suma += (image[k][l].rgbtGreen * 2);
                    }
                    else if ((l == (j - 1)) && (k == (i + 1)))
                    {
                        suma += (image[k][l].rgbtGreen * -1);
                    }
                    else if ((l == (j + 1)) && (k == (i + 1)))
                    {
                        suma += (image[k][l].rgbtGreen * 1);
                    }
                    // Gy
                    if ((l == (j - 1)) && (k == (i - 1)))
                    {
                        sumb += (image[k][l].rgbtGreen * -1);
                    }
                    else if (k == i)
                    {
                        sumb += (image[k][l].rgbtGreen * 0);
                    }
                    else if ((l == (j + 1)) && (k == (i - 1)))
                    {
                        sumb += (image[k][l].rgbtGreen * -1);
                    }
                    else if ((l == j) && (k == (i - 1)))
                    {
                        sumb += (image[k][l].rgbtGreen * -2);
                    }
                    else if ((l == (j + 1)) && (k == (i + 1)))
                    {
                        sumb += (image[k][l].rgbtGreen * 1);
                    }
                    else if ((l == (j - 1)) && (k == (i + 1)))
                    {
                        sumb += (image[k][l].rgbtGreen * 1);
                    }
                    else if ((l == j) && (k == (i + 1)))
                    {
                        sumb += (image[k][l].rgbtGreen * 2);
                    }
                    break;
                case 2:
                    // Gx
                    if ((l == (j - 1)) && (k == (i - 1)))
                    {
                        suma += (image[k][l].rgbtBlue * -1);
                    }
                    else if (l == j)
                    {
                        suma += (image[k][l].rgbtBlue * 0);
                    }
                    else if ((l == (j + 1)) && (k == (i - 1)))
                    {
                        suma += (image[k][l].rgbtBlue * 1);
                    }
                    else if ((l == (j - 1)) && (k == i))
                    {
                        suma += (image[k][l].rgbtBlue * -2);
                    }
                    else if ((l == (j + 1)) && (k == i))
                    {
                        suma += (image[k][l].rgbtBlue * 2);
                    }
                    else if ((l == (j - 1)) && (k == (i + 1)))
                    {
                        suma += (image[k][l].rgbtBlue * -1);
                    }
                    else if ((l == (j + 1)) && (k == (i + 1)))
                    {
                        suma += (image[k][l].rgbtBlue * 1);
                    }
                    // Gy
                    if ((l == (j - 1)) && (k == (i - 1)))
                    {
                        sumb += (image[k][l].rgbtBlue * -1);
                    }
                    else if (k == i)
                    {
                        sumb += (image[k][l].rgbtBlue * 0);
                    }
                    else if ((l == (j + 1)) && (k == (i - 1)))
                    {
                        sumb += (image[k][l].rgbtBlue * -1);
                    }
                    else if ((l == j) && (k == (i - 1)))
                    {
                        sumb += (image[k][l].rgbtBlue * -2);
                    }
                    else if ((l == (j + 1)) && (k == (i + 1)))
                    {
                        sumb += (image[k][l].rgbtBlue * 1);
                    }
                    else if ((l == (j - 1)) && (k == (i + 1)))
                    {
                        sumb += (image[k][l].rgbtBlue * 1);
                    }
                    else if ((l == j) && (k == (i + 1)))
                    {
                        sumb += (image[k][l].rgbtBlue * 2);
                    }
                    break;
            }
        }
    }
    return limit(round(sqrt((suma * suma) + (sumb * sumb))));
}
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j] = image[i][j];
        }
    }
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = getEdge(i, j, height, width, temp, 0);
            image[i][j].rgbtGreen = getEdge(i, j, height, width, temp, 1);
            image[i][j].rgbtBlue = getEdge(i, j, height, width, temp, 2);
        }
    }
    return;
}