当前位置: 首页 > article >正文

pset4filter less: helpers.c

(4)blur function

  • 简单画图熟悉一下要做什么
    在这里插入图片描述
  • 可以看到3种情况,顶格,边界,里面
  • 如果分开算的话,是真的麻烦;但是当时还真的没有想到更好的,就先写一写(此处摘取3处)
  • 外部都是 for (int i = 0; i < height; i++) { 下边省去不写 for (int j = 0; j < width; j++) { } }

里面

int sumRed = 0,sumGreen = 0,sumBlue = 0;
    //set 3 conditions by the limit
         //!前面1部分
        //计算9个格子
         if (i > 0 && i < height - 1 && j > 0 && j < width - 1) {
            for (int k = -1; k <= 1; k++) {
                for (int m = -1; m <= 1; m++) {
                sumRed += copy[i + k][j + m].rgbtRed;
                sumGreen += copy[i + k][j + m].rgbtGreen;
                sumBlue += copy[i + k][j + m].rgbtBlue;
            }
        }
         //after sum it all and give that value to image
        image[i][j].rgbtRed = round(sumRed / 9.0);
        image[i][j].rgbtGreen = round(sumGreen / 9.0);
        image[i][j].rgbtBlue = round(sumBlue / 9.0);
}

顶格

//1.1 左上角 i + j +
    sumRed = 0, sumGreen = 0, sumBlue = 0;
    if (j == 0 && i == 0) {
        for (int k = 0; k <= 1; k++) {
            for (int m = 0; m <= 1; m++) {
                sumRed += copy[i + k][j + m].rgbtRed;
                sumGreen += copy[i + k][j + m].rgbtGreen;
                sumBlue += copy[i + k][j + m].rgbtBlue;
            }
        }
        //after sum it all and give that value to image
        image[i][j].rgbtRed = round(sumRed / 4.0);
        image[i][j].rgbtGreen = round(sumGreen / 4.0);
        image[i][j].rgbtBlue = round(sumBlue / 4.0);
    }

边界

    //2.1上边界:移动的方向只有下(高度-1)k +,左边m -,右边m+
    sumRed = 0, sumGreen = 0, sumBlue = 0;
    if (i == 0) {
        //计算RGB利用双循环(k,m)
        for (int k = 0; k <= 1; k++) {
            for (int m = -1; m <= 1; m++) {
                sumRed += copy[i + k][j + m].rgbtRed;
                sumGreen += copy[i + k][j + m].rgbtGreen;
                sumBlue += copy[i + k][j + m].rgbtBlue;
            }
        }
        image[i][j].rgbtRed = round(sumRed / 6.0);
        image[i][j].rgbtGreen = round(sumGreen / 6.0);
        image[i][j].rgbtBlue = round(sumBlue / 6.0);
        }

简化

  • 然后可以看到很多重复的地方,需要开始统一,找到不同的地方4,6,9,然后里层的双循环k,m其实也可以合并,
  • 只要移动之后的index在范围里面就可以了,里层循环的次数加上限定的条件计算count,然后一步步开始删减…
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // copy the 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];
        }
    }
    int sumRed = 0, sumGreen = 0, sumBlue = 0, count = 0;
    // set 3 conditions by the limit
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {

            for (int k = -1; k <= 1; k++)
            {
                for (int m = -1; m <= 1; m++)
                {
                    // condition limit boundary
                    if (i + k >= 0 && i + k <= height - 1 && j + m >= 0 && j + m <= width - 1)
                    {
                        sumRed += copy[i + k][j + m].rgbtRed;
                        sumGreen += copy[i + k][j + m].rgbtGreen;
                        sumBlue += copy[i + k][j + m].rgbtBlue;
                        count++;
                    }
                }
            }
            // after sum it all and give that value to image
            //before calculating the results,first convert it to float!
            image[i][j].rgbtRed = round((float) sumRed / count);
            image[i][j].rgbtGreen = round((float) sumGreen / count);
            image[i][j].rgbtBlue = round((float) sumBlue / count);
            // clear it and for next round
            sumRed = 0, sumGreen = 0, sumBlue = 0, count = 0;
        }
    }
}

问题

这里要在计算之前,把int sumRGB值转化成float类型

image[i][j].rgbtRed = round((float) sumRed / count);

而不是float(计算之后的值),这样就把小数点之后的都丢了

image[i][j].rgbtRed = round((float)( sumRed / count));

总结

刚开始的时候想不到简洁的方法或者不太理解,先慢慢写出来,能运行就可以。
后面再去优化,一步一步来,不需要太大压力

check

:) helpers.c exists
:) filter compiles
:) grayscale correctly filters single pixel with whole number average
:) grayscale correctly filters single pixel without whole number average
:) grayscale leaves alone pixels that are already gray
:) grayscale correctly filters simple 3x3 image
:) grayscale correctly filters more complex 3x3 image
:) grayscale correctly filters 4x4 image
:) sepia correctly filters single pixel
:) sepia correctly filters simple 3x3 image
:) sepia correctly filters more complex 3x3 image
:) sepia correctly filters 4x4 image
:) reflect correctly filters 1x2 image
:) reflect correctly filters 1x3 image
:) reflect correctly filters image that is its own mirror image
:) reflect correctly filters 3x3 image
:) reflect correctly filters 4x4 image
:) blur correctly filters middle pixel
:) blur correctly filters pixel on edge
:) blur correctly filters pixel in corner
:) blur correctly filters 3x3 image
:) blur correctly filters 4x4 image

http://www.kler.cn/a/443314.html

相关文章:

  • STM32+WIFI获取网络时间+8位数码管显示+0.96OLED显
  • ADO.NET知识总结3---SqlCommand命令对象
  • 反规范化带来的数据不一致问题的解决方案
  • Mac-docker配置
  • QPS和TPS 的区别是什么?QPS 大了会有什么问题,怎么解决?
  • 快速入门Spring Cloud Alibaba,轻松玩转微服务
  • DHTMLX Scheduler 7.2全新发布:增强了重复事件的编辑、修改了实时更新等
  • 【Linux】HTTPS
  • day38-SSH安全登录
  • 安全地使用 Docker 和 Systemctl 部署 Kafka 的综合指南
  • 【机器人】Graspness 端到端抓取点估计 | 环境搭建 | 模型推理测试
  • 利用Java爬虫获取京东商品SKU信息
  • 敏捷多模态微型机器人:独特的被动变形轮设计
  • C# 基本信息介绍
  • Linux高性能服务器编程 | 读书笔记 | 12. 多线程编程
  • 环境变革下 B2B 销售的转型与创新:开源 AI 智能名片与 S2B2C 商城小程序的助力
  • uniapp 极速上手鸿蒙开发
  • 软件信息化平台项目投标技术方案中如何进行项目实施方案以及安全质量方案培训售后方案应急预案的编写?
  • 日常思考笔记
  • 如何处理对象的状态变化?
  • 公文写作一体机实现个性化写作与专属文风定制
  • Spring MVC 中,处理异常的 6种方式
  • 【ELK】Filebeat采集Docker容器日志
  • 华为数通最新题库 H12-821 HCIP稳定过人中
  • LeetCode1143. 最长公共子序列(2024冬季每日一题 36)
  • barin.js(十四)GRU实战教程 - 文本情感分析之有害内容检测