Accept a bias for the energy.
[seam-carving.git] / src / seam_main.cc
blobad6bead83ad7cc34f0b255a35a0ef7649d36d80f
1 #include <iostream>
2 #include <stack>
3 #include <vector>
5 #include <boost/lexical_cast.hpp>
6 #include <boost/foreach.hpp>
8 #include <mln/io/ppm/save.hh>
9 #include <mln/level/fill.hh>
11 #include "all.hh"
13 using namespace mln;
14 using namespace mln::value;
16 static inline
17 void
18 add_bias (image2d<float> nrg, const char* path)
20 image2d<rgb8> tmp = load (path);
21 assert (tmp.domain () == nrg.domain ());
22 image2d<rgb8>::fwd_piter p (tmp.domain ());
24 for_all (p)
25 nrg (p) += tmp (p).red () - tmp (p).green ();
28 /// \file Find a vertical seam in an image.
30 int main (int argc, char** argv)
32 if (argc != 5 && argc != 6)
34 std::cerr << "usage: " << *argv
35 << " N in.ppm carved.ppm seams.ppm [bias.ppm]\n"
36 "where N is an integer > 0: the number of seams to carve.\n";
37 return 1;
40 unsigned nseam = 1;
42 try {
43 nseam = boost::lexical_cast<unsigned> (argv[1]);
45 catch (const boost::bad_lexical_cast&)
47 std::cerr << "invalid 1st argument" << std::endl;
48 return 2;
51 std::cerr << "initializing ... ";
52 image2d<rgb8> img = load (argv[2]);
53 image2d<float> nrg = get_nrg (img);
54 if (argc >= 6)
55 add_bias (nrg, argv[5]);
56 std::stack<std::vector<unsigned> > seams;
58 for (unsigned i = 0; i < nseam; ++i)
60 std::cerr << (i + 1) << "..";
61 seams.push (find_vertical_seam (nrg));
62 img = carve_vertical_seam (img, seams.top ());
63 nrg = carve_vertical_seam (nrg, seams.top ());
65 std::cerr << '.';
66 io::ppm::save (img, argv[3]);
67 for (unsigned i = 0; i < nseam; ++i)
69 img = uncarve_vertical_seam (img, seams.top ());
70 seams.pop ();
72 std::cerr << '.';
73 io::ppm::save (img, argv[4]);
74 std::cerr << std::endl;