Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
QQxiaoming committed Mar 25, 2020
1 parent 6b0ab5f commit 2e2c9d6
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 103 deletions.
35 changes: 19 additions & 16 deletions ImgViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "YUVdecoder.h"
#include "ui_UI_ImgViewer.h"


YUVDecodeThread::YUVDecodeThread(QWidget *parent,QString yuvfilename,QString YUVFormat,
int W, int H, int startframe, int totalframe) :
QThread(parent)
Expand All @@ -22,31 +21,35 @@ YUVDecodeThread::YUVDecodeThread(QWidget *parent,QString yuvfilename,QString YUV
this->H = H;
this->startframe = startframe;
this->totalframe = totalframe;
// 获取该格式的解码函数
this->decoder = YUV2RGB::yuvdecoder_map.find(YUVFormat).value();
}

void YUVDecodeThread::run()
{
/// 定义img列表用了保存每一帧的QImage*
QList<QImage*> img_RGB_list;
if(this->decoder == nullptr)
{
// 未能成功获取则返回无法解码
emit finsh_signal(img_RGB_list,nullptr);
}
else
{
// 成功获取则返回计算结果
QList<cv::Mat*> frame_RGB_list = this->decoder(this->yuvfilename, this->W, this->H, this->startframe, this->totalframe);
// 将原始帧转换到QImage*并保存到img列表
foreach( cv::Mat* img,frame_RGB_list)
{
// 提取图像的通道和尺寸,用于将OpenCV下的image转换成Qimage
QImage *qImg = new QImage((const unsigned char*)(img->data), img->cols, img->rows, img->step, QImage::Format_RGB888);
qImg->rgbSwapped();
img_RGB_list.insert(img_RGB_list.end(), qImg);
}

emit finsh_signal(img_RGB_list,this->yuvfilename);
}
}


ImgViewer::ImgViewer(QWidget *parent,QWidget *parentWindow) :
QWidget(parent),
ui(new Ui::ImgViewerWindow)
Expand All @@ -69,33 +72,42 @@ ImgViewer::~ImgViewer()

bool ImgViewer::setFileList(QStringList filenamelist,QString YUVFormat, int W, int H, int startframe, int totalframe)
{
// 获取该格式的解码函数
yuvdecoder_t decoder = YUV2RGB::yuvdecoder_map.find(YUVFormat).value();
if(decoder == nullptr)
{
// 未能成功获取则返回无法解码
return false;
}
else
{
// 成功获取解码器则准备解码
ui->imgViewer->setText("");
// 遍历文件列表
foreach( QString filename, filenamelist)
{
// 使用获取的解码函数进行解码得到RGB的原始帧列表
QList<cv::Mat*> frame_RGB_list = decoder(filename, W, H, startframe, totalframe);
if (frame_RGB_list.empty())
{
return false;
}
// 定义img列表用来保存每一帧的Qimage*
QList<QImage*> img_RGB_list;
// 将原始帧转换到Qimage*并保存到img列表
foreach (cv::Mat* img, frame_RGB_list)
{
// 提取图像的通道和尺寸,用于将OpenCV下的image转换成Qimage
QImage *qImg = new QImage((const unsigned char*)(img->data), img->cols, img->rows, img->step, QImage::Format_RGB888);
qImg->rgbSwapped();
img_RGB_list.insert(img_RGB_list.end(), qImg);
}

// img_RGB_list以及文件名存入列表
this->img_list.insert(this->img_list.end(),img_RGB_list);
QFileInfo fileInfo(filename);
this->filelist.insert(this->filelist.end(),fileInfo.fileName());
}
// 设置显示第一个YUV文件的第一帧图像
this->currentImg_RGB_list = this->img_list.at(0);
this->currentImg = this->currentImg_RGB_list.at(0);
this->setWindowTitle(this->filelist.at(0)+"-0");
Expand All @@ -105,7 +117,6 @@ bool ImgViewer::setFileList(QStringList filenamelist,QString YUVFormat, int W, i
}
}


void ImgViewer::reciveimgdata(QList<QImage*> img_RGB_list,QString filename)
{
if (!img_RGB_list.empty())
Expand Down Expand Up @@ -142,14 +153,16 @@ void ImgViewer::reciveimgdata(QList<QImage*> img_RGB_list,QString filename)
}
}


bool ImgViewer::setFileList_multithreading(QStringList filenamelist,QString YUVFormat, int W, int H, int startframe, int totalframe)
{
// 获取该格式的解码函数
yuvdecoder_t decoder = YUV2RGB::yuvdecoder_map.find(YUVFormat).value();
if(decoder == nullptr)
{
// 未能成功获取则返回无法解码
return false;
}
// 遍历文件列表
foreach( QString filename, filenamelist)
{
YUVDecodeThread *decodeThread = new YUVDecodeThread(this, filename, YUVFormat, W, H, startframe, totalframe);
Expand All @@ -160,20 +173,17 @@ bool ImgViewer::setFileList_multithreading(QStringList filenamelist,QString YUVF
return true;
}


void ImgViewer::closeEvent(QCloseEvent *event)
{
this->parentWindow->show();
event->accept();
}


void ImgViewer::draw_img(QPainter *painter)
{
painter->drawPixmap(this->point, QPixmap::fromImage(this->scaled_img));
}


void ImgViewer::paintEvent(QPaintEvent *event)
{
if (!this->img_list.empty())
Expand All @@ -185,7 +195,6 @@ void ImgViewer::paintEvent(QPaintEvent *event)
}
}


void ImgViewer::mouseMoveEvent(QMouseEvent *event)
{
if (!this->img_list.empty())
Expand All @@ -200,7 +209,6 @@ void ImgViewer::mouseMoveEvent(QMouseEvent *event)
}
}


void ImgViewer::mousePressEvent(QMouseEvent *event)
{
if (!this->img_list.empty())
Expand All @@ -213,7 +221,6 @@ void ImgViewer::mousePressEvent(QMouseEvent *event)
}
}


void ImgViewer::mouseReleaseEvent(QMouseEvent *event)
{
if (!this->img_list.empty())
Expand All @@ -231,7 +238,6 @@ void ImgViewer::mouseReleaseEvent(QMouseEvent *event)
}
}


void ImgViewer::mouseDoubleClickEvent(QMouseEvent *event)
{
if (!this->img_list.empty())
Expand All @@ -250,7 +256,6 @@ void ImgViewer::mouseDoubleClickEvent(QMouseEvent *event)
}
}


void ImgViewer::wheelEvent(QWheelEvent *event)
{
if (!this->img_list.empty())
Expand Down Expand Up @@ -288,7 +293,6 @@ void ImgViewer::wheelEvent(QWheelEvent *event)
}
}


void ImgViewer::resizeEvent(QResizeEvent *event)
{
if (!this->img_list.empty())
Expand All @@ -299,7 +303,6 @@ void ImgViewer::resizeEvent(QResizeEvent *event)
}
}


void ImgViewer::previousImg()
{
if (!this->img_list.empty())
Expand Down
2 changes: 1 addition & 1 deletion ImgViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private slots:
void resizeEvent(QResizeEvent *event);

private:
void draw_img(QPainter *painter);
Ui::ImgViewerWindow *ui;
QWidget *parentWindow;
bool left_click;
Expand All @@ -83,7 +84,6 @@ private slots:
QPoint startPos;
QPoint endPos;

void draw_img(QPainter *painter);
};

#endif // IMGVIEWER_H
29 changes: 13 additions & 16 deletions ImgViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from UI_ImgViewer import Ui_ImgViewerWindow
from YUVdecoder import YUV2RGB


class YUVDecodeThread(QThread):
finsh_signal = pyqtSignal(list, str)

Expand All @@ -27,11 +26,11 @@ def __init__(self, Window, yuvfilename, YUVFormat, W, H, startframe, totalframe)

def run(self):
if self.decoder is None:
#未能成功获取则返回无法解码
# 未能成功获取则返回无法解码
self.finsh_signal.emit([],'')
else:
try:
#成功获取则返回计算结果
# 成功获取则返回计算结果
frame_RGB_list = self.decoder(self.filename, self.W, self.H, self.startframe, self.totalframe)
except Exception as e:
self.finsh_signal.emit([], '')
Expand All @@ -47,8 +46,6 @@ def run(self):
img_RGB_list.append(QPixmap.fromImage(qImg))
self.finsh_signal.emit(img_RGB_list,self.filename)



class ImgViewer(QWidget, Ui_ImgViewerWindow):
def __init__(self,parentWindow):
super(ImgViewer, self).__init__()
Expand All @@ -64,24 +61,24 @@ def __init__(self,parentWindow):
self.left_click = False

def setFileList(self,filelist,YUVFormat, W, H, startframe, totalframe):
#获取该格式的解码函数
# 获取该格式的解码函数
decoder = YUV2RGB.YUVdecoder_dict.get(YUVFormat)
if decoder is None:
#未能成功获取则返回无法解码
# 未能成功获取则返回无法解码
return False
else:
#成功获取解码则准备解码
#定义空列表
# 成功获取解码器则准备解码
# 定义空列表
self.img_list = []
self.ui.imgViewer.setText('')
self.filelist = []
#遍历文件列表
# 遍历文件列表
for filename in filelist:
#使用获取的解码函数进行解码得到RGB的原始帧列表
# 使用获取的解码函数进行解码得到RGB的原始帧列表
frame_RGB_list = decoder(filename, W, H, startframe, totalframe)
#定义img列表用来保存每一帧的QPixmap
# 定义img列表用来保存每一帧的QPixmap
img_RGB_list = []
#将原始帧转换到QPixmap并保存到img列表
# 将原始帧转换到QPixmap并保存到img列表
for img in frame_RGB_list:
# 提取图像的通道和尺寸,用于将OpenCV下的image转换成Qimage
height, width, channel = img.shape
Expand Down Expand Up @@ -128,12 +125,12 @@ def setFileList_multithreading(self,filelist,YUVFormat, W, H, startframe, totalf
if decoder is None:
# 未能成功获取则返回无法解码
return False
#定义空列表
# 定义空列表
self.img_list = []
self.filelist = []
self.decode_thread = []
self.decode_thread_finsh = []
#遍历文件列表
# 遍历文件列表
for filename in filelist:
decodeThread = YUVDecodeThread(self,filename,YUVFormat, W, H, startframe, totalframe)
decodeThread.finsh_signal.connect(self.reciveimgdata)
Expand All @@ -155,7 +152,7 @@ def paintEvent(self, e):
self.draw_img(painter)
painter.end()

def mouseMoveEvent(self, e): # 重写移动事件
def mouseMoveEvent(self, e):
if not len(self.img_list) == 0:
if self.left_click:
self._endPos = e.pos() - self._startPos
Expand Down
11 changes: 4 additions & 7 deletions YUVdecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@

using namespace cv;


QMap<QString, yuvdecoder_t> YUV2RGB::yuvdecoder_map =
{
{"YV12", YUV2RGB::yv12},
{"I420", YUV2RGB::i420},
{"YUY2", nullptr},
{"UYUV", nullptr},
{"4:2:2", nullptr},
{"4:4:4", nullptr},
{"YUY2", nullptr}, // 暂未支持该格式
{"UYUV", nullptr}, // 暂未支持该格式
{"4:2:2", nullptr}, // 暂未支持该格式
{"4:4:4", nullptr}, // 暂未支持该格式
};

YUV2RGB::YUV2RGB()
{

}


QList<cv::Mat*> YUV2RGB::yv12(QString yuvfilename,int W, int H, int startframe, int totalframe)
{
QList<cv::Mat*> rgbImglist;
Expand All @@ -53,7 +51,6 @@ QList<cv::Mat*> YUV2RGB::yv12(QString yuvfilename,int W, int H, int startframe,
return rgbImglist;
}


QList<cv::Mat*> YUV2RGB::i420(QString yuvfilename,int W, int H, int startframe, int totalframe)
{
QList<cv::Mat*> rgbImglist;
Expand Down
3 changes: 0 additions & 3 deletions YUVdecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@

typedef QList<cv::Mat*> (* yuvdecoder_t)(QString yuvfilename,int W, int H, int startframe, int totalframe);



class YUV2RGB
{
public:
YUV2RGB();
static QList<cv::Mat*> yv12(QString yuvfilename,int W, int H, int startframe, int totalframe);
static QList<cv::Mat*> i420(QString yuvfilename,int W, int H, int startframe, int totalframe);
static QMap<QString, yuvdecoder_t> yuvdecoder_map;

};


Expand Down
8 changes: 4 additions & 4 deletions YUVdecoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class YUV2RGB(object):
YUVdecoder_dict = {
'YV12' : lambda yuvfilename, W, H, startframe, totalframe: YUV2RGB.yv12(yuvfilename, W, H, startframe, totalframe),
'I420' : lambda yuvfilename, W, H, startframe, totalframe: YUV2RGB.i420(yuvfilename, W, H, startframe, totalframe),
'YUY2' : None, #暂未支持该格式
'UYUV' : None, #暂未支持该格式
'4:2:2': None, #暂未支持该格式
'4:4:4': None, #暂未支持该格式
'YUY2' : None, # 暂未支持该格式
'UYUV' : None, # 暂未支持该格式
'4:2:2': None, # 暂未支持该格式
'4:4:4': None, # 暂未支持该格式
}

@classmethod
Expand Down
Loading

0 comments on commit 2e2c9d6

Please sign in to comment.