ThresholdTool.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "thresholdtool.h"
  2. ThresholdTool::ThresholdTool(QWidget *parent) : ToolInterface(parent), ui(new Ui::ThresholdTool)
  3. {
  4. ui->setupUi(this);
  5. ui->horizontalSlidermin->setMinimum(0);
  6. ui->horizontalSlidermin->setMaximum(255);
  7. ui->horizontalSlidermin->setSingleStep(1);
  8. ui->horizontalSlidermax->setMinimum(0);
  9. ui->horizontalSlidermax->setMaximum(255);
  10. ui->horizontalSlidermax->setSingleStep(1);
  11. ui->horizontalSlidermin->setValue(50);
  12. ui->horizontalSlidermax->setValue(200);
  13. ui->label2->setText("50");
  14. ui->label4->setText("200");
  15. }
  16. ThresholdTool::~ThresholdTool()
  17. {
  18. delete ui;
  19. }
  20. bool ThresholdTool::Serialized(QDataStream& ar, bool bIsOut)
  21. {
  22. int paranum;//参数数量
  23. if (bIsOut)//保存参数流程
  24. {
  25. paranum = 3;
  26. ar << paranum;//先保存参数数量
  27. ar << (int)1 << Min;
  28. ar << (int)2 << Max;
  29. ar << (int)3 << (bool)ui->checkBox->isChecked();
  30. }
  31. else//加载参数流程,参数加载顺序一定要跟保存顺序一致
  32. {
  33. bool bIsChecked;
  34. int para;
  35. ar >> paranum;//读取参数数量
  36. for (int i = 0; i < paranum; i++)
  37. {
  38. ar >> para;
  39. switch (para)
  40. {
  41. case 1: ar >> Min; break;
  42. case 2: ar >> Max; break;
  43. case 3: ar >> bIsChecked; break;
  44. default:
  45. {
  46. qWarning() << "Serialized(In) Error";
  47. return false;
  48. }
  49. break;
  50. }
  51. }
  52. {
  53. ui->checkBox->setChecked(bIsChecked);
  54. ui->horizontalSlidermin->setValue(Min);
  55. ui->horizontalSlidermax->setValue(Max);
  56. ui->label2->setText(QString::number(Min));
  57. ui->label4->setText(QString::number(Max));
  58. emit updateParameter(ShowParameter());
  59. }
  60. }
  61. return true;
  62. }
  63. QString ThresholdTool::ShowParameter()
  64. {
  65. QString str;
  66. Min = ui->horizontalSlidermin->value();
  67. Max = ui->horizontalSlidermax->value();
  68. str += QString::number(Min);
  69. str += ",";
  70. str += QString::number(Max);
  71. str += ",";
  72. if(ui->checkBox->isChecked())
  73. {
  74. str += "1";
  75. }
  76. else
  77. {
  78. str += "0";
  79. }
  80. return str;
  81. }
  82. int ThresholdTool::Execute()
  83. {
  84. int result = 1;
  85. Min = ui->horizontalSlidermin->value();
  86. Max = ui->horizontalSlidermax->value();
  87. try
  88. {
  89. if(Max > Min)
  90. {
  91. HTuple Channels;
  92. CountChannels(m_InImage, &Channels);
  93. if(Channels == 1)
  94. {
  95. HTuple hv_Width, hv_Height;
  96. HObject ho_Regions;
  97. Threshold(m_InImage, &ho_Regions, Min, Max);
  98. GetImageSize(m_InImage, &hv_Width, &hv_Height);
  99. if(ui->checkBox->isChecked())
  100. {
  101. Complement(ho_Regions, &ho_Regions);
  102. }
  103. RegionToBin(ho_Regions, &m_OutImage, 255, 0, hv_Width, hv_Height);
  104. }
  105. }
  106. }
  107. catch(...)
  108. {
  109. }
  110. return result;
  111. }
  112. void ThresholdTool::on_horizontalSlidermin_valueChanged(int value)
  113. {
  114. ui->label2->setText(QString::number(value));
  115. emit updateParameter(ShowParameter());
  116. }
  117. void ThresholdTool::on_horizontalSlidermax_valueChanged(int value)
  118. {
  119. ui->label4->setText(QString::number(value));
  120. emit updateParameter(ShowParameter());
  121. }
  122. void ThresholdTool::on_checkBox_clicked()
  123. {
  124. emit updateParameter(ShowParameter());
  125. }