リソース · ガイド・記事
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の表。これにより最下行からシームをたどり直せる
- エネルギーマップ、findLowEnergySeam、deleteSeamのTypeScript関数と、ループごとにシームを一本取り除くエントリー関数
- アルファチャンネルでピクセルをマスクするオブジェクト除去と、情報量の多い画像でこの方法が歪みを生む例
使い方
PrepKit の学習アドバイスであり、ソースの主張ではありません。
findLowEnergySeamをグリッドの問題として扱ってください。小さな行列でDPの表を手で埋め、シームをたどり直してから、何も見ずにコードを書きます。記事の例で貪欲法がうまくいかない理由も説明しましょう。
範囲と制約
コードはTypeScriptで、幅を縮める処理だけを扱います。エネルギーマップの部分的な再計算は省略されており、代わりに作者のjs-image-carverリポジトリが案内されています。
利用方法と形式
MITライセンスのtrekhleb/javascript-algorithmsリポジトリ内のGitHub上のREADMEで、英語で無料で読めます。作者のブログにあるインタラクティブ版へのリンクがあります。
PrepKit が 2026/10/02 に確認しました。 リソースの出典のコミット: 85293e3e2b88
この概要の根拠
- src/algorithms/image-processing/seam-carving/README.md
この項目を掲載しているページ
PrepKit ニュースレターを受け取る
任意です。PrepKit の利用には必要ありません。