last-svn-commit-139-g33ffff6 Improve Scribo demo.

* demo/demat/demat.pro: Add new flags. * demo/demat/src/main.cc: Make use of MLN_WO_GLOBALS_VARS. * demo/demat/src/main_window.cc, * demo/demat/src/main_window.hh: Cropping is not required anymore. Improve visualization speed. * demo/demat/src/preprocessing_task.hh, * demo/demat/src/runner.cc, * demo/demat/src/runner.hh, * demo/demat/ui/main_window.ui: Add a new task for Sauvola ms split. * demo/review/src/main.cc, * demo/review/src/mainwindow.cc, * demo/review/src/mainwindow.hh: Start updating executable names and arguments. * demo/shared/src/crop_item.cc, * demo/shared/src/crop_item.hh, * demo/shared/src/image_viewer.cc, * demo/shared/src/image_viewer.hh, * demo/shared/src/image_viewer.hxx, * demo/shared/src/internal/interactive_scene.cc, * demo/shared/src/internal/interactive_scene.hh, * demo/shared/ui/image_viewer.ui: Improve look and feel and drawing speed of these components. * demo/wizard/src/load_page.cc, * demo/wizard/src/main_window.cc, * demo/wizard/src/runner.cc: Fix compilation. --- scribo/ChangeLog | 37 ++++++++ scribo/demo/demat/demat.pro | 19 +++-- scribo/demo/demat/src/main.cc | 4 + scribo/demo/demat/src/main_window.cc | 48 ++++++++--- scribo/demo/demat/src/main_window.hh | 5 + scribo/demo/demat/src/preprocessing_task.hh | 1 + scribo/demo/demat/src/runner.cc | 91 +++++++++++--------- scribo/demo/demat/src/runner.hh | 1 + scribo/demo/demat/ui/main_window.ui | 15 +++ scribo/demo/review/src/main.cc | 13 ++- scribo/demo/review/src/mainwindow.cc | 58 +++++++------ scribo/demo/review/src/mainwindow.hh | 2 +- scribo/demo/shared/src/crop_item.cc | 84 +++++++++++++++--- scribo/demo/shared/src/crop_item.hh | 8 ++ scribo/demo/shared/src/image_viewer.cc | 79 ++++++++++++------ scribo/demo/shared/src/image_viewer.hh | 9 ++- scribo/demo/shared/src/image_viewer.hxx | 31 +++++++ .../demo/shared/src/internal/interactive_scene.cc | 11 ++- .../demo/shared/src/internal/interactive_scene.hh | 1 + scribo/demo/shared/ui/image_viewer.ui | 34 ++------ scribo/demo/wizard/src/load_page.cc | 1 + scribo/demo/wizard/src/main_window.cc | 10 ++- scribo/demo/wizard/src/runner.cc | 6 +- 23 files changed, 400 insertions(+), 168 deletions(-) create mode 100644 scribo/demo/shared/src/image_viewer.hxx diff --git a/scribo/ChangeLog b/scribo/ChangeLog index 8835298..cacdf4d 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,5 +1,42 @@ 2010-06-03 Guillaume Lazzara <z@lrde.epita.fr> + Improve Scribo demo. + + * demo/demat/demat.pro: Add new flags. + + * demo/demat/src/main.cc: Make use of MLN_WO_GLOBALS_VARS. + + * demo/demat/src/main_window.cc, + * demo/demat/src/main_window.hh: Cropping is not required + anymore. Improve visualization speed. + + * demo/demat/src/preprocessing_task.hh, + * demo/demat/src/runner.cc, + * demo/demat/src/runner.hh, + * demo/demat/ui/main_window.ui: Add a new task for Sauvola ms + split. + + * demo/review/src/main.cc, + * demo/review/src/mainwindow.cc, + * demo/review/src/mainwindow.hh: Start updating executable names + and arguments. + + * demo/shared/src/crop_item.cc, + * demo/shared/src/crop_item.hh, + * demo/shared/src/image_viewer.cc, + * demo/shared/src/image_viewer.hh, + * demo/shared/src/image_viewer.hxx, + * demo/shared/src/internal/interactive_scene.cc, + * demo/shared/src/internal/interactive_scene.hh, + * demo/shared/ui/image_viewer.ui: Improve look and feel and + drawing speed of these components. + + * demo/wizard/src/load_page.cc, + * demo/wizard/src/main_window.cc, + * demo/wizard/src/runner.cc: Fix compilation. + +2010-06-03 Guillaume Lazzara <z@lrde.epita.fr> + Small fixes. * binarization/sauvola_threshold_image.hh: Add ifdefs. diff --git a/scribo/demo/demat/demat.pro b/scribo/demo/demat/demat.pro index 4f4af23..92fbe74 100644 --- a/scribo/demo/demat/demat.pro +++ b/scribo/demo/demat/demat.pro @@ -8,26 +8,31 @@ TARGET = DEPENDPATH += . src INCLUDEPATH += . ../ ../shared $(OLN)/milena $(OLN) -QMAKE_CXXFLAGS += -g +QMAKE_CXXFLAGS += -g -O0 -LIBS += `Magick++-config --ldflags --libs` +LIBS +=/usr/lib/libMagick++.so -DEFINES += NDEBUG MLN_INCLUDE_ONLY +# `Magick++-config --ldflags --libs` + +DEFINES += NDEBUG MLN_WO_GLOBAL_VARS # Input HEADERS += \ + ../shared/src/crop_item.hh \ ../shared/src/image_viewer.hh \ ../shared/src/internal/interactive_scene.hh \ - ../shared/src/crop_item.hh \ ../shared/src/browse_widget.hh \ - src/runner.hh \ - src/main_window.hh + src/main_window.hh \ + src/runner.hh SOURCES += \ ../shared/src/crop_item.cc \ + ../shared/src/image_viewer.cc \ ../shared/src/internal/interactive_scene.cc \ ../shared/src/browse_widget.cc \ - src/mln_widgets.cc + src/main_window.cc \ + src/main.cc \ + src/runner.cc FORMS += \ ../shared/ui/image_viewer.ui \ diff --git a/scribo/demo/demat/src/main.cc b/scribo/demo/demat/src/main.cc index ee35a48..231cd9d 100644 --- a/scribo/demo/demat/src/main.cc +++ b/scribo/demo/demat/src/main.cc @@ -1,3 +1,7 @@ +#undef MLN_WO_GLOBAL_VARS +#include <mln/core/image/image2d.hh> +#include <mln/trace/all.hh> + #include <QtGui> #include <src/main_window.hh> #include <src/preprocessing_task.hh> diff --git a/scribo/demo/demat/src/main_window.cc b/scribo/demo/demat/src/main_window.cc index ee82ca1..5d3a54b 100644 --- a/scribo/demo/demat/src/main_window.cc +++ b/scribo/demo/demat/src/main_window.cc @@ -45,10 +45,6 @@ namespace scribo namespace demat { - // FIXME: move it as attribute. - static mln::image2d<mln::value::rgb8> input_; - - main_window::main_window() { setupUi(this); @@ -86,6 +82,9 @@ namespace scribo progress_->setCancelButton(0); progress_->setAutoClose(true); + action_Reload->setEnabled(false); + run_btn->setEnabled(false); + // Setup crop tool. viewer_->set_selection_enabled(true); connect(viewer_, SIGNAL(ready_for_crop()), this, SLOT(crop_slot())); @@ -114,13 +113,16 @@ namespace scribo void main_window::load() { - text_->hide(); + text_->hide(); - mln::io::magick::load(input_, current_image_.toStdString()); + mln::io::magick::load(input_, current_image_.toStdString()); input_dsp_ = QPixmap::fromImage(mln::convert::to_qimage_nocopy(input_)); viewer_->draw_image(input_dsp_); + + action_Reload->setEnabled(true); + run_btn->setEnabled(true); } @@ -132,7 +134,8 @@ namespace scribo process_args args; // Nothing for now. - runner_.start(current_image_, input_, Text_Doc, tasks, args); + runner_.start(current_image_, get_crop_image(), Text_Doc, tasks, args); + setEnabled(false); } @@ -161,6 +164,8 @@ namespace scribo tasks.insert(BinarizationSauvola); else if (binarizationCBox->currentIndex() == 2) tasks.insert(BinarizationSauvolaMs); + else if (binarizationCBox->currentIndex() == 3) + tasks.insert(BinarizationSauvolaMsSplit); return tasks; } @@ -199,15 +204,24 @@ namespace scribo while (!in.atEnd()) { QString line = in.readLine(); + line = line.section(' ', 4); text_->append(line); } text_->show(); + setEnabled(true); } - void main_window::crop_slot() + void main_window::on_action_Reload_triggered(bool) + { + load(); + } + + + mln::image2d<mln::value::rgb8> + main_window::get_crop_image() const { mln::box2d b = input_.domain(); QRectF selection = viewer_->selection(); @@ -224,15 +238,27 @@ namespace scribo // Update underlying data. mln::image2d<mln::value::rgb8> output(sbox); mln::data::fill(output, input_); - input_ = output; + return output; + } + + // No crop performed. + return input_; + } + + + void main_window::crop_slot() + { + mln::image2d<mln::value::rgb8> tmp = get_crop_image(); + if (tmp.buffer() != input_.buffer()) // A crop has been performed. + { + input_ = tmp; // Update display viewer_->draw_image(input_); } - - viewer_->enable_crop_tool(false); // Disable crop tool } + } // end of namespace scribo::demo::demat } // end of namespace scribo::demo diff --git a/scribo/demo/demat/src/main_window.hh b/scribo/demo/demat/src/main_window.hh index f52f171..dc23ccb 100644 --- a/scribo/demo/demat/src/main_window.hh +++ b/scribo/demo/demat/src/main_window.hh @@ -64,12 +64,15 @@ namespace scribo void crop_slot(); + void on_action_Reload_triggered(bool b); + private: // members void load(); void prepare_progress_bar(unsigned max); Doc_Type get_doc_type(); preprocessing_task_set_t get_tasks(); + mln::image2d<mln::value::rgb8> get_crop_image() const; private: // attributes QPixmap input_dsp_; @@ -78,6 +81,8 @@ namespace scribo QProgressDialog* progress_; runner runner_; + + mln::image2d<mln::value::rgb8> input_; }; diff --git a/scribo/demo/demat/src/preprocessing_task.hh b/scribo/demo/demat/src/preprocessing_task.hh index 5c335b7..5c287c4 100644 --- a/scribo/demo/demat/src/preprocessing_task.hh +++ b/scribo/demo/demat/src/preprocessing_task.hh @@ -46,6 +46,7 @@ namespace scribo BinarizationSimple, BinarizationSauvola, BinarizationSauvolaMs, + BinarizationSauvolaMsSplit, PREPROCESSING_TASK_COUNT // Invalid task. }; diff --git a/scribo/demo/demat/src/runner.cc b/scribo/demo/demat/src/runner.cc index 6972902..0ab591c 100644 --- a/scribo/demo/demat/src/runner.cc +++ b/scribo/demo/demat/src/runner.cc @@ -18,10 +18,11 @@ #include <scribo/preprocessing/split_bg_fg.hh> #include <scribo/preprocessing/denoise.hh> #include <scribo/preprocessing/homogeneous_contrast.hh> -#include <scribo/preprocessing/unskew.hh> +#include <scribo/preprocessing/deskew.hh> #include <scribo/binarization/sauvola.hh> #include <scribo/binarization/sauvola_ms.hh> -#include <scribo/binarization/simple.hh> +#include <scribo/binarization/sauvola_ms_split.hh> +#include <scribo/binarization/global_threshold_auto.hh> #include <mln/logical/not.hh> @@ -33,9 +34,6 @@ namespace scribo QString basedir_ = ""; - // FIXME: move as attribute. - mln::image2d<mln::value::rgb8> input_; - runner::runner(QObject *parent) : QThread(parent) @@ -97,6 +95,19 @@ namespace scribo image2d<value::rgb8> tmp_color = duplicate(ima); + // + //========== + // Subsample + //========== + if (tasks_.contains(ReduceSize)) + { + emit new_progress_label("Subsampling input image"); + tmp_color = mln::subsampling::antialiased(tmp_color, + find_best_scale(tmp_color)); + emit progress(1); + } + + //================== // Remove background //================== @@ -121,16 +132,16 @@ namespace scribo emit progress(1); - // FIXME: SUbsampling should be done at the very beginning!!!! - // - //========== - // Subsample - //========== - if (tasks_.contains(ReduceSize)) + //======= + // Deskew + //======= + if (tasks_.contains(Unskew)) { - emit new_progress_label("Subsampling input image"); - intensity_ima = mln::subsampling::antialiased(intensity_ima, - find_best_scale(intensity_ima)); + std::cout << "Deskew" << std::endl; + emit new_progress_label("Deskew"); + + intensity_ima = scribo::preprocessing::deskew(intensity_ima); + emit progress(1); } @@ -168,12 +179,20 @@ namespace scribo { // FIXME: sauvola should not negate the image. std::cout << "Binarization Sauvola_ms" << std::endl; - out_bool = binarization::sauvola_ms(intensity_ima, 51, 2, 67); + out_bool = binarization::sauvola_ms(intensity_ima, 51, 2); + } + else if (tasks_.contains(BinarizationSauvolaMsSplit)) + { + // FIXME: sauvola should not negate the image. + // FIXME: WARNING: work directly on the color image -> it is + // NOT deskewed nor contrast improved. + std::cout << "Binarization Sauvola_ms_split" << std::endl; + out_bool = binarization::sauvola_ms_split(tmp_color, 51, 2, 2); } else if (tasks_.contains(BinarizationSimple)) { std::cout << "Binarization Simple" << std::endl; - out_bool = scribo::binarization::simple(intensity_ima); + out_bool = scribo::binarization::global_threshold_auto(intensity_ima); } else { @@ -185,30 +204,18 @@ namespace scribo // FIXME: remove! -// logical::not_inplace(out_bool); + logical::not_inplace(out_bool); + //======== // Denoise //======== -// if (tasks_.contains(RemoveNoise)) -// { -// std::cout << "Remove noise" << std::endl; -// emit new_progress_label("Remove noise"); - -// out_bool = preprocessing::denoise(out_bool, c8(), 2, 2); - -// emit progress(1); -// } - - //======= - // Unskew - //======= - if (tasks_.contains(Unskew)) + if (tasks_.contains(RemoveNoise)) { - std::cout << "Unskew" << std::endl; - emit new_progress_label("Unskew"); + std::cout << "Remove noise" << std::endl; + emit new_progress_label("Remove noise"); - out_bool = scribo::preprocessing::unskew(out_bool).first(); + out_bool = preprocessing::denoise(out_bool, c8(), 2, 2); emit progress(1); } @@ -232,7 +239,7 @@ namespace scribo { case Text_Doc: emit new_progress_label("Finding text in document..."); - qDebug() << "Running text_in_article_pbm"; + qDebug() << "Running pbm_text_in_doc"; args << "/tmp/tmp.pbm" << "/tmp/out.txt"; // Denoise. @@ -246,20 +253,24 @@ namespace scribo args << "/tmp/"; - if (process_.execute(basedir_ + "/text_in_article_pbm", args)) + qDebug() << "Running:" + << basedir_ + "/pbm_text_in_doc" + << args; + + if (process_.execute(basedir_ + "/pbm_text_in_doc", args)) { - qDebug() << "Error while running text_in_article_pbm."; + qDebug() << "Error while running pbm_text_in_doc."; return; } break; case Picture: emit new_progress_label("Finding text in picture..."); - qDebug() << "Running text_in_photo_pbm_fast"; + qDebug() << "Running text_in_picture"; args << "/tmp/tmp.pbm" << "/tmp/out.ppm" << "1" << "1" << "1"; - if (process_.execute(basedir_ + "/text_in_photo_pbm_fast", args)) + if (process_.execute(basedir_ + "/text_in_picture", args)) { - qDebug() << "Error while running text_in_photo_pbm_fast."; + qDebug() << "Error while running text_in_picture."; return; } break; diff --git a/scribo/demo/demat/src/runner.hh b/scribo/demo/demat/src/runner.hh index 93e91bc..be32e5a 100644 --- a/scribo/demo/demat/src/runner.hh +++ b/scribo/demo/demat/src/runner.hh @@ -81,6 +81,7 @@ namespace scribo QSet<Preprocessing_Task> tasks_; QString filename_; process_args args_; + mln::image2d<mln::value::rgb8> input_; }; diff --git a/scribo/demo/demat/ui/main_window.ui b/scribo/demo/demat/ui/main_window.ui index a552500..349d43a 100644 --- a/scribo/demo/demat/ui/main_window.ui +++ b/scribo/demo/demat/ui/main_window.ui @@ -64,6 +64,8 @@ </property> <addaction name="action_Open" /> <addaction name="separator" /> + <addaction name="action_Reload" /> + <addaction name="separator" /> <addaction name="action_Quit" /> </widget> <widget class="QMenu" name="menuWindow" > @@ -270,6 +272,11 @@ <string>Local threshold multi-scale</string> </property> </item> + <item> + <property name="text" > + <string>Local threshold multi-scale (3-channels)</string> + </property> + </item> </widget> </item> </layout> @@ -336,6 +343,14 @@ <string>Advanced options</string> </property> </action> + <action name="action_Reload" > + <property name="text" > + <string>&Reload</string> + </property> + <property name="shortcut" > + <string>F5, Ctrl+R</string> + </property> + </action> </widget> <customwidgets> <customwidget> diff --git a/scribo/demo/review/src/main.cc b/scribo/demo/review/src/main.cc index ad9c28d..2a6acaf 100644 --- a/scribo/demo/review/src/main.cc +++ b/scribo/demo/review/src/main.cc @@ -30,12 +30,19 @@ int main(int argc, char *argv[]) { + if (argc < 2 || argc > 4) + { + qDebug() << "Usage: " + << argv[0] << " <path_to_scribo_src> [base_img_dir]"; + return 0; + } + QApplication app(argc, argv); - scribo::demo::MainWindow win; + scribo::demo::MainWindow win(argv[1]); - if (argc == 2) - win.set_base_img_dir(argv[1]); + if (argc == 3) + win.set_base_img_dir(argv[2]); win.show(); return app.exec(); diff --git a/scribo/demo/review/src/mainwindow.cc b/scribo/demo/review/src/mainwindow.cc index 4a12359..f2d10a2 100644 --- a/scribo/demo/review/src/mainwindow.cc +++ b/scribo/demo/review/src/mainwindow.cc @@ -34,41 +34,41 @@ const char * modes[][4] = { - { "Text in pictures", "pics", "../src/text_in_photo_ppm", "image-x-generic.png" }, - { "Text in docs", "doc", "../src/text_in_doc", "edit-find.png" }, + { "Text in pictures", "pics", "text_in_picture", "image-x-generic.png" }, + { "Text in docs", "doc", "text_in_doc", "edit-find.png" }, { (const char *)(1), 0, 0, 0}, // Separator { "Handwritten text lines", "hsc", "../../milena/sandbox/icdar/2009/hsc/input_to_lines", "text-x-generic.png" }, { "Handwritten text words", "hsc", "../../milena/sandbox/icdar/2009/hsc/input_to_words", "text-x-generic.png" }, { (const char *)(1), 0, 0, 0}, // Separator - { "Horizontal and Vertical Lines", "hvl", "../src/extract/primitive/find_discontinued_lines", "list-add.png" }, - { "Tables", "hvl", "../src/table/extract", "x-office-spreadsheet.png" }, - { "Pre-processing", "pproc", "../src/preprocess", "format-indent-more.png" }, - { "OCR", "ocr", "../src/recognition", "edit-find.png" }, + { "Horizontal and Vertical Lines", "hvl", "extract/primitive/find_discontinued_lines", "list-add.png" }, + { "Tables", "hvl", "table/extract", "x-office-spreadsheet.png" }, + { "Pre-processing", "pproc", "preprocess", "format-indent-more.png" }, + { "OCR", "ocr", "recognition", "edit-find.png" }, { 0, 0, 0 } // Empty line, do NOT delete. }; -// Allow to set up to 3 extra arguments to the binaries. +// Allow to set up to 5 extra arguments to the binaries. // // A program will be launched as follow: // -// ./my_program input.pbm <arg1> <arg2> <arg3> output.ppm +// ./my_program input.pbm output <arg1> <arg2> <arg3> <arg4> <arg5> // // Unused arguments are set to 0. // Each line must be mapped to the related on in the previous // array "modes". So DO preserve the order. // -const char *args_list[][3] = { - { 0, 0, 0 }, // Text in Pictures - { 0, 0, 0 }, // Text in Docs - { (const char *)(1), 0, 0}, // Separator - { "/dev/null", 0, 0 }, // Handwritten text lines - { "/dev/null", 0, 0 }, // Handwritten text words - { (const char *)(1), 0, 0}, // Separator - { "51", 0, 0 }, // Horizontal and Vertical lines - { "/dev/null", 0, 0 }, // Tables - { 0, 0, 0 }, // Pre-processing - { 0, 0, 0 }, // OCR +const char *args_list[][6] = { + { "1", "1", "1", "1", "1", 0 }, // Text in Pictures + { 0, 0, 0, 0, 0, 0 }, // Text in Docs + { (const char *)(1), 0, 0, 0, 0, 0 }, // Separator + { "/dev/null", 0, 0, 0, 0, 0 }, // Handwritten text lines + { "/dev/null", 0, 0, 0, 0, 0 }, // Handwritten text words + { (const char *)(1), 0, 0, 0, 0, 0 }, // Separator + { "51", 0, 0, 0, 0, 0 }, // Horizontal and Vertical lines + { "/dev/null", 0, 0, 0, 0, 0 }, // Tables + { 0, 0, 0, 0, 0, 0 }, // Pre-processing + { 0, 0, 0, 0, 0, 0 }, // OCR // Not empty line needed. }; @@ -80,8 +80,8 @@ namespace scribo namespace demo { - MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), context_(this) + MainWindow::MainWindow(const QString& exec_prefix, QWidget *parent) + : QMainWindow(parent), exec_prefix_(exec_prefix + "/"), context_(this) { setupUi(this); @@ -92,7 +92,6 @@ namespace scribo cached_result_.resize(nmodes); mainRefItem_ = 0; mainResultItem_ = 0; - exec_prefix_ = ""; base_img_dir_ = QCoreApplication::applicationDirPath(); pdialog_.setRange(0,0); @@ -165,7 +164,7 @@ namespace scribo action->setCheckable(true); action->setData(i); action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0 + i)); - action->setEnabled(QFile::exists(modes[i][2])); + action->setEnabled(QFile::exists(exec_prefix_ + modes[i][2])); if (modes[i][3] != 0) action->setIcon(QIcon(QString(":/icons/") + modes[i][3])); @@ -185,7 +184,7 @@ namespace scribo filename = QFileDialog::getOpenFileName(this, tr("Open Image."), base_img_dir_ + "/" + current_dir, - tr("Images (*.pbm *.pgm *.ppm)")); + tr("Images (*.pbm *.pgm *.ppm *.jpg *.png *.tiff)")); prepare_for_run(filename); } @@ -221,18 +220,21 @@ namespace scribo QString input = filepath->text(); args << input; + QTemporaryFile f; + f.open(); + args << f.fileName(); + last_output_ = f.fileName(); + for (unsigned i = 0; args_list[mode_][i]; ++i) args << args_list[mode_][i]; // if (!is_in_ocr_mode()) // { - QTemporaryFile f; - f.open(); - args << f.fileName(); - last_output_ = f.fileName(); // } // reset_progress_dialog(); + qDebug() << "Running " << exec_prefix_ + modes[mode_][2] + << " " << args; exec_.start(exec_prefix_ + modes[mode_][2], args); } diff --git a/scribo/demo/review/src/mainwindow.hh b/scribo/demo/review/src/mainwindow.hh index 90ed991..cb32e30 100644 --- a/scribo/demo/review/src/mainwindow.hh +++ b/scribo/demo/review/src/mainwindow.hh @@ -45,7 +45,7 @@ namespace scribo public: - MainWindow(QWidget *parent = 0); + MainWindow(const QString& exec_prefix, QWidget *parent = 0); ~MainWindow(); void set_base_img_dir(const QString& dir); diff --git a/scribo/demo/shared/src/crop_item.cc b/scribo/demo/shared/src/crop_item.cc index 70598fa..579eb65 100644 --- a/scribo/demo/shared/src/crop_item.cc +++ b/scribo/demo/shared/src/crop_item.cc @@ -35,17 +35,36 @@ namespace scribo namespace shared { + crop_item::crop_item(QGraphicsItem *parent) : QGraphicsItem(parent) { mousePress_ = false; setAcceptHoverEvents(true); +// draw_rect_ = scene()->sceneRect(); reset(); grabMouse(); } + + // Mouse is pressed. The rectangle must be created and about to + // be enlarged. + crop_item::crop_item(const QPointF& p, QGraphicsItem *parent) + : QGraphicsItem(parent) + { + mousePress_ = true; + setAcceptHoverEvents(true); + +// draw_rect_ = scene()->sceneRect(); + + cropRect_ = QRectF(p, QSize(4, 4)); + cropResize_ = CropItemResizeBottomRight; + + grabMouse(); + } + crop_item::~crop_item() { @@ -53,10 +72,11 @@ namespace scribo void crop_item::reset() { - if (parentItem()) - cropRect_ = parentItem()->boundingRect(); - else - cropRect_ = QRectF(20, 20, 100, 70); + cropRect_ = QRect(); +// if (parentItem()) +// cropRect_ = parentItem()->boundingRect(); +// else +// cropRect_ = QRectF(20, 20, 100, 70); } QRectF crop_item::cropRect() const @@ -68,6 +88,7 @@ namespace scribo QRectF crop_item::boundingRect() const { return scene()->sceneRect(); +// return draw_rect_; } void crop_item::paint(QPainter *painter, @@ -83,6 +104,7 @@ namespace scribo QPainterPath windowPath; windowPath.addRect(option->rect); +// windowPath.addRect(draw_rect_); windowPath = windowPath.subtracted(cropPath); // Draw Alpha-Black Background. @@ -134,6 +156,15 @@ namespace scribo cropResize_ = CropItemResizeNone; if (event->buttons() & Qt::LeftButton) { + if (!cropRect_.isValid()) + { + cropRect_ = QRectF(event->pos(), QSize(4, 4)); + cropResize_ = CropItemResizeBottomRight; + update(); + return; + } + + int wsize = std::min(int(cropRect_.width() / 6.f), 80); int hsize = std::min(int(cropRect_.height() / 6.f), 80); @@ -237,22 +268,35 @@ namespace scribo QGraphicsItem::mouseMoveEvent(event); - qreal minSize = 4 + (CROP_BORDER_LINE << 1); - QPointF delta = event->pos() - event->lastPos(); - switch (cropResize_) + + if (cropResize_ == CropItemResizeNone) { - case CropItemResizeNone: - if (!cropRect_.contains(event->pos())) - return; + if (//!cropRect_.contains(event->pos()) + (!(event->buttons() & Qt::LeftButton))) + return; - setCursor(Qt::SizeAllCursor); + // Moving existing rectangle + setCursor(Qt::SizeAllCursor); + cropRect_.translate(delta); - if (!(event->buttons() & Qt::LeftButton)) - return; +// draw_rect_.translate(delta); + update(); + return; + } - cropRect_.translate(delta); - break; + resize(delta); + } + + + void crop_item::resize(const QPointF& delta_) + { + qreal minSize = 4 + (CROP_BORDER_LINE << 1); + QPointF delta = delta_; + + QRectF tmp = cropRect_; + switch (cropResize_) + { case CropItemResizeTopLeft: delta.setY(qMin(cropRect_.height() - minSize, delta.y())); delta.setX(qMin(cropRect_.width() - minSize, delta.x())); @@ -275,11 +319,14 @@ namespace scribo cropRect_.translate(delta.x(), 0); setCursor(Qt::SizeBDiagCursor); break; + + default: case CropItemResizeBottomRight: cropRect_.setWidth(cropRect_.width() + delta.x()); cropRect_.setHeight(cropRect_.height() + delta.y()); setCursor(Qt::SizeFDiagCursor); break; + case CropItemResizeLeft: delta.setX(qMin(cropRect_.width() - minSize, delta.x())); cropRect_.setWidth(cropRect_.width() - delta.x()); @@ -307,6 +354,13 @@ namespace scribo if (cropRect_.height() < minSize) cropRect_.setHeight(minSize); + + // Updating bounding rect. +// if ((cropRect_.width() * cropRect_.height()) < (tmp.width() * tmp.height())) +// draw_rect_ = tmp; +// else +// draw_rect_ = cropRect_; + update(); } diff --git a/scribo/demo/shared/src/crop_item.hh b/scribo/demo/shared/src/crop_item.hh index d342f23..7978538 100644 --- a/scribo/demo/shared/src/crop_item.hh +++ b/scribo/demo/shared/src/crop_item.hh @@ -56,6 +56,11 @@ namespace scribo public: crop_item(QGraphicsItem *parent); + + // Mouse is pressed. The rectangle must be created and about + // to be enlarged. + crop_item(const QPointF& p, QGraphicsItem *parent); + virtual ~crop_item(); QRectF cropRect() const; @@ -67,6 +72,8 @@ namespace scribo void reset(); + void resize(const QPointF& delta); + protected: void mousePressEvent (QGraphicsSceneMouseEvent *event); void mouseReleaseEvent (QGraphicsSceneMouseEvent *event); @@ -97,6 +104,7 @@ namespace scribo private: CropItemResize cropResize_; QRectF cropRect_; + QRectF draw_rect_; bool mousePress_; }; diff --git a/scribo/demo/shared/src/image_viewer.cc b/scribo/demo/shared/src/image_viewer.cc index f9467dc..416f822 100644 --- a/scribo/demo/shared/src/image_viewer.cc +++ b/scribo/demo/shared/src/image_viewer.cc @@ -55,6 +55,9 @@ namespace scribo set_selection_enabled(false); set_rotation_enabled(false); + + // Set cache limit to 20Mb. + QPixmapCache::setCacheLimit(20480); } @@ -88,17 +91,6 @@ namespace scribo viewer_->show(); } - template <typename V> - void image_viewer::draw_image(const mln::image2d<V>& ima) - { - QImage - qima = mln::convert::to_qimage_nocopy(ima); - QPixmap pixmap = QPixmap::fromImage(qima); - - draw_image(pixmap, QPoint(ima.domain().pmin().col(), - ima.domain().pmin().row())); - } - void image_viewer::draw_image(const QPixmap& pixmap) { @@ -124,6 +116,8 @@ namespace scribo } image_ = viewer_->scene()->addPixmap(pixmap); + image_->setCacheMode(QGraphicsItem::DeviceCoordinateCache, + QSize(100,100)); viewer_->scene()->setSceneRect(image_->boundingRect()); if (pixmap.width() > viewer_->maximumViewportSize().width()) @@ -137,7 +131,7 @@ namespace scribo viewer_->setSceneRect(image_->sceneBoundingRect()); // Restore selection mode if needed - on_crop_btn_toggled(restore_selection); + setup_selection_tool(restore_selection); } @@ -252,6 +246,32 @@ namespace scribo resize_image(viewer_->viewport()->geometry()); } + + void image_viewer::new_mouse_selection_slot(const QPointF& p) + { + if (selection_) + { + if (selection_->cropRect().contains(p)) + return; + else + { + setup_selection_tool(false); + return; + } + } + + setup_selection_tool(false); // Remove previous selection. + setup_selection_tool(true, p); // Create new selection. + } + + + void image_viewer::new_mouse_released_slot(const QPointF&) + { + if (selection_ && !selection_->cropRect().isValid()) // CAS POURRI!! + setup_selection_tool(false); + } + + void image_viewer::clear() { visible_slider(false); @@ -263,16 +283,20 @@ namespace scribo } } - void image_viewer::on_crop_btn_toggled(bool b) + void image_viewer::setup_selection_tool(bool b, const QPointF& p) { if (b) { - if (selection_ == 0) + if (viewer_->scene() && selection_ == 0) { - selection_ = new crop_item(image_); + if (!p.isNull()) + selection_ = new crop_item(p, image_); + else + selection_ = new crop_item(image_); + + // Enable cropping on double click. connect(selection_, SIGNAL(ready_for_crop()), this, SIGNAL(ready_for_crop())); - } } else @@ -283,15 +307,22 @@ namespace scribo } - void image_viewer::enable_crop_tool(bool b) - { - crop_btn->setChecked(b); - } - - void image_viewer::set_selection_enabled(bool b) { - crop_btn->setVisible(b); + if (b) + { + connect(viewer_->scene(), SIGNAL(mouse_pressed(const QPointF&)), + this, SLOT(new_mouse_selection_slot(const QPointF&))); + connect(viewer_->scene(), SIGNAL(mouse_released(const QPointF&)), + this, SLOT(new_mouse_released_slot(const QPointF&))); + } + else + { + disconnect(viewer_->scene(), SIGNAL(mouse_pressed(const QPointF&)), + this, SLOT(new_mouse_selection_slot(const QPointF&))); + disconnect(viewer_->scene(), SIGNAL(mouse_released(const QPointF&)), + this, SLOT(new_mouse_released_slot(const QPointF&))); + } } @@ -369,8 +400,6 @@ namespace scribo rotate_ccw_btn->setEnabled(b); rotate_cw_btn->setEnabled(b); rotate_lbl->setEnabled(b); - - crop_btn->setEnabled(b); } diff --git a/scribo/demo/shared/src/image_viewer.hh b/scribo/demo/shared/src/image_viewer.hh index 3fe23ab..539e17d 100644 --- a/scribo/demo/shared/src/image_viewer.hh +++ b/scribo/demo/shared/src/image_viewer.hh @@ -73,6 +73,7 @@ namespace scribo void clear(); void set_selection_enabled(bool b); +// void set_mouse_selection_enabled(bool b); void set_rotation_enabled(bool b); @@ -100,7 +101,7 @@ namespace scribo void move_vertical_sliders(int value); void move_horizontal_sliders(int value); - void on_crop_btn_toggled(bool b); + void setup_selection_tool(bool b, const QPointF& p = QPointF()); void on_slider_valueChanged(int sli); @@ -112,11 +113,13 @@ namespace scribo void on_zoomFixed_clicked(); void on_zoomOriginal_clicked(); + void new_mouse_selection_slot(const QPointF&); + void new_mouse_released_slot(const QPointF& p); +// void selection_mouse_moved_slot(const QPointF& p); public slots: void set_image_layer_count(unsigned nslis); void update_image(const mln::image2d<dsp_data_t>& ima); - void enable_crop_tool(bool b); signals: void slider_valueChanged(int sli); @@ -140,4 +143,6 @@ namespace scribo } // end of namespace scribo +# include <shared/src/image_viewer.hxx> + #endif // ! SCRIBO_DEMO_SHARED_SRC_IMAGE_VIEWER_HH diff --git a/scribo/demo/shared/src/image_viewer.hxx b/scribo/demo/shared/src/image_viewer.hxx new file mode 100644 index 0000000..c7008a7 --- /dev/null +++ b/scribo/demo/shared/src/image_viewer.hxx @@ -0,0 +1,31 @@ +#ifndef SCRIBO_DEMO_SHARED_SRC_IMAGE_VIEWER_HH +# error "This file cannot be included directly." +#endif + + +namespace scribo +{ + + namespace demo + { + + namespace shared + { + + + template <typename V> + void image_viewer::draw_image(const mln::image2d<V>& ima) + { + QImage + qima = mln::convert::to_qimage_nocopy(ima); + QPixmap pixmap = QPixmap::fromImage(qima); + + draw_image(pixmap, QPoint(ima.domain().pmin().col(), + ima.domain().pmin().row())); + } + + } // end of namespace scribo::demo::shared + + } // end of namespace scribo::demo + +} // end of namespace scribo diff --git a/scribo/demo/shared/src/internal/interactive_scene.cc b/scribo/demo/shared/src/internal/interactive_scene.cc index a56517b..e49b855 100644 --- a/scribo/demo/shared/src/internal/interactive_scene.cc +++ b/scribo/demo/shared/src/internal/interactive_scene.cc @@ -47,13 +47,18 @@ namespace scribo { super_::mouseMoveEvent(e); + +// qDebug() << "interactiveScene::mouseMoveEvent"; + if (e->buttons() & Qt::LeftButton) { - emit mouse_pressed(e->scenePos()); + emit mouse_moved_and_pressed(e->scenePos()); mouse_moved_ = true; } emit mouse_moved(e->scenePos()); + + e->ignore(); } void interactiveScene::mousePressEvent(QGraphicsSceneMouseEvent *e) @@ -64,6 +69,7 @@ namespace scribo pressed_point_ = e->scenePos(); emit mouse_pressed(e->scenePos()); + e->ignore(); } void interactiveScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) @@ -75,7 +81,8 @@ namespace scribo emit mouse_released(e->scenePos()); - e->accept(); +// qDebug() << "crop_item::mouseReleaseEvent"; + e->ignore(); } } // end of namespace mln::demo::shared::internal diff --git a/scribo/demo/shared/src/internal/interactive_scene.hh b/scribo/demo/shared/src/internal/interactive_scene.hh index ba9e9e4..7099265 100644 --- a/scribo/demo/shared/src/internal/interactive_scene.hh +++ b/scribo/demo/shared/src/internal/interactive_scene.hh @@ -64,6 +64,7 @@ namespace scribo void mouse_click(const QPointF& p); void mouse_moved(const QPointF& p); void mouse_pressed(const QPointF& p); + void mouse_moved_and_pressed(const QPointF& p); void mouse_released(const QPointF& p); private: diff --git a/scribo/demo/shared/ui/image_viewer.ui b/scribo/demo/shared/ui/image_viewer.ui index c129b97..fcf1239 100644 --- a/scribo/demo/shared/ui/image_viewer.ui +++ b/scribo/demo/shared/ui/image_viewer.ui @@ -5,7 +5,7 @@ <rect> <x>0</x> <y>0</y> - <width>529</width> + <width>553</width> <height>350</height> </rect> </property> @@ -37,29 +37,6 @@ </spacer> </item> <item> - <widget class="QPushButton" name="crop_btn" > - <property name="enabled" > - <bool>false</bool> - </property> - <property name="maximumSize" > - <size> - <width>16777215</width> - <height>20</height> - </size> - </property> - <property name="text" > - <string>Crop</string> - </property> - <property name="icon" > - <iconset resource="../shared.qrc" > - <normaloff>:/icons/icons/crop-icon.png</normaloff>:/icons/icons/crop-icon.png</iconset> - </property> - <property name="checkable" > - <bool>true</bool> - </property> - </widget> - </item> - <item> <widget class="QLabel" name="rotate_lbl" > <property name="enabled" > <bool>false</bool> @@ -236,12 +213,15 @@ <property name="interactive" > <bool>true</bool> </property> - <property name="renderHints" > - <set>QPainter::Antialiasing</set> - </property> <property name="dragMode" > <enum>QGraphicsView::NoDrag</enum> </property> + <property name="viewportUpdateMode" > + <enum>QGraphicsView::BoundingRectViewportUpdate</enum> + </property> + <property name="optimizationFlags" > + <set>QGraphicsView::DontAdjustForAntialiasing|QGraphicsView::DontClipPainter|QGraphicsView::DontSavePainterState</set> + </property> </widget> </item> <item> diff --git a/scribo/demo/wizard/src/load_page.cc b/scribo/demo/wizard/src/load_page.cc index 2d9ed43..69c2f7f 100644 --- a/scribo/demo/wizard/src/load_page.cc +++ b/scribo/demo/wizard/src/load_page.cc @@ -45,6 +45,7 @@ namespace scribo void load_page::initializePage() { + cleanupPage(); } diff --git a/scribo/demo/wizard/src/main_window.cc b/scribo/demo/wizard/src/main_window.cc index 7f44ec5..2af6c82 100644 --- a/scribo/demo/wizard/src/main_window.cc +++ b/scribo/demo/wizard/src/main_window.cc @@ -44,10 +44,11 @@ namespace scribo main_window::main_window() + : QWizard(0, Qt::Window) { - QPixmap bg_pixmap(":/images/images/dematerialization.png"); - setPixmap(QWizard::WatermarkPixmap, bg_pixmap); - setPixmap(QWizard::BackgroundPixmap, bg_pixmap); +// QPixmap bg_pixmap(":/images/images/dematerialization.png"); +// setPixmap(QWizard::WatermarkPixmap, bg_pixmap); +// setPixmap(QWizard::BackgroundPixmap, bg_pixmap); connect(this, SIGNAL(currentIdChanged(int)), @@ -76,7 +77,8 @@ namespace scribo addPage(page_preprocessing); // Page_Processing - addPage(new process_page(this)); + process_page *page_process = new process_page(this); + addPage(page_process); // Page_Result addPage(new result_page(this)); diff --git a/scribo/demo/wizard/src/runner.cc b/scribo/demo/wizard/src/runner.cc index dd9ad9c..7e888f6 100644 --- a/scribo/demo/wizard/src/runner.cc +++ b/scribo/demo/wizard/src/runner.cc @@ -17,7 +17,7 @@ #include <scribo/preprocessing/unskew.hh> #include <scribo/binarization/sauvola.hh> #include <scribo/binarization/sauvola_ms.hh> -#include <scribo/binarization/simple.hh> +#include <scribo/binarization/global_threshold_auto.hh> #include <mln/logical/not.hh> @@ -157,12 +157,12 @@ namespace scribo { // FIXME: sauvola should not negate the image. std::cout << "Binarization Sauvola_ms" << std::endl; - out_bool = binarization::sauvola_ms(intensity_ima, 51, 2, 67); + out_bool = binarization::sauvola_ms(intensity_ima, 51, 2); } else if (tasks_.contains(BinarizationSimple)) { std::cout << "Binarization Simple" << std::endl; - out_bool = scribo::binarization::simple(intensity_ima); + out_bool = scribo::binarization::global_threshold_auto(intensity_ima); } else { -- 1.5.6.5
participants (1)
-
Guillaume Lazzara