资源 · 指南或文章
Content-aware image resizing in JavaScript
仓库创建者撰写的博客式文章,把接缝裁剪(一种内容感知的图像缩放方法)作为动态规划的完整案例。最适合想看一道网格路径 DP 题如何在真实代码中从朴素解法推进到最优解法的候选人。
审核日期
打开原站: Content-aware image resizing in JavaScript (在新标签页中打开原网站)添加到路线图
把这个资源放进你正在跟进的路线图。它会作为你自己的补充资源显示;路线图的步骤不会改变,也不计入你已记录的步骤。
来源内容语言:英语
There are many great articles written about the Seam Carving algorithm already, but I couldn't resist the temptation to explore this elegant, powerful, and yet simple algorithm on my own, and to write about my personal experience with it. Another point that drew my attention (as a creator of javascript-algorithms repo) was the fact that Dynamic Programming (DP) approach might be smoothly applied to solve it. And, if you're like me and still on your "learning algorithms" journey, this algorithmic solution may enrich your personal DP arsenal. So, with this article I want to do three things: Content-aware image resizing might be applied when it comes to changing the image proportions (i.e. reducing the width while keeping the height) and when losing some parts of the image is not desirable. Doing the straightforward image scaling in this case would distort the objects in it. To preserve the proportions of the objects while changing the image proportions we may use the Seam Carving algorithm that was introduced by Shai Avidan and Ariel Shamir.
PrepKit 概览
本概览由 PrepKit 编辑根据源代码仓库撰写并审核,并非资源本身的文字。
- 适合人群
- 来源未说明适用级别
- 形式
- 文章
涵盖内容
- 像素能量定义为与左右相邻像素的 RGB 颜色差,以及每删除一条接缝后如何重建能量图
- 比较三种接缝搜索方法:朴素法 O(w * 3^h),贪心法 O(w + h) 但不保证最优,DP 法 O(w * h)
- 一张 seamsEnergies 表,存储每个格子的最佳接缝能量和前一个像素,以便从最底行回溯出接缝
- 用 TypeScript 编写的能量图、findLowEnergySeam 和 deleteSeam 函数,以及每轮循环删除一条接缝的入口函数
- 通过 alpha 通道遮罩像素来移除物体,以及该方法会让内容繁杂的图像变形的例子
使用建议
这是 PrepKit 的学习建议,并非来源的说法。
把 findLowEnergySeam 当作一道网格题:在一个小矩阵上手动填写 DP 表并回溯出接缝,然后独立写出代码,并解释为什么贪心法在文章的例子上会失败。
范围与局限
代码为 TypeScript,只涉及缩小宽度,并跳过了能量图的局部重新计算,而是引导读者去看作者的 js-image-carver 仓库。
获取方式与形式
GitHub 上采用 MIT 许可的 trekhleb/javascript-algorithms 仓库中的 README;英文,免费阅读。附有作者博客上交互版本的链接。
PrepKit 于 2026年10月2日 审核。 资源来源提交: 85293e3e2b88
本概览的依据
- src/algorithms/image-processing/seam-carving/README.md
收录此条目的页面
订阅 PrepKit 通讯
可选。使用 PrepKit 并不需要订阅。