Cs50的问题集4-较少过滤-模糊功能

如何解决Cs50的问题集4-较少过滤-模糊功能

因此,我一直在努力纠正我的代码以修复pset4的模糊功能,并觉得我的代码是正确的(即使效率不是最佳)。

这是我的处理方式:

  1. 首先,我循环遍历了股票图像的高度和宽度 2个循环。

  2. 将pixelcounter设置为0以计算周围的有效像素数 第i行第j个像素定义的任何特定像素。

  3. 我设置了两个for循环以遍历3x3网格状结构 有问题的像素周围。这里的变量是k和 l。 K必须在i上方1行(因此k = i-1),在i下方结束1行 (因此k

  4. 在“ 3”中所述的for循环内。我用一个“ if循环”来确定 3x3网格中是否存在迭代的像素- 指出k必须大于-1且小于“高度”,并且 同上l。

  5. pixelcounter ++,可将其添加到周围的有效像素总数中 [i] [j]个像素。

  6. 退出混乱的循环,以确保对所有像素进行计数 pixelcolour声明为pixelcounter的大小。

  7. 我使用了与第3步中相同的循环来遍历3x3 第[i] [j]个像素周围的像素网格,如果 健康)状况。只有这次我使用z作为嵌套在for循环中 我的pixelcounter,这样它就可以遍历一维数组 pixelcolor,将其颜色存储在zth位置 如果有效,请在[k] [l]处显示RGB颜色。

  8. 我声明了3个变量-rawred,rawblue,rawgreen,目的是 只是对红色,绿色和蓝色的值求和。

  9. 让循环执行第8步。

  10. 然后我用初始化RGB分量的平均值 平均/平均蓝色/平均绿色,带有未分类的红色/原始蓝色/原始绿色的花车 由pixelcounter转换为浮点数。取整结果给出 整数值。

  11. 然后我将那些整数值输入到第[i] [j]个像素 像素。

代码如下:

void blur(int height,int width,RGBTRIPLE image[height][width])
{
    // Looping through height of the image
    for (int i = 0; i < height; i++)
    {
        // Looping through the individual pixels in each row
        for (int j = 0; j < width; j++)
        {
            int pixelcounter = 0;
            // Looping through a 3x3 pixel grid surrounding of the individual pixel - Height
            for (int k = i - 1; k <= i + 1; k++)
            {
                // Looping through individual pixels within the kth row
                for (int l = j - 1; l <= j + 1; l++)
                {
                    // Counting the number of valid pixels in the 3x3 grid
                    if ((k > -1) && (k < height) && (l > -1) && (l < width))
                    {
                        pixelcounter++;
                    }
                }
            }
            
            RGBTRIPLE pixelcolour[pixelcounter];
            // Looping through array 3x3 pixel grid surrounding the individual pixel - height
            for (int z = 0; z < pixelcounter; z++)
            {
                for (int k = i - 1; k <= i + 1; k++)
                {
                    for (int l = j - 1; l <= j + 1; l++)
                    {
                        // Storing valid pixels in an array of valid pixels
                        if ((k > -1) && (k < height) && (l > -1) && (l < width))
                        {
                        
                            pixelcolour[z] = image[k][l];
                        
                        }
                    }
                }
            }
            
            // adding all RGB components
            float rawred = 0;
            float rawblue = 0;
            float rawgreen = 0;
            for (int a = 0; a < pixelcounter; a++)
            {
                rawred = rawred + pixelcolour[a].rgbtRed;
                rawblue = rawblue + pixelcolour[a].rgbtBlue;
                rawgreen = rawgreen + pixelcolour[a].rgbtGreen;
            }
            
            // Calculating average values of RGB component
            int avgred = round(rawred / (float) pixelcounter);
            int avgblue = round(rawblue / (float) pixelcounter);
            int avggreen = round(rawgreen / (float) pixelcounter);
            
            // Dereferencing original pixel colour to new colour
            image[i][j].rgbtRed = avgred;
            image[i][j].rgbtBlue = avgblue;
            image[i][j].rgbtGreen = avggreen;
        }
    }

    return;
}

P.S:我知道可能有一种更有效的方法来执行此操作,但是我真的很想看看我在这段代码中到底出了什么问题。编译后,最终结果图片奇怪地移到了左角。没有任何东西会变得模糊,整个图片会移动一个像素,并且不应该有像素出现(杂色的随机像素)。

编辑1: 以下是我收到的错误:

:( blur correctly filters middle pixel
    expected "127 140 149\n",not "145 160 169\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n",not "90 106 116\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...",not "70 85 95\n90 1..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...",not "70 85 95\n90 1..."

解决方法

解决了仍然困扰我的问题。我意识到模糊功能受到周围模糊像素的影响,解决方案是使用临时数组创建图像的副本。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-