Advertisement
Guest User

Carve a seammmmmm

a guest
Jul 9th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. //Select a certain number of horizontal seams and remove them...
  2.         for ( int vSeams = 0; vSeams < difWid; vSeams++ ) {
  3.  
  4.             //Create and calculate the seam values
  5.             //Set the first row of seam values equal to the energies at those points.
  6.             double *seamMap = new double [(inWid-vSeams)*outHei];
  7.             for ( int col = 0; col < inWid-vSeams; col++ )
  8.                
  9.                 seamMap[col] = energy[col];
  10.  
  11.             //For every subsequent row, set each pixel's seam value equal to the sum of its energy and the least of all the top neighbor's energies.
  12.             for ( int row = 1; row < outHei ; row++ )
  13.  
  14.                 for ( int col = 0; col < inWid-vSeams; col++ )
  15.                    
  16.                     seamMap[row*(inWid-vSeams) + col] = energy[row*inWid + col]
  17.                                                       + minOfThree ( getArBd(row-1,col-1,inWid-vSeams,outHei,seamMap,maxEnergy*outHei),
  18.                                                                      getArBd(row-1,col  ,inWid-vSeams,outHei,seamMap,maxEnergy*outHei),
  19.                                                                      getArBd(row-1,col+1,inWid-vSeams,outHei,seamMap,maxEnergy*outHei));
  20.  
  21.             //Store each column index for every row, starting from last and working backward. This way we can easily get each pixel of the seam.
  22.             int *colIndicies = new int [outHei];
  23.  
  24.             //Find lowest value seam ending by looking at ending columns and finding lowest cumulative seam.
  25.             colIndicies[outHei-1] = 0;
  26.             double minSeam = getArBd ( outHei-1, 0, inWid-vSeams, outHei, seamMap, maxEnergy*outHei );
  27.             for ( int col = 1; col < inWid-vSeams; col++ ) {
  28.  
  29.                 double current = getArBd ( outHei-1, col, inWid-vSeams, outHei, seamMap, maxEnergy*inWid );
  30.                 if ( current < minSeam ) { minSeam = current; colIndicies[outHei-1] = col; }
  31.             }
  32.            
  33.             //Work from ending to recover entire least important seam.
  34.             for ( int row = outHei - 2; row >= 0; row -- ) {
  35.  
  36.                 double a = getArBd ( row, colIndicies[row+1]-1, inWid-vSeams, outHei, seamMap, maxEnergy*outHei );
  37.                 double b = getArBd ( row, colIndicies[row+1]  , inWid-vSeams, outHei, seamMap, maxEnergy*outHei );
  38.                 double c = getArBd ( row, colIndicies[row+1]+1, inWid-vSeams, outHei, seamMap, maxEnergy*outHei );
  39.                 double min = minOfThree ( a, b, c );
  40.                 if ( min == a )         colIndicies[row] = colIndicies[row+1]-1;
  41.                 else if ( min == b )    colIndicies[row] = colIndicies[row+1]  ;
  42.                 else                    colIndicies[row] = colIndicies[row+1]+1;
  43.             }
  44.  
  45.             //Call shift function on the seam point of each column of image and energy; get rid of that seam in the image and in energy map
  46.             for ( int row = 0; row < outHei; row ++ ) {
  47.  
  48.                 shift ( row, colIndicies[row], inWid-vSeams, outHei, inWid, false, image );
  49.                 shift ( row, colIndicies[row], inWid-vSeams, outHei, inWid, false, energy );
  50.             }
  51.  
  52.             //Recompute the energy at the locations where the seam was removed, and at the locations exactly below that
  53.             for ( int row = 0; row < outHei; row ++ ) {
  54.  
  55.                 for ( int dif = -2; dif <= 2; dif ++ ) {
  56.  
  57.                     int index = colIndicies[row] + dif;
  58.                     if ( index >= 0 && index < (inWid-vSeams) )
  59.  
  60.                         energy[row*inWid + index] = computeEnergy ( row, index, inWid-vSeams-1, outHei, inWid, image );
  61.                 }
  62.             }
  63.             delete [] colIndicies;
  64.             delete [] seamMap;
  65.         }
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement