Please enable Javascript to view the contents

## Seam Carving

·  ☕ 3 分钟

Seam Carving算法是一种基于内容的图像缩放方法，在保证图像中“重要区域”不发生形变的前提下，对图像进行缩放。

## 1. 基本方法

$$e_1(\pmb I)=\left|\frac{\partial}{\partial x}\pmb I\right|+\left|\frac{\partial}{\partial y}\pmb I\right|$$

$$e_{HoG}(\pmb I)=\frac{\left|\frac{\partial}{\partial x}\pmb I\right|+\left|\frac{\partial}{\partial y}\pmb I\right|}{\max(HoG(\pmb I(x,y)))}$$

  1 2 3 4 5 6 7 8 9 10 11  void SeamCarving::genEnergyMap(const cv::Mat& img, cv::Mat& energy) { cv::Mat sobel_x, sobel_y, gray_energy; cv::cvtColor(img, gray_energy, cv::COLOR_BGR2GRAY); cv::Sobel(gray_energy, sobel_x, CV_32F, 1, 0, 3); cv::convertScaleAbs(sobel_x, sobel_x); cv::Sobel(gray_energy, sobel_y, CV_32F, 0, 1, 3); cv::convertScaleAbs(sobel_y, sobel_y); cv::addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0, energy); energy.convertTo(energy, CV_32FC1); } 

$$\pmb{s^x}=\{s_i^x\}_{i=1}^n=\{(x(i),i)\}_{i=1}^n,\ \ s.t.\ \ \forall i,\ \ |x(i)-x(i-1)|\leq 1$$

$$\pmb{s^y}=\{s_i^y\}_{j=1}^m=\{(j, y(j))\}_{j=1}^m,\ \ s.t.\ \ \forall j,\ \ |y(j)-y(j-1)|\leq 1$$

$$s^\ast=\min_{\pmb s}E(\pmb s)=\min_{\pmb s}\sum_{i=1}^ne(\pmb I(s_i))$$

$$M(i,j)=e(i,j)+\min(M(i-1,j-1), M(i-1,j), M(i-1,j+1))$$

### 1.1. 图像缩小任务

$$\min_{\pmb{s^x},\pmb{s^y},\alpha}\sum_{i=1}^kE(\alpha_i\pmb {s_i^x}+(1-\alpha_i)\pmb{s_i^y})$$

$$\pmb T(r,c)=\min(\pmb T(r-1,c),E(\pmb s^x(\pmb {I_{n-r-1\times m-c}})),\pmb T(r,c-1),E(\pmb s^y(\pmb {I_{n-r\times m-c-1}})))$$
$\pmb {I_{n-r-1\times m-c}}$表示大小为$n-r-1\times m-c$的图像（中间量），$E(\pmb{s^x}(\pmb I))$和$E(\pmb{s^y}(\pmb I))$为相应的方向接缝删除后的能量。

## 3. 总结

• 进行图像拉伸任务时容易造成图像区块重复，可以考虑手动排除部分区域进行优化
• 在梯度变化不明显的“重要区域”容易造成误处理，比如：《蒙娜丽莎》大片的头发。可以考虑手动划分“重要区域”进行处理

## 参考文献

[1] S. Avidan and A. Shamir. Seam carving for content-aware image resizing. In ACM SIGGRAPH 2007 papers, pages 10–es. 2007

Wenbo Chen
CG Student