123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #include "DynthresholdTool.h"
- #include "ui_DynthresholdTool.h"
- dynthreshold::dynthreshold(QWidget *parent) : ToolInterface(parent), ui(new Ui::dynthreshold)
- {
- ui->setupUi(this);
- ui->lineEdit_width->setText("50");
- ui->lineEdit_height->setText("50");
- ui->lineEdit_offset->setText("50");
- ui->lineEdit_width->setValidator(new QIntValidator(1, 16777215, this));
- ui->lineEdit_height->setValidator(new QIntValidator(1, 16777215, this));
- ui->lineEdit_offset->setValidator(new QIntValidator(1, 16777215, this));
- ui->comboBox->setView(new QListView());
- }
- dynthreshold::~dynthreshold()
- {
- delete ui;
- }
- bool dynthreshold::Serialized(QDataStream& ar, bool bIsOut)
- {
- int paranum;//参数数量
- if (bIsOut)//保存参数流程
- {
- paranum = 5;
- ar << paranum;//先保存参数数量
- ar << (int)1 << m_nWidth;
- ar << (int)2 << m_nHeight;
- ar << (int)3 << m_nOffset;
- ar << (int)4 << m_nType;
- ar << (int)5 << ui->comboBox->currentIndex();
- }
- else//加载参数流程,参数加载顺序一定要跟保存顺序一致
- {
- int nCurrentIndex = 0;
- int para;
- ar >> paranum;//读取参数数量
- for (int i = 0; i < paranum; i++)
- {
- ar >> para;
- switch (para)
- {
- case 1: ar >> m_nWidth; break;
- case 2: ar >> m_nHeight; break;
- case 3: ar >> m_nOffset; break;
- case 4: ar >> m_nType; break;
- case 5: ar >> nCurrentIndex; break;
- default:
- {
- qWarning() << "Serialized(In) Error";
- return false;
- }
- break;
- }
- }
- {
- ui->comboBox->setCurrentIndex(nCurrentIndex);
- ui->lineEdit_width->setText(QString::number(m_nWidth) );
- ui->lineEdit_height->setText(QString::number(m_nHeight));
- ui->lineEdit_offset->setText(QString::number(m_nOffset));
- emit updateParameter(ShowParameter());
- }
- }
- return true;
- }
- QString dynthreshold::ShowParameter()
- {
- QString str;
- m_nWidth = ui->lineEdit_width->text().toInt();
- m_nHeight = ui->lineEdit_height->text().toInt();
- m_nOffset = ui->lineEdit_offset->text().toInt();
- str += QString::number(m_nWidth);
- str += ",";
- str += QString::number(m_nHeight);
- str += ",";
- str += QString::number(m_nOffset);
- str += ",";
- str += ui->comboBox->currentText();
- return str;
- }
- int dynthreshold::Execute()
- {
- int result = 1;
- m_nWidth = ui->lineEdit_width->text().toInt();
- m_nHeight = ui->lineEdit_height->text().toInt();
- m_nOffset = ui->lineEdit_offset->text().toInt();
- try
- {
- HTuple Channels;
- CountChannels(m_InImage, &Channels);
- if(Channels == 1)
- {
- HTuple hv_Width, hv_Height;
- HObject ho_Regions, ho_Mean;
- GetImageSize(m_InImage, &hv_Width, &hv_Height);
- MeanImage(m_InImage, &ho_Mean, m_nWidth, m_nHeight);
- if(m_nType == 0)
- {
- DynThreshold(m_InImage, ho_Mean, &ho_Regions, m_nOffset, "light");
- }
- if(m_nType == 1)
- {
- DynThreshold(m_InImage, ho_Mean, &ho_Regions, m_nOffset, "dark");
- }
- if(m_nType == 2)
- {
- DynThreshold(m_InImage, ho_Mean, &ho_Regions, m_nOffset, "equal");
- }
- if(m_nType == 3)
- {
- DynThreshold(m_InImage, ho_Mean, &ho_Regions, m_nOffset, "not_equal");
- }
- RegionToBin(ho_Regions, &m_OutImage, 255, 0, hv_Width, hv_Height);
- }
- }
- catch(...)
- {
- }
- return result;
- }
- void dynthreshold::on_comboBox_currentIndexChanged(int index)
- {
- m_nType = index;
- emit updateParameter(ShowParameter());
- }
- void dynthreshold::on_BTN_Add1_clicked()
- {
- ui->lineEdit_width->setText(QString::number(ui->lineEdit_width->text().toInt() + 1));
- emit updateParameter(ShowParameter());
- }
- void dynthreshold::on_BTN_Sub1_clicked()
- {
- if(ui->lineEdit_width->text().toInt() >= 2)
- {
- ui->lineEdit_width->setText(QString::number(ui->lineEdit_width->text().toInt() - 1));
- emit updateParameter(ShowParameter());
- }
- }
- void dynthreshold::on_BTN_Add2_clicked()
- {
- ui->lineEdit_height->setText(QString::number(ui->lineEdit_height->text().toInt() + 1));
- emit updateParameter(ShowParameter());
- }
- void dynthreshold::on_BTN_Sub2_clicked()
- {
- if(ui->lineEdit_height->text().toInt() >= 2)
- {
- ui->lineEdit_height->setText(QString::number(ui->lineEdit_height->text().toInt() - 1));
- emit updateParameter(ShowParameter());
- }
- }
- void dynthreshold::on_BTN_Add3_clicked()
- {
- ui->lineEdit_offset->setText(QString::number(ui->lineEdit_offset->text().toInt() + 1));
- emit updateParameter(ShowParameter());
- }
- void dynthreshold::on_BTN_Sub3_clicked()
- {
- if(ui->lineEdit_offset->text().toInt() >= 2)
- {
- ui->lineEdit_offset->setText(QString::number(ui->lineEdit_offset->text().toInt() - 1));
- emit updateParameter(ShowParameter());
- }
- }
- void dynthreshold::on_lineEdit_width_editingFinished()
- {
- emit updateParameter(ShowParameter());
- }
- void dynthreshold::on_lineEdit_height_editingFinished()
- {
- emit updateParameter(ShowParameter());
- }
- void dynthreshold::on_lineEdit_offset_editingFinished()
- {
- emit updateParameter(ShowParameter());
- }
|