Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Select a certain number of horizontal seams and remove them...
- for ( int vSeams = 0; vSeams < difWid; vSeams++ ) {
- //Create and calculate the seam values
- //Set the first row of seam values equal to the energies at those points.
- double *seamMap = new double [(inWid-vSeams)*outHei];
- for ( int col = 0; col < inWid-vSeams; col++ )
- seamMap[col] = energy[col];
- //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.
- for ( int row = 1; row < outHei ; row++ )
- for ( int col = 0; col < inWid-vSeams; col++ )
- seamMap[row*(inWid-vSeams) + col] = energy[row*inWid + col]
- + minOfThree ( getArBd(row-1,col-1,inWid-vSeams,outHei,seamMap,maxEnergy*outHei),
- getArBd(row-1,col ,inWid-vSeams,outHei,seamMap,maxEnergy*outHei),
- getArBd(row-1,col+1,inWid-vSeams,outHei,seamMap,maxEnergy*outHei));
- //Store each column index for every row, starting from last and working backward. This way we can easily get each pixel of the seam.
- int *colIndicies = new int [outHei];
- //Find lowest value seam ending by looking at ending columns and finding lowest cumulative seam.
- colIndicies[outHei-1] = 0;
- double minSeam = getArBd ( outHei-1, 0, inWid-vSeams, outHei, seamMap, maxEnergy*outHei );
- for ( int col = 1; col < inWid-vSeams; col++ ) {
- double current = getArBd ( outHei-1, col, inWid-vSeams, outHei, seamMap, maxEnergy*inWid );
- if ( current < minSeam ) { minSeam = current; colIndicies[outHei-1] = col; }
- }
- //Work from ending to recover entire least important seam.
- for ( int row = outHei - 2; row >= 0; row -- ) {
- double a = getArBd ( row, colIndicies[row+1]-1, inWid-vSeams, outHei, seamMap, maxEnergy*outHei );
- double b = getArBd ( row, colIndicies[row+1] , inWid-vSeams, outHei, seamMap, maxEnergy*outHei );
- double c = getArBd ( row, colIndicies[row+1]+1, inWid-vSeams, outHei, seamMap, maxEnergy*outHei );
- double min = minOfThree ( a, b, c );
- if ( min == a ) colIndicies[row] = colIndicies[row+1]-1;
- else if ( min == b ) colIndicies[row] = colIndicies[row+1] ;
- else colIndicies[row] = colIndicies[row+1]+1;
- }
- //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
- for ( int row = 0; row < outHei; row ++ ) {
- shift ( row, colIndicies[row], inWid-vSeams, outHei, inWid, false, image );
- shift ( row, colIndicies[row], inWid-vSeams, outHei, inWid, false, energy );
- }
- //Recompute the energy at the locations where the seam was removed, and at the locations exactly below that
- for ( int row = 0; row < outHei; row ++ ) {
- for ( int dif = -2; dif <= 2; dif ++ ) {
- int index = colIndicies[row] + dif;
- if ( index >= 0 && index < (inWid-vSeams) )
- energy[row*inWid + index] = computeEnergy ( row, index, inWid-vSeams-1, outHei, inWid, image );
- }
- }
- delete [] colIndicies;
- delete [] seamMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement