1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
std::vector<unsigned> GetMinSeam(const Array& array){
unsigned w = array.Width();
unsigned h = array.Height();
std::vector<unsigned> seam(h);
Array e = energies(array);
Array dp (w,h);
double min = std::numeric_limits<double>::max();
for(unsigned int i=0; i<w; ++i){
dp(i,0) = e(i,0);
}
for(unsigned int j=1; j<h-1; ++j){
for(unsigned int i=0; i<w; ++i){
if(i==0){
dp(i,j) = e(i,j) + std::min(dp(i,j-1), dp(i+1,j-1));
}
else if(i==w-1){
dp(i,j) = e(i,j) + std::min(dp(i,j-1), dp(i-1, j-1));
}
else{
double t = std::min(dp(i,j-1), dp(i-1,j-1));
dp(i,j) = e(i,j) + std::min(t, dp(i+1, j-1));
}
}
}
unsigned p,o;
for(o=0; o<w; ++o){
if(dp(o, h-1) < min){ //i changed that cause i gave the seam length h
min = dp(o,h-1);
p = o;
}
}
std::cerr << min << " " << p << std::endl;
seam.at(h-1) = p; //changed that as well, thanks for telling :)
int u;
for(int k = h-1; k>0; --k){
if(p == w-1){
if(dp(p,k-1)<dp(p-1,k-1)){
seam.at(k-1) = p; //that didn't match as well
}
else{
seam.at(k-1) = p-1;
p = p-1;
}
}
else if(p == 0){
if(dp(p,k-1)<=dp(p+1,k-1)){
seam.at(k-1) = p;
}
else{
seam.at(k-1) = p+1;
p = p+1;
}
}
else{
if(dp(p-1,k-1)<=dp(p,k-1)){
u = p-1;
}
else{
u = p;
}
if(dp(u,k-1)<=dp(p+1,k-1)){
seam.at(k-1) = u;
p = u;
}
else{
seam.at(k-1)=p+1;
p = p+1;
}
}
}
std::cerr << GetSeamEnergy(array,seam) << " " << std::endl;
return seam;
}
|