Image processing (ii) Seam carving algorithm-siggraph 2007_ image processing

Source: Internet
Author: User
Seam carving algorithm is a siggraph on the 2007 paper, named "Seam carving for content-aware image Resizing", this paper proposed a content-aware image scaling algorithm, After this paper thought, in recent years Siggraph above also has several good content-aware image scaling algorithms. Content-aware image scaling algorithm is generally used for image clipping, that is, sometimes you think a picture is a bit big, you want to cut it smaller, but you want to keep the object in the photo, this time will use the content-aware of the synaptic scaling algorithm.
Original cut result
Crop in vertical direction
It's time to start learning this algorithm, looks like very inscrutable appearance, the theory looks like the cool coax, what energy, the dynamic plan, all frighten me, finally the theory thing I really is not the bird, then the decisive direct reading code, saw the code, only then knew this algorithm actually very simple, The difficulty is more difficult than the ordinary image scaling is so little, in fact, the principle of the algorithm is very simple. Don't say much nonsense, this side only introduces the narrowing algorithm, and is the image width reduces the algorithm, for the image height reduces, only then to the image transpose processing, in short paper other function realization, very similar, the whole process says simply is by looks for a line, as shown in Figure 1 above, found the line later, Remove all the pixel points on that line to achieve a narrowing operation.

The process is implemented through iterations, with only one column removed at a time, and iterations until the width meets the requirements. Here's how to find a way to remove an energy line, and note that every time you remove a column, you recalculate the energy graph, which means that the gradient graph needs to be recalculated for the next round of removal.

Algorithm Flow

1, the algorithm first step: The calculation of image energy map

Energy map is generally the image pixel gradient modulus, in order to simplify the calculation can be converted to grayscale images, and then directly using the following formula (directly in the X, y direction of the difference in the absolute value, and then add), in fact this step is equivalent to the edge detection algorithm:


2, the algorithm second step: to find the smallest energy line

The minimum energy line refers to the column that needs to be removed: first you need to iterate with the first row or the last behavior of the image. The following are implemented to start with the last line of the image, to iterate up,

[C + +] View plain copy//First in the bottom row of the picture, the least energy pixel as the starting point int min_x = 0; int width = (*source).   Width (); int height = (*source).   Height (); for (int x = 0; x < width x + +) {if ((*source) (x,height-1)->energy < (*source) (min_x,height-1)->ener       GY) {min_x = x; }   }

After finding the last line of pixels that needs to be removed, set its coordinates to P (x,y), and then look up the previous line to find the energy minimum pixel in the three adjacent pixels in the y-1 row for the point P point. That is to find the coordinates (X-1,Y-1), (x,y-1), (x+1,y-1);

  [c++]  View Plain  copy  //min_x is the smallest pixel point in the picture at the bottom of the line   so when traversing, look up    void  Generate_path ( CML_image_ptr * Energy, int min_x, int * Path )    {       int min;       int x  = min_x;//The energy minimum point        int height =  (*energy) of the starting line. Height ();  //from the bottom to find the smallest energy line        for ( int y = height  - 1; y >= 0; y-- )  //from bottom up        {            min = x; //First vertex iteration update            //calculates the pixel of the least energy value in    three contiguous pixels in the previous line   as the energy minimum point of the previous line             int maxy=  get_max ( Energy, min , y );           if ( get_max ( energy, x-1, y  )  < Maxy )  //           {                min = x - 1;            }            if ( get_max ( Energy, x+1, y )  < Maxy )  //up-right            {                min = x + 1;            }           Path[y] = min;            x = min;       }  &nBsp }  

3, the algorithm step three: Remove the resulting minimum energy line, so that the width of the picture to reduce a pixel

Remove the smallest energy line, and all the pixels located to the right of the smallest energy line move one unit to the left, thereby reducing the width of the image by one unit.

When removing, in order for the image to look natural, it needs to be averaged at the place where the stitches are removed, assuming that the removal coordinates are p (x,y), then the pixel value of the x-1,y p (x-1,y) and P (X,y) are averaged. The pixel value of P (x+1,y) is the average of the pixel value of P (x-1,y) and P (x,y), and then P (x+1,y) can be moved to the position of P (x,y).

  [c++]  View plain  copy int height= (*source). Height ();       int width= (*source). Width ();       //removal function        for ( int y  =0; y < height; y++ )        {            int remove =  (Path) [y];             (* (Source)) (remove,y)->removed = true;      When the       //is removed   to make the image look natural, it needs to be averaged at the place where the stitches are removed, assuming that the removal coordinates are p (x,y), then             //the average       of pixel values of P (x-1,y) and P (x,y) after removal of P (x-1,y) The pixel value of       //p (x+1,y) is the average of the pixel value of P (x-1,y) and P (x,y), and then P (x+1,y) can be moved to the position of P (x,y)             if   (remove - 1)  > 0 )            {                if (  (* (Source)) (remove,y)->weight >= 0  )                {                     (* (Source)) (remove-1 , y)->image = average_pixels (  (* (Source)) (remove,y)->image,                         (* ( Source)). Get (remove-1,y)->image );                }                (* (Source)) ( Remove-1,y)->gray = grayscale_pixel ( & (* (Source)) (remove-1,y)->image );    &NBSP;&NBSp;      }               if (  (remove + 1)  <  (* (Source)). Width ()  )            {                if (  (* (Source)) (remove,y)->weight >=  0 )                 {                     (* (Source)) (remove+1,y)->image = average_pixels (  (* (Source)) (remove,y)->image,                          (* (Source)). Get (remove+1,y)->image );                }                (* (Source)) (remove+1,y)->gray = grayscale_pixel (  & (* (Source)) (remove+1,y)->image );            }            (* (Source)). Shift_row ( remove + 1, y, -1 );       }  

For the principle of image amplification algorithm, the minimum energy line is first found, and the coordinate of the point on the energy line is P (x,y), the new pixel is inserted in the center of P (x,y), P (x+1,y), and the pixel value is the average of P (x,y) and P (x+1,y). This article address: http://blog.csdn.net/hjimce/article/details/44916869 Author: hjimce Contact qq:1393852684 More resources please pay attention to my blog: http:/ /BLOG.CSDN.NET/HJIMCE original article, All rights reserved, reprint please keep these lines of information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.