CS50 Pset4更少过滤

时间:2020-11-12 00:23:31

标签: cs50

由于某些原因我的代码无法正常工作,您能帮我吗

#include "helpers.h"

#include

//将图像转换为灰度

void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
          for (int j = 0; j < width; j++)
          {
              RGBTRIPLE pixel = image [i][j];
       
              int average = round((pixel.rgbtBlue + pixel.rgbtRed + pixel.rgbtGreen) /3.0);
              image[i][j].rgbtRed = image[i][j].rgbtBlue = image[i][j].rgbtGreen = average;       
       
          }
    }
return;

}

//将图像转换为棕褐色

void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            RGBTRIPLE pixel = image [i][j];
        
            int sepiaRed = round(.393 * pixel.rgbtRed + .769 * pixel.rgbtGreen + .189 * pixel.rgbtBlue);
            int sepiaGreen = round(.349 * pixel.rgbtRed + .686 * pixel.rgbtGreen + .168 * pixel.rgbtBlue);
            int sepiaBlue = round(.272 * pixel.rgbtRed + .534 * pixel.rgbtGreen + .131 * pixel.rgbtBlue);
        
            image[i][j].rgbtRed = sepiaRed > 255 ? 255 : sepiaRed;
            image[i][j].rgbtGreen = sepiaGreen > 255 ? 255 : sepiaGreen;
            image[i][j].rgbtBlue = sepiaBlue > 255 ? 255 : sepiaBlue;
        }
    }
    return;
}

//水平反射图像

void reflect(int height, int width, RGBTRIPLE image[height][width])
{

    RGBTRIPLE temp[height][width];

    for(int c = 0; c < height; c++)
    {
        for (int f = 0; f < (width/2); f++)
        {
            int red = image[c][f].rgbtRed;
            int green = image[c][f].rgbtGreen;
            int blue = image[c][f].rgbtBlue;
        
        
            image[c][f].rgbtRed = image[c][width - f - 1].rgbtRed;
            image[c][f].rgbtGreen = image[c][width - f - 1].rgbtGreen;
            image[c][f].rgbtBlue = image[c][width - f - 1].rgbtBlue;
        
            image[c][width - f - 1].rgbtRed = red;
            image[c][width - f - 1].rgbtGreen = blue;
            image[c][width - f - 1].rgbtBlue = green;
        }
    }
    return;

}

//模糊图像

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];

    for (int row = 0; row < width; row++)
    {
        int pos = 0;
        for (int col = 0; col < height; col++)
        {
            int count = 0;
            int xc[] = { row-1, row, row+1 };
            int yc[] = { col-1, col, col+1 };
            float totalR = 0, totalG = 0, totalB = 0;
            for (int r = 0; r < 3; r++)
            {
                for (int c = 0; c < 3; c++)
                {
                    int curr = xc[r];
                    int curc = yc[c];
                
                    if (curr >= 0 && curr < height && curc >= 0 && curc < width)
                    {
                       RGBTRIPLE pixel = image[curr][curc];
                       totalR += image[row][col].rgbtRed;
                       totalG += image[row][col].rgbtGreen;
                       totalB += image[row][col].rgbtBlue;                       
                       count++;
                     
                    }
                }
            }
            temp[row][col].rgbtRed = round(totalR / count);
            temp[row][col].rgbtGreen = round(totalG / count);
            temp[row][col].rgbtBlue = round(totalB / count);



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



    return;
    }
}

}

错误:

:) helpers.c存在 :)过滤器编译 :)灰度可以正确过滤具有整数平均值的单个像素 :)灰度可正确过滤单个像素而无整数平均值 :)灰度留下已经是灰色的像素 :)灰度可以正确过滤简单的3x3图像 :)灰度可以正确过滤更复杂的3x3图像 :)灰度正确过滤4x4图像 :)棕褐色正确过滤单个像素 :)棕褐色正确过滤了简单的3x3图像 :)棕褐色可以正确过滤更复杂的3x3图像 :)棕褐色正确过滤4x4图像 :)正确反映滤镜1x2图像 :)正确反映滤镜1x3图像 :(正确反映过滤器图像,这是它自己的镜像 预期为“ 255 0 0 \ n255 0 ...”,而不是“ 255 0 0 \ n255 0 ...” :(正确反映滤镜3x3图像 应该是“ 70 80 90 \ n40 5 ...”,而不是“ 70 80 90 \ n40 5 ...” :(正确反映滤镜4x4图像 应该是“ 100110120 \ n7 ...”,而不是“ 100110120 \ n7 ...” :(正确模糊过滤中间像素 预期为“ 127140149 \ n”,而不是“ 120140150 \ n” :(模糊正确过滤边缘上的像素 预期为“ 80 95 105 \ n”,而不是“ 40 50 60 \ n” :(正确模糊过滤掉角落的像素 应该是“ 70 85 95 \ n”,而不是“ 10 20 30 \ n” :(正确模糊过滤3x3图像 应该是“ 70 85 95 \ n80 9 ...”,而不是“ 10 20 30 \ n40 5 ...” :(正确模糊过滤4x4图像 应该是“ 70 85 95 \ n80 9 ...”,而不是“ 10 20 30 \ n40 5 ...”

0 个答案:

没有答案