games101 作业4提高部分

games101 作业4提高部分

作业四中,我们按照实验步骤完成bazier曲线之后,得到的结果有一定的锯齿感:

image-20221116152241459

然后pdf中给出的思路是:

对于一个曲线上的点,不只把它对应于一个像素,你需要根据到像素中心的距离来考虑与它相邻的像素的颜色。

思路是用一个点的9邻域到point的距离来对邻域像素的颜色进行一定的设置,当然就是越远越淡。

image-20221116153249906

这个距离最大是 \(3\sqrt2 \over 2\),最小是\(0\),(最大是落在中心像素某一个顶点上,最小是落在中心像素中点。)因此我们可以考虑使用归一化:

\[normal\_distance = {\sqrt2 \over 3}distance \]

代码为:

void bezier(const std::vector<cv::Point2f> &control_points, cv::Mat &window) 
{
    // TODO: Iterate through all t = 0 to t = 1 with small steps, and call de Casteljau's 
    for (float t = 0.0f; t <= 1.0f; t += 0.001f) {
        auto point = recursive_bezier(control_points, t);

        auto centerPoint = cv::Point2f(int(point.x) + 0.5f, int(point.y) + 0.5f);

        auto x = point.x, y = point.y, centerX = centerPoint.x, centerY = centerPoint.y;
        for (int i = -1; i <= 1; i++)
            for (int j = -1; j <= 1; j++) {
                float distance = std::sqrt(std::pow(x - (centerX - i), 2) + std::pow(y - (centerY - j), 2));
                float normal_distance = distance * std::sqrt(2) / 3; 
                float color = 255.0f * (1 - normal_distance);
                
                if (window.at<cv::Vec3b>(centerY - j, centerX - i)[1] < color)
                    window.at<cv::Vec3b>(centerY - j, centerX - i)[1] = color;
            }
     }
}
image-20221116164411734
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王行知

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值