35 #include <QCoreApplication> 38 #include <QHostAddress> 39 #include "vtkImageData.h" 40 #include "vtkSmartPointer.h" 41 #include "vtkMetaImageReader.h" 42 #include "vtkImageImport.h" 43 #include "vtkLookupTable.h" 44 #include "vtkImageMapToColors.h" 45 #include "vtkMetaImageWriter.h" 53 #include "cxVideoServerConfig.h" 56 #include <opencv2/highgui/highgui.hpp> 57 #include <opencv2/imgproc/imgproc.hpp> 65 std::vector<PropertyPtr> retval;
76 retval->setGroup(
"OpenCV");
83 bool defaultValue =
false;
85 "When starting OpenCV, print properties to console",
87 retval->setAdvanced(
true);
88 retval->setGroup(
"OpenCV");
95 retval[
"--type"] =
"OpenCV";
96 retval[
"--videoport"] = this->
getVideoPortOption(root)->getValueAsVariant().toString();
98 retval[
"--properties"] =
"1";
106 retval <<
"--videoport: video id, default=0";
107 retval <<
"--in_width: width of incoming image, default=camera";
108 retval <<
"--in_height: height of incoming image, default=camera";
109 retval <<
"--out_width: width of outgoing image, default=camera";
110 retval <<
"--out_height: width of outgoing image, default=camera";
111 retval <<
"--properties: dump image properties";
127 void GetRandomTestMatrix(igtl::Matrix4x4& matrix)
157 mAvailableImage =
false;
161 mVideoCapture.reset(
new cv::VideoCapture());
163 mSendTimer =
new QTimer(
this);
164 connect(mSendTimer, SIGNAL(timeout()),
this, SLOT(send()));
169 this->deinitialize_local();
188 void ImageStreamerOpenCV::deinitialize_local()
192 qApp->processEvents();
193 mVideoCapture->release();
194 mVideoCapture.reset(
new cv::VideoCapture());
198 void ImageStreamerOpenCV::initialize_local()
202 if (!mArguments.count(
"videoport"))
203 mArguments[
"videoport"] =
"0";
204 if (!mArguments.count(
"out_width"))
205 mArguments[
"out_width"] =
"";
206 if (!mArguments.count(
"out_height"))
207 mArguments[
"out_height"] =
"";
208 if (!mArguments.count(
"in_width"))
209 mArguments[
"in_width"] =
"";
210 if (!mArguments.count(
"in_height"))
211 mArguments[
"in_height"] =
"";
213 QString videoSource = mArguments[
"videoport"];
216 bool sourceIsInt =
false;
217 videoSource.toInt(&sourceIsInt);
221 mVideoCapture->open(videoport);
225 mVideoCapture->open(videoSource.toStdString().c_str());
228 if (!mVideoCapture->isOpened())
230 cerr <<
"ImageStreamerOpenCV: Failed to open a video device or video file!\n" << endl;
238 mVideoCapture->grab();
240 catch(cv::Exception& e)
242 CX_LOG_ERROR() <<
"OpenCV failed with message: " << e.what();
243 mVideoCapture->release();
250 int default_width = mVideoCapture->get(CV_CAP_PROP_FRAME_WIDTH);
251 int default_height = mVideoCapture->get(CV_CAP_PROP_FRAME_HEIGHT);
256 mVideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, in_width);
257 mVideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, in_height);
262 mRescaleSize.setWidth(out_width);
263 mRescaleSize.setHeight(out_height);
265 if (mArguments.count(
"properties"))
266 this->dumpProperties();
268 std::cout <<
"ImageStreamerOpenCV: Started streaming from openCV device " 269 << videoSource.toStdString()
270 <<
", size=(" << in_width <<
"," << in_height <<
")";
271 if (( in_width!=mRescaleSize.width() )|| (in_height!=mRescaleSize.height()))
272 std::cout <<
". Scaled to (" << mRescaleSize.width() <<
"," << mRescaleSize.height() <<
")";
274 std::cout << std::endl;
282 this->initialize_local();
284 if (!mSendTimer || !mVideoCapture->isOpened())
286 reportError(
"ImageStreamerOpenCV: Failed to start streaming: Not initialized.");
291 mSendTimer->start(getSendInterval());
292 this->continousGrabEvent();
295 reportWarning(
"ImageStreamerOpenCV: Failed to start streaming: CX_USE_OpenCV not defined.");
296 #endif //CX_USE_OpenCV 306 this->deinitialize_local();
311 return (mSendTimer && mVideoCapture->isOpened());
314 void ImageStreamerOpenCV::dumpProperties()
317 this->dumpProperty(CV_CAP_PROP_POS_MSEC,
"CV_CAP_PROP_POS_MSEC");
318 this->dumpProperty(CV_CAP_PROP_POS_FRAMES,
"CV_CAP_PROP_POS_FRAMES");
319 this->dumpProperty(CV_CAP_PROP_POS_AVI_RATIO,
"CV_CAP_PROP_POS_AVI_RATIO");
320 this->dumpProperty(CV_CAP_PROP_FRAME_WIDTH,
"CV_CAP_PROP_FRAME_WIDTH");
321 this->dumpProperty(CV_CAP_PROP_FRAME_HEIGHT,
"CV_CAP_PROP_FRAME_HEIGHT");
322 this->dumpProperty(CV_CAP_PROP_FPS,
"CV_CAP_PROP_FPS");
323 this->dumpProperty(CV_CAP_PROP_FOURCC,
"CV_CAP_PROP_FOURCC");
324 this->dumpProperty(CV_CAP_PROP_FRAME_COUNT,
"CV_CAP_PROP_FRAME_COUNT");
325 this->dumpProperty(CV_CAP_PROP_FORMAT,
"CV_CAP_PROP_FORMAT");
326 this->dumpProperty(CV_CAP_PROP_MODE,
"CV_CAP_PROP_MODE");
327 this->dumpProperty(CV_CAP_PROP_BRIGHTNESS,
"CV_CAP_PROP_BRIGHTNESS");
328 this->dumpProperty(CV_CAP_PROP_CONTRAST,
"CV_CAP_PROP_CONTRAST");
329 this->dumpProperty(CV_CAP_PROP_SATURATION,
"CV_CAP_PROP_SATURATION");
330 this->dumpProperty(CV_CAP_PROP_HUE,
"CV_CAP_PROP_HUE");
331 this->dumpProperty(CV_CAP_PROP_GAIN,
"CV_CAP_PROP_GAIN");
332 this->dumpProperty(CV_CAP_PROP_EXPOSURE,
"CV_CAP_PROP_EXPOSURE");
333 this->dumpProperty(CV_CAP_PROP_CONVERT_RGB,
"CV_CAP_PROP_CONVERT_RGB");
335 this->dumpProperty(CV_CAP_PROP_RECTIFICATION,
"CV_CAP_PROP_RECTIFICATION");
339 void ImageStreamerOpenCV::dumpProperty(
int val, QString name)
342 double value = mVideoCapture->get(val);
344 std::cout <<
"Property " << name.toStdString() <<
" : " << mVideoCapture->get(val) << std::endl;
353 void ImageStreamerOpenCV::continousGrabEvent()
355 if (!mSendTimer->isActive())
358 QMetaObject::invokeMethod(
this,
"continousGrabEvent", Qt::QueuedConnection);
361 void ImageStreamerOpenCV::grab()
364 if (!mVideoCapture->isOpened())
371 bool success = mVideoCapture->grab();
373 mLastGrabTime = QDateTime::currentDateTime();
374 mAvailableImage = success;
379 void ImageStreamerOpenCV::send()
381 if (!mSender || !mSender->isReady())
383 if (!mAvailableImage)
389 package->mImage = this->getImageMessage();
390 mSender->send(package);
391 mAvailableImage =
false;
398 ImagePtr ImageStreamerOpenCV::getImageMessage()
401 if (!mVideoCapture->isOpened())
404 QTime start = QTime::currentTime();
406 cv::Mat frame_source;
408 if (!mVideoCapture->retrieve(frame_source, 0))
411 if (this->thread() == QCoreApplication::instance()->thread() && !mSender)
413 cv::imshow(
"ImageStreamerOpenCV", frame_source);
416 cv::Mat frame = frame_source;
417 if (( frame.cols!=mRescaleSize.width() )|| (frame.rows!=mRescaleSize.height()))
419 cv::resize(frame_source, frame, cv::Size(mRescaleSize.width(), mRescaleSize.height()), 0, 0, CV_INTER_LINEAR);
424 image->setAcquisitionTime(mLastGrabTime);
431 vtkImageDataPtr ImageStreamerOpenCV::convertTovtkImageData(cv::Mat& frame)
435 Eigen::Array3i dim(frame.cols, frame.rows, 1);
437 retval->SetDimensions(dim.data());
438 retval->SetSpacing(1,1,1);
443 if (frame.channels() == 3)
445 dataType = VTK_UNSIGNED_CHAR;
447 else if (frame.channels() == 1)
454 if (frame.depth() == 8)
456 dataType = VTK_UNSIGNED_CHAR;
462 std::cerr <<
"unknown image type" << std::endl;
466 retval->AllocateScalars(dataType, frame.channels());
478 unsigned char* dest =
reinterpret_cast<unsigned char*
> (retval->GetScalarPointer());
479 uchar* src = frame.data;
480 int N = frame.rows * frame.cols;
483 if (frame.channels() == 3)
485 if (frame.isContinuous())
488 for (
int i = 0; i < N; ++i)
500 for (
int i=0; i<dim[1]; ++i)
503 for (
int j=0; j<dim[0]; ++j)
515 if (frame.channels() == 1)
517 if (!frame.isContinuous())
519 std::cout <<
"Error: Non-continous frame data." << std::endl;
524 for (
int i = 0; i < N; ++i)
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
void reportError(QString msg)
DoublePropertyBasePtr getVideoPortOption(QDomElement root)
virtual void initialize(StringMap arguments)
Utility class for describing a bounded numeric range.
boost::shared_ptr< class Image > ImagePtr
virtual QStringList getArgumentDescription()
virtual void stopStreaming()
QStringList getArgumentDescription()
BoolPropertyBasePtr getPrintPropertiesOption(QDomElement root)
virtual ~ImageStreamerOpenCV()
StringMap convertToCommandLineArguments(QDomElement root)
std::map< QString, QString > StringMap
void reportWarning(QString msg)
virtual QString getType()
boost::shared_ptr< class BoolPropertyBase > BoolPropertyBasePtr
boost::shared_ptr< class DoublePropertyBase > DoublePropertyBasePtr
boost::shared_ptr< class DoubleProperty > DoublePropertyPtr
boost::shared_ptr< struct Package > PackagePtr
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
virtual void startStreaming(SenderPtr sender)
virtual bool isStreaming()
int convertStringWithDefault(QString text, int def)
boost::shared_ptr< class BoolProperty > BoolPropertyPtr
boost::shared_ptr< Sender > SenderPtr
virtual void initialize(StringMap arguments)
virtual std::vector< PropertyPtr > getSettings(QDomElement root)
Namespace for all CustusX production code.