Skip to content

Commit b15d92d

Browse files
committed
simple player in qt, still need some fixes
1 parent 3a2424a commit b15d92d

9 files changed

+698
-12
lines changed

ReadVideo/VideoOpenCv/VideoOpenCv.pro

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2018-08-09T01:31:30
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui
8+
9+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10+
11+
TARGET = VideoOpenCv
12+
TEMPLATE = app
13+
14+
# The following define makes your compiler emit warnings if you use
15+
# any feature of Qt which has been marked as deprecated (the exact warnings
16+
# depend on your compiler). Please consult the documentation of the
17+
# deprecated API in order to know how to port your code away from it.
18+
DEFINES += QT_DEPRECATED_WARNINGS
19+
20+
# You can also make your code fail to compile if you use deprecated APIs.
21+
# In order to do so, uncomment the following line.
22+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
23+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24+
25+
26+
SOURCES += \
27+
main.cpp \
28+
mainwindow.cpp \
29+
helperfunctions.cpp
30+
31+
HEADERS += \
32+
mainwindow.h
33+
34+
FORMS += \
35+
mainwindow.ui
36+
37+
INCLUDEPATH += $$PWD/../../../../../../usr/local/include
38+
DEPENDPATH += $$PWD/../../../../../../usr/local/include
39+
40+
LIBS += -lopencv_core
41+
LIBS += -lstdc++ -lopencv_highgui -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio -lopencv_video -lopencv_videostab

ReadVideo/VideoOpenCv/VideoOpenCv.pro.user

+336
Large diffs are not rendered by default.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include<memory>
2+
bool isNumber(const char * str){
3+
int i =0;
4+
while(*str != 0 ){
5+
if( !isdigit(str[i]) )
6+
return false;
7+
i++;
8+
str++;
9+
}
10+
return true;
11+
}
12+
// note: this implementation does not disable this overload for array types
13+
template<typename T, typename... Args>
14+
std::unique_ptr<T> make_unique(Args&&... args)
15+
{
16+
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
17+
}

ReadVideo/VideoOpenCv/main.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "mainwindow.h"
2+
#include <QApplication>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
MainWindow w(nullptr, "/home/windspring/c++Pro/opencvExamples/ReadVideo/Snow.mp4");
8+
w.show();
9+
return a.exec();
10+
}

ReadVideo/VideoOpenCv/mainwindow.cpp

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include "mainwindow.h"
2+
#include "ui_mainwindow.h"
3+
#include "helperfunctions.cpp"
4+
#include <QDebug>
5+
#include <QInputEvent>
6+
7+
#include <QPixmap>
8+
#include <QImage>
9+
#include "opencv2/core/core_c.h"
10+
#define RGBA2BGR 3
11+
using namespace cv;
12+
13+
MainWindow::MainWindow(QWidget *parent, const char *fileName) :
14+
QMainWindow(parent),
15+
ui(new Ui::MainWindow)
16+
{
17+
18+
if(!isNumber( fileName) )
19+
cap.open( String( fileName ) );
20+
else
21+
cap.open( atoi(fileName) );
22+
23+
fps = cap.get( cv::CAP_PROP_FPS );
24+
cap.set(CV_CAP_PROP_FRAME_WIDTH,1920);
25+
cap.set(CV_CAP_PROP_FRAME_HEIGHT,1000);
26+
27+
width = (int)cap.get(CAP_PROP_FRAME_WIDTH);
28+
height = (int)cap.get(CAP_PROP_FRAME_HEIGHT);
29+
frameCount = (int)cap.get(CAP_PROP_FRAME_COUNT);
30+
31+
ui->setupUi(this);
32+
this->ui->TimeLine->setMaximum(frameCount);
33+
34+
player = std::make_unique<std::thread>(&MainWindow::videoPlayerStart, this);
35+
36+
}
37+
38+
void MainWindow::keyPressEvent(QKeyEvent *event){
39+
40+
switch (event->key()){
41+
case Qt::Key_S:
42+
status = PlayerStatus::PAUSE;
43+
currentFrame = cap.get(CV_CAP_PROP_POS_FRAMES);
44+
currentFrame++;
45+
this->setFrame();
46+
break;
47+
case Qt::Key_D:
48+
status = PlayerStatus::PAUSE;
49+
currentFrame = cap.get(CV_CAP_PROP_POS_FRAMES);
50+
currentFrame--;
51+
this->setFrame();
52+
break;
53+
case Qt::Key_Escape:
54+
status = PlayerStatus::EXIT;
55+
break;
56+
57+
}
58+
}
59+
60+
void MainWindow::videoPlayerStart()
61+
{
62+
63+
Size size( width , height);
64+
65+
writer.open( "output", CV_FOURCC('M','J','P','G'), fps, size );
66+
while(true){
67+
cap >> frame;
68+
if( frame.empty() )
69+
break;
70+
cvtColor(frame, frame, RGBA2BGR);
71+
ui->display->setPixmap((QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888))));
72+
if(status == PlayerStatus::PAUSE)
73+
while( status == PlayerStatus::PAUSE){
74+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
75+
}
76+
else if( status == PlayerStatus::EXIT )
77+
break;
78+
std::this_thread::sleep_for(std::chrono::milliseconds( 20 ));
79+
currentFrame++;
80+
this->ui->TimeLine->setValue(currentFrame);
81+
82+
}
83+
}
84+
void MainWindow::setFrame(){
85+
cap.set(CV_CAP_PROP_POS_FRAMES,currentFrame);
86+
cap >> frame;
87+
cvtColor(frame, frame, RGBA2BGR);
88+
ui->display->setPixmap((QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888))));
89+
}
90+
91+
MainWindow::~MainWindow()
92+
{
93+
delete ui;
94+
status = PlayerStatus::EXIT;
95+
player->join();
96+
}
97+
98+
void MainWindow::on_Play_released()
99+
{
100+
this->status = PlayerStatus::ACTIVE;
101+
}
102+
103+
void MainWindow::on_pause_released()
104+
{
105+
this->status = PlayerStatus::PAUSE;
106+
}
107+
108+
void MainWindow::on_TimeLine_sliderReleased()
109+
{
110+
111+
}

ReadVideo/VideoOpenCv/mainwindow.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef MAINWINDOW_H
2+
#define MAINWINDOW_H
3+
4+
#include <QMainWindow>
5+
6+
#include "opencv2/highgui/highgui.hpp"
7+
#include "opencv2/imgproc/imgproc.hpp"
8+
#include "opencv2/core/cvstd.hpp"
9+
#include <opencv2/videoio/videoio_c.h>
10+
#include <chrono>
11+
#include <thread>
12+
13+
namespace Ui {
14+
class MainWindow;
15+
}
16+
enum class PlayerStatus{
17+
ACTIVE,
18+
PAUSE,
19+
EXIT
20+
};
21+
22+
class MainWindow : public QMainWindow
23+
{
24+
Q_OBJECT
25+
26+
public:
27+
explicit MainWindow(QWidget *parent = 0, const char *fileName = "");
28+
void keyPressEvent(QKeyEvent *event);
29+
void videoPlayerStart();
30+
~MainWindow();
31+
32+
private slots:
33+
void on_Play_released();
34+
35+
void on_pause_released();
36+
37+
void on_TimeLine_sliderReleased();
38+
39+
private:
40+
Ui::MainWindow *ui;
41+
cv::VideoCapture cap;
42+
cv::Mat frame;
43+
cv::VideoWriter writer;
44+
45+
int width, height, frameCount, currentFrame{0};
46+
double fps;
47+
std::unique_ptr<std::thread> player;
48+
PlayerStatus status;
49+
50+
// private functions
51+
void setFrame();
52+
};
53+
54+
#endif // MAINWINDOW_H

ReadVideo/VideoOpenCv/mainwindow.ui

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>MainWindow</class>
4+
<widget class="QMainWindow" name="MainWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>421</width>
10+
<height>306</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
<widget class="QWidget" name="centralWidget">
17+
<layout class="QVBoxLayout" name="verticalLayout">
18+
<item>
19+
<layout class="QGridLayout" name="gridLayout">
20+
<item row="0" column="0">
21+
<widget class="QLabel" name="display">
22+
<property name="maximumSize">
23+
<size>
24+
<width>1900</width>
25+
<height>910</height>
26+
</size>
27+
</property>
28+
<property name="text">
29+
<string/>
30+
</property>
31+
</widget>
32+
</item>
33+
</layout>
34+
</item>
35+
<item>
36+
<widget class="QGroupBox" name="groupBox">
37+
<property name="sizePolicy">
38+
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
39+
<horstretch>0</horstretch>
40+
<verstretch>0</verstretch>
41+
</sizepolicy>
42+
</property>
43+
<property name="minimumSize">
44+
<size>
45+
<width>0</width>
46+
<height>100</height>
47+
</size>
48+
</property>
49+
<property name="maximumSize">
50+
<size>
51+
<width>16777215</width>
52+
<height>100</height>
53+
</size>
54+
</property>
55+
<property name="title">
56+
<string>Options</string>
57+
</property>
58+
<widget class="QPushButton" name="Play">
59+
<property name="geometry">
60+
<rect>
61+
<x>0</x>
62+
<y>30</y>
63+
<width>80</width>
64+
<height>25</height>
65+
</rect>
66+
</property>
67+
<property name="text">
68+
<string>Play</string>
69+
</property>
70+
</widget>
71+
<widget class="QPushButton" name="pause">
72+
<property name="geometry">
73+
<rect>
74+
<x>80</x>
75+
<y>30</y>
76+
<width>80</width>
77+
<height>25</height>
78+
</rect>
79+
</property>
80+
<property name="text">
81+
<string>Pause</string>
82+
</property>
83+
</widget>
84+
<widget class="QSlider" name="TimeLine">
85+
<property name="geometry">
86+
<rect>
87+
<x>0</x>
88+
<y>70</y>
89+
<width>381</width>
90+
<height>16</height>
91+
</rect>
92+
</property>
93+
<property name="sizePolicy">
94+
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
95+
<horstretch>1</horstretch>
96+
<verstretch>0</verstretch>
97+
</sizepolicy>
98+
</property>
99+
<property name="maximum">
100+
<number>100000</number>
101+
</property>
102+
<property name="orientation">
103+
<enum>Qt::Horizontal</enum>
104+
</property>
105+
</widget>
106+
</widget>
107+
</item>
108+
</layout>
109+
</widget>
110+
</widget>
111+
<layoutdefault spacing="6" margin="11"/>
112+
<resources/>
113+
<connections/>
114+
</ui>

ReadVideo/example2

-88 Bytes
Binary file not shown.

ReadVideo/example2.cpp

+15-12
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,23 @@ int main(int argc, char const *argv[])
2727
cap >> frame;
2828
if( frame.empty() )
2929
break;
30+
3031
cv::imshow("Example2", frame);
31-
logPolar(
32-
frame,
33-
logpolar_frame,
34-
Point2f(
35-
frame.cols/2,
36-
frame.rows/2
37-
),
38-
40,
39-
WARP_FILL_OUTLIERS
40-
);
32+
// logPolar(
33+
// frame,
34+
// logpolar_frame,
35+
// Point2f(
36+
// frame.cols/2,
37+
// frame.rows/2
38+
// ),
39+
// 40,
40+
// WARP_FILL_OUTLIERS
41+
// );
42+
43+
// writer << logpolar_frame;
44+
cvtColor(frame, logpolar_frame, COLOR_BGR2GRAY);
45+
Canny(logpolar_frame, logpolar_frame, 10,100,3,true);
4146
imshow("Logpolar",logpolar_frame);
42-
writer << logpolar_frame;
43-
4447
if(cv::waitKey(1) >= 0 )
4548
break;
4649
}

0 commit comments

Comments
 (0)