Monday, 9 September 2013

openCV2, Qt and Histogram back projection--01

  In the previous post, I show you the principles of histogram back projection. This time I want to demonstrate how to combine openCV2 and Qt5(I prefer QWidget but not QtQuick2.0 in this post) together.You could download the codes from github.The gui is designed by QtDesigner, the implementation is same as the last post.I would assume you already know Qt5 and focus on how to show the cv::Mat by Qt5 in a nutshell.

void histBackProjectUI::run()
{
    if(input_.empty() || model_.empty()){
        QMessageBox::critical(this, tr("Error"), 
                     tr("input or model do not exist"));
        return;
    }

    typedef std::pair Result;

    cv::Size const kernel_size
    (
       ui->slider_kernel_width_->value() % 2 == 0 ? 
       ui->slider_kernel_width_->value() + 1 : 
       ui->slider_kernel_width_->value(),
       ui->slider_kernel_height_->value() % 2 == 0 ? 
       ui->slider_kernel_height_->value() + 1 : 
       ui->slider_kernel_height_->value()
    );

    int const morph_type = get_morphological_type(
                           ui->combobox_morph->currentText());
    impl_.set_input_and_model(input_, model_);
    Result custom_back_project = impl_.custom_back_project(
                                 ui->slider_hist_dim_->value(),   //(1)
                                 kernel_size, morph_type);
    Result opencv_back_project = impl_.openCV_back_project(
                                 ui->slider_hist_dim_->value(),   //(2)
                                 kernel_size, morph_type);

    QImage map_custom = mat_to_qimage_cpy(
                        custom_back_project.first); //(3)
    QImage result_custom = mat_to_qimage_cpy(
                           custom_back_project.second);//(4)
    QImage map_opencv = mat_to_qimage_cpy(
                        opencv_back_project.first); //(5)
    QImage result_opencv = mat_to_qimage_cpy(
                           opencv_back_project.second);//(6)
    ui->label_custom_map_image_->setPixmap(
                                 QPixmap::fromImage(map_custom)); //(7)
    ui->label_custom_result_image_->setPixmap(
                                 QPixmap::fromImage(result_custom)); //(8)
    ui->label_opencv_map_image_->setPixmap(
                                 QPixmap::fromImage(map_opencv)); //(9)
    ui->label_opencv_result_image_->setPixmap(
                                 QPixmap::fromImage(result_opencv)); //(10)
}
(1) and (2) retrieve the probability map and the result.(3)~(6) use the functions introduce at conversion between cv::Mat and QImage.(7)~(10)  show the image by QLabel.

No comments:

Post a Comment