Skip to content

Commit

Permalink
Добавил дополнительные опции в примеры для отладки
Browse files Browse the repository at this point in the history
  • Loading branch information
MimusTriurus committed Jan 31, 2018
1 parent a04b553 commit b782997
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 18 deletions.
16 changes: 14 additions & 2 deletions lib/static/CvVideoCapture/Capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

Capture::Capture( QObject *parent ) :
QObject( parent ) {

_capture.set( CV_CAP_PROP_FRAME_HEIGHT, 480 );
_capture.set( CV_CAP_PROP_FRAME_WIDTH, 640 );
}

Capture::~Capture( ) {
Expand All @@ -23,8 +24,15 @@ void Capture::fps( int value ) {
_capture.set( CV_CAP_PROP_FPS, value );
}

bool Capture::isOpened( ) {
return _capture.isOpened( );
}

bool Capture::open( const int deviceId ) {
return _capture.open( deviceId );
_capture.open( deviceId );
qDebug( ) << _capture.get( CV_CAP_PROP_FRAME_HEIGHT );
qDebug( ) << _capture.get( CV_CAP_PROP_FRAME_WIDTH );
return _capture.isOpened( );
}

void Capture::close( ) {
Expand All @@ -35,3 +43,7 @@ cv::Mat Capture::read( ) {
_capture >> _frame;
return _frame;
}

bool Capture::retrieve( cv::Mat &mat ) {
return _capture.retrieve( mat );
}
4 changes: 4 additions & 0 deletions lib/static/CvVideoCapture/Capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ class Capture : public QObject {
void frameHeight( int value );
void frameWidth( int value );
void fps( int value );

bool isOpened( );
public slots:
/**
* @brief чтение очередного кадра
* @return кадр
*/
cv::Mat read( );

bool retrieve( cv::Mat &mat );
/**
* @brief получение доступа к камере
* @param deviceId идентификатор камеры
Expand Down
11 changes: 11 additions & 0 deletions lib/static/MatSerialization/MatSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ QByteArray MatSerialization::serializeMat( const cv::Mat &mat , int quality ) {
return result;
}

void MatSerialization::serializeMat(const cv::Mat &mat, QByteArray &matData, int quality ) {
std::vector<uchar> buffer;
std::vector<int> compressionParams = std::vector<int>( 2 );
compressionParams[ 0 ] = 1;
compressionParams[ 1 ] = quality;
cv::imencode( MAT_EXT, mat, buffer, compressionParams );

matData = QByteArray::fromRawData( reinterpret_cast<const char*>( buffer.data( ) ),
static_cast<int>( buffer.size( ) ) );
}

cv::Mat MatSerialization::deserializeMat( const QByteArray &matData ) {
std::vector<uchar> buffer( matData.cbegin( ), matData.cend( ) );
cv::Mat result = cv::imdecode( cv::Mat( buffer ), 1 );
Expand Down
1 change: 1 addition & 0 deletions lib/static/MatSerialization/MatSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class MatSerialization {
MatSerialization( ) = delete;

static QByteArray serializeMat( const cv::Mat &mat, int quality = 80 );
static void serializeMat( const cv::Mat &mat, QByteArray &matData, int quality = 80 );
static cv::Mat deserializeMat( const QByteArray &matData );
};

Expand Down
54 changes: 41 additions & 13 deletions test/CvTransmitterTest/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <QDebug>
#include <MatSerialization.h>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc.hpp>

MainWindow::MainWindow( QWidget *parent ) :
QMainWindow( parent ),
Expand All @@ -30,6 +30,9 @@ void MainWindow::initInterface( ) {
this->centralWidget( )->setLayout( mainLayout );
_showFrameWin.setChecked( true );
mainLayout->addWidget( &_showFrameWin );
mainLayout->addWidget( &_toGrayscale );
mainLayout->addWidget( &_resize );
mainLayout->addWidget( &_byLink );

auto lbl = new QLabel( "Set camera:", this );
mainLayout->addWidget( lbl );
Expand All @@ -43,31 +46,56 @@ void MainWindow::initInterface( ) {
mainLayout->addWidget( lbl );
mainLayout->addWidget( &_port );

auto btn = new QPushButton( "Start", this );
connect( btn, SIGNAL( clicked( ) ), this, SLOT( onBtnStart( ) ) );
mainLayout->addWidget( btn );
lbl = new QLabel( "Set quality( 20 - 100 ):", this );
mainLayout->addWidget( lbl );
mainLayout->addWidget( &_quality );

connect( &_btnStart, SIGNAL( clicked( ) ), this, SLOT( onBtnStart( ) ) );
mainLayout->addWidget( &_btnStart );

mainLayout->addWidget( &_log );
}

void MainWindow::onBtnStart( ) {
bool camOpened = _capture.open( _cameraId.text( ).toInt( ) );

_transmitter.host( _host.text( ) );
_transmitter.port( _port.text( ).toInt( ) );

if ( camOpened )
_tmrFrameUpdate.start( 5 );
if ( !_capture.isOpened( ) ) {
bool camOpened = _capture.open( _cameraId.text( ).toInt( ) );
_transmitter.host( _host.text( ) );
_transmitter.port( _port.text( ).toInt( ) );
if ( camOpened )
_tmrFrameUpdate.start( 5 );
_btnStart.setText( "Stop" );
}
else {
_btnStart.setText( "Start" );
_capture.close( );
_tmrFrameUpdate.stop( );
}
}

void MainWindow::onUpdateFrame( ) {
cv::Mat frame = _capture.read( );
cv::Mat frame;
_capture.retrieve( frame );
if ( _toGrayscale.isChecked( ) )
cv::cvtColor( frame, frame, cv::COLOR_BGR2GRAY );
if ( _resize.isChecked( ) ) {
cv::resize( frame, frame, cv::Size( 320, 240 ) );
}

if ( frame.data ) {
if ( _showFrameWin.isChecked( ) ) {
cv::namedWindow( "Transmitter", cv::WINDOW_AUTOSIZE );
cv::imshow( "Transmitter", frame );
}
_transmitter.sendFrameData( MatSerialization::serializeMat( frame ) );
int quality{ _quality.text( ).toInt( ) };
QByteArray outputBytes;
if ( _byLink.isChecked( ) ) {
MatSerialization::serializeMat( frame, outputBytes, quality );
}
else {
outputBytes = MatSerialization::serializeMat( frame, quality );
}
//qDebug( ) << "count:" << outputBytes.count( );
_transmitter.sendFrameData( outputBytes );
}
}

Expand Down
12 changes: 9 additions & 3 deletions test/CvTransmitterTest/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ class MainWindow : public QMainWindow {
QTimer _tmrFrameUpdate;
VideoTransmitter _transmitter;

QLineEdit _cameraId{ "0" };
QLineEdit _host{ "127.0.0.1" };
QLineEdit _port{ "10000" };
QLineEdit _cameraId { "0" };
QLineEdit _host { "192.168.1.40" };
QLineEdit _port { "10000" };
QTextEdit _log;
QCheckBox _showFrameWin{ "Show frame window" };
QCheckBox _toGrayscale { "Convert BGR2RGB" };
QCheckBox _resize { "Resize to 320x240" };
QCheckBox _byLink { "Transfer qbytearray by link" };
QLineEdit _quality { "80" };

QPushButton _btnStart { "Start" };

void initInterface( );
private slots:
Expand Down

0 comments on commit b782997

Please sign in to comment.