Advertisement
Guest User

22.05 coloring mainwin

a guest
May 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.28 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "main.h"
  4.  
  5. using namespace cv;
  6. using namespace std;
  7.  
  8. cv::Mat tros_frame;
  9. cv::Mat radio_mat, radio_frame, radio_framex;
  10. cv::Mat thermo_mat, thermo_mat32, thermo_frame, thermo_framex, thermo_colored, thermo_mycolored;
  11.  
  12. int ksize_dred = 2, ksize_dred_c = 1, threshold_dred = 250;
  13. Mat grad, grad_x, grad_y;
  14. Mat dred_frame, canny_frame_d;
  15. Mat t_col_grad, t_col_grad1, t_col_gradx;
  16. float     resize_val = 1.5;
  17.  
  18. int filename_r = 1, filename_t = 1;
  19. cv::FileStorage storage;// (ffname, cv::FileStorage::READ);
  20. std::string path_r = "rims36/", path_t = "t1/";//"thermophotos_10/";
  21. std::string filename = "1";
  22. std::string fileformat1 = ".png";
  23. std::string fileformat2 = ".xml";
  24. std::string ffname_r = "rims36/1.xml", ffname_t = "thermophotos_10/1.xml";  //full file name
  25.  
  26.  
  27. MainWindow::MainWindow(QWidget *parent) :
  28.     QMainWindow(parent),
  29.     ui(new Ui::MainWindow)
  30. {
  31.     ui->setupUi(this);
  32.     radio_mat = (MatZ, MatX, CV_16SC1, Scalar(0));
  33.     radio_frame = (MatZ, MatX, CV_8UC1, Scalar(0));
  34.     t_col_grad = imread("tgrad/thermo-gradient.png", IMREAD_COLOR);
  35.     t_col_grad1 = imread("tgrad/thermo-gradient_scuare.png", IMREAD_COLOR);
  36.     cv::resize (t_col_grad1, t_col_gradx, cv::Size(), 0.2, 0.231);
  37.     //ui->label_label->setText(QString::number(radio_mat.depth()));//shit
  38.     //ui->label_label_2->setText(QString::number(radio_frame.depth()));
  39.     QImage thermo_rgb((uchar*)t_col_gradx.data, t_col_gradx.cols, t_col_gradx.rows, t_col_gradx.step, QImage::Format_RGB888);
  40.     thermo_rgb = thermo_rgb.rgbSwapped();
  41.     ui->col_grad->setPixmap(QPixmap::fromImage(thermo_rgb));//QImage::Format_RGB888
  42.     ui->col_grad_2->setPixmap(QPixmap::fromImage(thermo_rgb));
  43.     //  ui->col_grad->setPixmap(QPixmap::fromImage(QImage(t_col_grad.data, t_col_grad.cols, t_col_grad.rows, t_col_grad.step, QImage::Format_RGB888)));
  44.     //  ui->col_grad_2->setPixmap(QPixmap::fromImage(QImage(t_col_grad.data, t_col_grad.cols, t_col_grad.rows, t_col_grad.step, QImage::Format_RGB888)));
  45.     cv::imshow("t_col_grad", t_col_grad);
  46.  
  47.     on_s_rim_update_clicked();
  48.     on_s_thermo_update_clicked();
  49.     on_s_dred_update_clicked();
  50.     on_s_dtod_update_clicked();
  51.     on_s_tros_update_clicked();
  52.  
  53. }
  54.  
  55. MainWindow::~MainWindow(){
  56.     delete ui;
  57. }
  58.  
  59.  
  60. void MainWindow::on_s_rim_update_clicked(){
  61.     filename = to_string(filename_r);
  62.     ffname_r = path_r + filename + fileformat2;
  63.     storage.open(ffname_r, cv::FileStorage::READ);
  64.     storage["rim"] >> radio_mat;
  65.     storage.release();
  66.     rim_mat2frame (&radio_mat, &radio_frame);
  67.     cv::resize (radio_frame, radio_framex, Size(), resize_val, resize_val);
  68.     ui->s_rim->setPixmap(QPixmap::fromImage(QImage(radio_framex.data, radio_framex.cols, radio_framex.rows, radio_framex.step, QImage::Format_Grayscale8)));
  69.     ui->rim_num->setText(QString::number(filename_r));
  70. }
  71.  
  72. void MainWindow::on_s_thermo_update_clicked(){
  73.     filename = to_string(filename_t);
  74.     ffname_t = path_t + filename + fileformat2;
  75.     storage.open(ffname_t, cv::FileStorage::READ);
  76.     storage["thermo"] >> thermo_mat;
  77.     storage.release();
  78.     rim_mat2frame (&thermo_mat, &thermo_frame);
  79.     cv::resize (thermo_frame, thermo_framex, Size(), resize_val, resize_val);
  80.     ui->s_thermo->setPixmap(QPixmap::fromImage(QImage(thermo_framex.data, thermo_framex.cols, thermo_framex.rows, thermo_framex.step, QImage::Format_Grayscale8)));
  81.     ui->th_num->setText(QString::number(filename_t));
  82. }
  83.  
  84. void MainWindow::on_s_dred_update_clicked(){
  85.     sobel_oper (&radio_frame, &grad, &ksize_dred);
  86.     Canny(grad, canny_frame_d, threshold_dred, 255, (2*ksize_dred_c+1));
  87.     cv::resize (canny_frame_d, canny_frame_d, Size(), resize_val, resize_val);
  88.     ui->s_dred ->setPixmap(QPixmap::fromImage(QImage(canny_frame_d.data, canny_frame_d.cols, canny_frame_d.rows, canny_frame_d.step, QImage::Format_Grayscale8)));
  89.  
  90. }
  91.  
  92. void MainWindow::on_s_dtod_update_clicked(){
  93.     cv::normalize(thermo_mat, thermo_frame, 0, 255, cv::NORM_MINMAX);
  94.     thermo_frame.convertTo(thermo_frame, CV_8UC1, 1);
  95.     cv::applyColorMap(thermo_frame, thermo_colored, COLORMAP_HOT);
  96.     cv::resize (thermo_colored, thermo_colored, cv::Size(), resize_val, resize_val);
  97.     QImage thermo_rgb((uchar*)thermo_colored.data, thermo_colored.cols, thermo_colored.rows, thermo_colored.step, QImage::Format_RGB888);
  98.     thermo_rgb = thermo_rgb.rgbSwapped();
  99.     ui->s_dtod->setPixmap(QPixmap::fromImage(thermo_rgb));//QImage::Format_RGB888
  100.  
  101. }
  102.  
  103. void MainWindow::on_s_tros_update_clicked(){
  104.     tros_frame = (radio_frame.rows,radio_frame.cols, CV_8UC3, Scalar(0,0,0));
  105.     mount_frame(&canny_frame_d, &thermo_colored, &tros_frame);
  106.     QImage thermo_rgb1((uchar*)tros_frame.data, tros_frame.cols, tros_frame.rows, tros_frame.step, QImage::Format_RGB888);
  107.     //thermo_rgb1 = thermo_rgb1.rgbSwapped();
  108.     ui->s_tros->setPixmap(QPixmap::fromImage(thermo_rgb1));//QImage::Format_RGB888
  109.  
  110. }
  111.  
  112. void MainWindow::on_s_color_update_clicked(){
  113.     int j, k;
  114.     float   min=65535.0, max=0.0;//
  115.     for (j = 0; j < MatX; j++) {
  116.         for (k = 0; k < MatZ; k++) {
  117.             if (thermo_mat.at<ushort>(k,j) > max )
  118.                 max = thermo_mat.at<ushort>(k,j);
  119.             if (thermo_mat.at<ushort>(k,j) < min) min = thermo_mat.at<ushort>(k,j);
  120.         }
  121.     }
  122.     ui->label_thermo_max->setText(QString::number(max));
  123.     ui->label_thermo_min->setText(QString::number(min));
  124.     for (j = 0; j < MatX; j++) {
  125.         for (k = 0; k < MatZ; k++) {
  126.             thermo_mat.at<ushort>(k,j) = thermo_mat.at<ushort>(k,j) - min;
  127.         }
  128.     }
  129. //
  130.     thermo_mat32 = (MatZ, MatX, CV_32FC1, Scalar(0));
  131.     int histSize = (max-min+1);//256;
  132.     thermo_mat.convertTo(thermo_mat32, CV_32F, 1.0/(histSize));
  133.     float range[]={0.0, 1.0};
  134.     const float *histRange = range;
  135.     Mat hist;
  136.     cv::calcHist(&thermo_mat32, 1, 0, Mat(),
  137.                  hist, 1, &histSize, &histRange, true, false );
  138.     // Plot the histogram
  139.     int hist_w = 512*2; int hist_h = 400;
  140.     int bin_w = cvRound( (double) hist_w/histSize );
  141.     Mat histImage( hist_h, hist_w, CV_8UC1, Scalar(0) );
  142.     normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
  143.     for( int i = 1; i < histSize; i++ )    {
  144.       line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) ,
  145.                        Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ),
  146.                        Scalar( 255, 0, 0), 2, 8, 0  );
  147.     }
  148.     namedWindow( "Result", 1 );    imshow( "Result", histImage );/**/
  149.     blur(thermo_mat32, thermo_mat32, Size(3,3), Point(-1,-1), BORDER_DEFAULT);
  150.     //medianBlur(thermo_mat32, thermo_mat32, 3);
  151.     cv::calcHist(&thermo_mat32, 1, 0, Mat(),
  152.                  hist, 1, &histSize, &histRange, true, false );
  153.     float hmax = 0;
  154.     int   medT = 0; // imax
  155.     for( int i = 0; i < histSize; i++ ) {
  156.         if (hist.at<float>(i) > hmax ) {
  157.             hmax = hist.at<float>(i);
  158.             medT = i;
  159.         }
  160.     }
  161.     //ui->label_thermo_max->setText(QString::number(medT));
  162.     thermo_mycolored = Mat (MatZ, MatX, CV_8UC3, Scalar(0,0,0));
  163.     for (int j = 0; j < thermo_mycolored.rows; j++){//thermo_mycolored.rows
  164. //        uchar* ptr = (uchar*)(thermo_mycolored.data+j * thermo_mycolored.step);
  165.         for (int k = 0; k < thermo_mycolored.cols; k++){
  166.             if (thermo_mat.at<ushort>(j,k) < (ushort)medT){
  167. //                ptr[3*k] = 0;
  168. //                ptr[3*k+1] = 0;
  169. //                ptr[3*k+2] = 0;
  170.                 //thermo_mycolored.at<Vec3b>(j,k)[0] = 0;
  171.                 //thermo_mycolored.at<Vec3b>(j,k)[1] = 0;
  172.                 thermo_mycolored.at<Vec3b>(j,k) =
  173.                         t_col_grad.at<Vec3b>(cvRound(thermo_mat.at<ushort>(j,k)*320/medT), 1);
  174.               }else{
  175.                 //ptr[3*k] = 255;
  176.                 //ptr[3*k+1] = 255;
  177.                 //ptr[3*k+2] = 255;
  178.                 //thermo_mycolored.at<Vec3b>(j,k)[0] = 255;
  179.                 //thermo_mycolored.at<Vec3b>(j,k)[1] = 255;
  180.                 thermo_mycolored.at<Vec3b>(j,k) =
  181.                         t_col_grad.at<Vec3b>(cvRound((thermo_mat.at<ushort>(j,k)-medT)*(999-320)/(histSize-medT))+320, 1);
  182.               }
  183.         }
  184.     }
  185.     namedWindow( "Result0", 1 );
  186.     imshow( "Result0", thermo_mycolored );
  187.     // Plot the histogram
  188.     histImage = ( hist_h, hist_w, CV_8UC1, Scalar(0) );
  189.     normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
  190.     for( int i = 1; i < histSize; i++ )    {
  191.       line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) ,
  192.                        Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ),
  193.                        Scalar( 255, 0, 0), 2, 8, 0  );
  194.     }
  195.     namedWindow( "Result1", 1 );    imshow( "Result1", histImage );/**/
  196.  
  197.     //QImage thermo_rgb1((uchar*)tros_frame.data, tros_frame.cols, tros_frame.rows, tros_frame.step, QImage::Format_RGB888);
  198.     //thermo_rgb1 = thermo_rgb1.rgbSwapped();
  199.     //ui->s_color->setPixmap(QPixmap::fromImage(thermo_rgb1));//QImage::Format_RGB888
  200.  
  201.     for (j = 0; j < MatX; j++) {
  202.         for (k = 0; k < MatZ; k++) {
  203.             thermo_mat.at<ushort>(k,j) = thermo_mat.at<ushort>(k,j) + min;
  204.         }
  205.     }
  206. }
  207.  
  208. // ///////////
  209. void MainWindow::on_push_next_t_clicked(){
  210.     if (filename_t < 14) filename_t ++;
  211.     on_s_thermo_update_clicked();
  212.     on_s_dtod_update_clicked();
  213.     on_s_tros_update_clicked();
  214. //    on_s_color_update_clicked();
  215. }
  216.  
  217. void MainWindow::on_push_next_r_clicked(){
  218.     if (filename_r < 9) filename_r ++;//26
  219.     on_s_rim_update_clicked();
  220.     on_s_dred_update_clicked();
  221.     on_s_tros_update_clicked();
  222.  
  223.     if (filename_t < 9) filename_t ++;
  224.     on_s_thermo_update_clicked();
  225.     on_s_dtod_update_clicked();
  226.     on_s_tros_update_clicked();
  227. //    on_s_color_update_clicked();
  228. }
  229.  
  230. void MainWindow::on_push_back_t_clicked(){
  231.     if (filename_t > 1) filename_t --;
  232.     on_s_thermo_update_clicked();
  233.     on_s_dtod_update_clicked();
  234.     on_s_tros_update_clicked();
  235. //    on_s_color_update_clicked();
  236. }
  237.  
  238. void MainWindow::on_push_back_r_clicked(){
  239.     if (filename_r > 1) filename_r --;
  240.     on_s_rim_update_clicked();
  241.     on_s_dred_update_clicked();
  242.     on_s_tros_update_clicked();
  243.  
  244.     if (filename_t > 1) filename_t --;
  245.     on_s_thermo_update_clicked();
  246.     on_s_dtod_update_clicked();
  247.     on_s_tros_update_clicked();
  248. //    on_s_color_update_clicked();
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement