VisionFlowPro/NodeEditorPro/examples/opcv/CvImageRGB2GrayModel.cpp
2023-02-28 14:50:28 +08:00

104 lines
2.3 KiB
C++

#include "opcv/CvImageRGB2GrayModel.h"
#include <QDebug>
#include <QTime>
CvImageRGB2GrayModel::CvImageRGB2GrayModel()
{
qRegisterMetaType<cv::Mat>("cv::Mat");
mCvImage = std::make_shared<CvImageData>();
}
unsigned int CvImageRGB2GrayModel::nPorts(PortType portType) const
{
unsigned int result = 1;
switch (portType)
{
case PortType::In:
result = 1;
break;
case PortType::Out:
result = 1;
default:
break;
}
return result;
}
bool CvImageRGB2GrayModel::RunTask()
{
Q_EMIT computingStarted();
PortIndex const outPortIndex = 0;
try
{
qDebug() << "CvImageRGB2GrayModel::RunTask thread:" << QThread::currentThreadId();
mAlgoTool = new CvAlgorithmTools();
mChildThread = new QThread();
mAlgoTool->moveToThread(mChildThread);
QObject::connect(mChildThread, &QThread::finished, mChildThread, &QObject::deleteLater);
QObject::connect(mChildThread, &QThread::finished, mAlgoTool, &QObject::deleteLater);
QObject::connect(this, &CvImageRGB2GrayModel::SignalCvImageRgb2Gray, mAlgoTool, &CvAlgorithmTools::CvImageRgb2Gray);
QObject::connect(mAlgoTool, &CvAlgorithmTools::sendCvImageRgb2GrayResult, this, &CvImageRGB2GrayModel::GetRgb2GrayResult);
mChildThread->start();
cv::Mat src;
mCvImage->CvImage()->copyTo(src);
if (!src.empty())
{
emit computingStarted();
emit SignalCvImageRgb2Gray(src);
modelValidationState = NodeValidationState::Valid;
modelValidationError = QString();
}
}
catch (...)
{
modelValidationState = NodeValidationState::Warning;
modelValidationError = QStringLiteral("ȱʧ»òÔËÐÐʧ°Ü!");
}
return true;
}
void CvImageRGB2GrayModel::GetRgb2GrayResult(cv::Mat grayImg)
{
mCvImage->setCvImage(grayImg);
if (mAlgoTool)
{
mChildThread->quit();
mChildThread->wait();
mAlgoTool = Q_NULLPTR;
mChildThread = Q_NULLPTR;
}
PortIndex const outPortIndex = 0;
Q_EMIT dataUpdated(outPortIndex);
Q_EMIT computingFinished();
}
void CvImageRGB2GrayModel::setInData(std::shared_ptr<NodeData>data , int)
{
if (data == nullptr)
return;
if (data->type() == mCvImage->type())
{
auto dataPtr = std::dynamic_pointer_cast<CvImageData>(data);
if (dataPtr->CvImage()->empty())
return;
mCvImage->setCvImage(*dataPtr->CvImage());
}
RunTask();
}
std::shared_ptr<NodeData> CvImageRGB2GrayModel::outData(PortIndex port)
{
return std::dynamic_pointer_cast<CvImageData>(mCvImage);
}