add opencv demo

master
zcy 2022-05-17 23:35:55 +08:00
parent b1f865dda7
commit 74aa46fbd2
6 changed files with 117 additions and 31 deletions

View File

@ -1,5 +1,3 @@
#include "CPlayWidget.h"
#include <QOpenGLTexture> #include <QOpenGLTexture>
#include <QOpenGLBuffer> #include <QOpenGLBuffer>
#include <QMouseEvent> #include <QMouseEvent>
@ -61,10 +59,6 @@ void main(void) \
gl_FragColor = vec4(rgb, 1); \ gl_FragColor = vec4(rgb, 1); \
}"; }";
// rgb???????????
// ???MEDIASUBTYPE_RGB32 ??bgr?????????????????????
const char *fsrcrgb = "varying vec2 textureOut; \ const char *fsrcrgb = "varying vec2 textureOut; \
uniform sampler2D rgbdata; \ uniform sampler2D rgbdata; \
void main() \ void main() \
@ -82,7 +76,6 @@ void CPlayWidget::OnPaintData(const rtc::scoped_refptr<webrtc::I420BufferInterfa
qDebug("CPlayWidget::PlayOneFrame new data memory. Len=%d width=%d height=%d\n", qDebug("CPlayWidget::PlayOneFrame new data memory. Len=%d width=%d height=%d\n",
len, m_nVideoW, m_nVideoW); len, m_nVideoW, m_nVideoW);
memcpy(m_pBufYuv420p, data,len); memcpy(m_pBufYuv420p, data,len);
//??????,????paintGL???
update(); update();
} }
*/ */
@ -112,6 +105,7 @@ CPlayWidget::CPlayWidget(QWidget *parent):QOpenGLWidget(parent) {
} }
CPlayWidget::~CPlayWidget() { CPlayWidget::~CPlayWidget() {
} }
@ -167,6 +161,16 @@ int CPlayWidget::SetImgSize(uint32_t width, uint32_t height)
return 0; return 0;
} }
int CPlayWidget::RenderWidth()
{
return this->m_nVideoW;
}
int CPlayWidget::RenderHeight()
{
return this->m_nVideoH;
}
/* /*
@ -176,7 +180,7 @@ U = - 0.1687 R - 0.3313 G + 0.5 B + 128
V = 0.5 R - 0.4187 G - 0.0813 B + 128 V = 0.5 R - 0.4187 G - 0.0813 B + 128
????????RGB ?????????YUV (256????) ????: RGB YUV (256)
R = Y + 1.402 (Cr-128) R = Y + 1.402 (Cr-128)
@ -190,7 +194,6 @@ void CPlayWidget::initializeGL()
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
m_pVSHader = new QOpenGLShader(QOpenGLShader::Vertex, this); m_pVSHader = new QOpenGLShader(QOpenGLShader::Vertex, this);
bool bCompile = m_pVSHader->compileSourceCode(vsrcyuv); bool bCompile = m_pVSHader->compileSourceCode(vsrcyuv);
if(!bCompile) if(!bCompile)
{ {
@ -372,7 +375,6 @@ int CPlayWidget::loadYuvTexture()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, id_u); glBindTexture(GL_TEXTURE_2D, id_u);
// glPixelStorei(GL_UNPACK_ROW_LENGTH, m_buffer->StrideU());
glTexImage2D(GL_TEXTURE_2D, glTexImage2D(GL_TEXTURE_2D,
0, GL_RED, 0, GL_RED,
m_nVideoW/2, m_nVideoW/2,
@ -385,10 +387,9 @@ int CPlayWidget::loadYuvTexture()
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// v·ÖÁ¿ glActiveTexture(GL_TEXTURE2);// GL_TEXTURE2
glActiveTexture(GL_TEXTURE2);//???????????GL_TEXTURE2
glBindTexture(GL_TEXTURE_2D, id_v); glBindTexture(GL_TEXTURE_2D, id_v);
// glPixelStorei(GL_UNPACK_ROW_LENGTH, m_buffer->StrideV());
glTexImage2D(GL_TEXTURE_2D, glTexImage2D(GL_TEXTURE_2D,
0, GL_RED, 0, GL_RED,
m_nVideoW/2, m_nVideoW/2,
@ -412,7 +413,7 @@ int CPlayWidget::loadRtcI420Texture()
if(nullptr == m_buffer) if(nullptr == m_buffer)
return 0; return 0;
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
//???????y????????????
glBindTexture(GL_TEXTURE_2D, id_y); glBindTexture(GL_TEXTURE_2D, id_y);
glPixelStorei(GL_UNPACK_ROW_LENGTH, m_buffer->StrideY()); glPixelStorei(GL_UNPACK_ROW_LENGTH, m_buffer->StrideY());
glTexImage2D(GL_TEXTURE_2D, glTexImage2D(GL_TEXTURE_2D,
@ -430,7 +431,7 @@ int CPlayWidget::loadRtcI420Texture()
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//????u????????
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, id_u); glBindTexture(GL_TEXTURE_2D, id_u);
glPixelStorei(GL_UNPACK_ROW_LENGTH, m_buffer->StrideU()); glPixelStorei(GL_UNPACK_ROW_LENGTH, m_buffer->StrideU());
@ -446,8 +447,8 @@ int CPlayWidget::loadRtcI420Texture()
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// v·ÖÁ¿
glActiveTexture(GL_TEXTURE2);//???????????GL_TEXTURE2 glActiveTexture(GL_TEXTURE2);//GL_TEXTURE2
glBindTexture(GL_TEXTURE_2D, id_v); glBindTexture(GL_TEXTURE_2D, id_v);
glPixelStorei(GL_UNPACK_ROW_LENGTH, m_buffer->StrideV()); glPixelStorei(GL_UNPACK_ROW_LENGTH, m_buffer->StrideV());
glTexImage2D(GL_TEXTURE_2D, glTexImage2D(GL_TEXTURE_2D,

View File

@ -43,6 +43,8 @@ public:
int StopRender(); int StopRender();
int OnCameraData(uint8_t *); int OnCameraData(uint8_t *);
int SetImgSize(uint32_t width,uint32_t ); int SetImgSize(uint32_t width,uint32_t );
int RenderWidth();
int RenderHeight();
protected: protected:
QTimer tm; QTimer tm;
void initializeGL() override; void initializeGL() override;

View File

@ -1,6 +1,13 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "camera_video_sink.h" #include "camera_video_sink.h"
#include <QLabel>
#include "opencv2/opencv.hpp"
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include "cvhelper.h"
class AsyncRennder :public QSSASyncProcess{ class AsyncRennder :public QSSASyncProcess{
public: public:
@ -20,16 +27,34 @@ public:
qtimer->setSingleShot(false); qtimer->setSingleShot(false);
QObject::connect(qtimer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); QObject::connect(qtimer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
qtimer->start(10); qtimer->start(3);
while(state){ while(state){
if(mfbs ->Size() > 0){ if(mfbs ->Size() > 0){
uint8_t *frame = this->mfbs->TakeLast(); uint8_t *frame = this->mfbs->TakeLast();
cv::Mat yuv420p;
yuv420p.create(mUI->RenderHeight()*3/2,
mUI->RenderWidth(), CV_8UC1);
memcpy(yuv420p.data, frame, mUI->RenderHeight()*mUI->RenderWidth()
*sizeof(unsigned char)*3/2);
cv::Mat rgbImg;
cv::cvtColor(yuv420p, rgbImg, cv::COLOR_YUV2BGR_I420);
// cv::Mat dst;
// cv::rotate(rgbImg, dst, cv::ROTATE_90_CLOCKWISE); // 绕x翻转上下颠倒
// cv::imshow("img", dst);
//// cv::Mat gray, dst;
//// cvtColor(src, gray, COLOR_BGR2GRAY);
//// imshow("input", gray);
//// equalizeHist(gray, dst);
//// imshow("eq", dst);
// cv::waitKey(1);
mUI->OnCameraData(frame); mUI->OnCameraData(frame);
// qDebug()<<"dst size is "<<dst.size().width<<dst.size().height;
eventLoop.exec(); // 渲染一次 eventLoop.exec(); // 渲染一次
delete frame; delete frame;
} }
} }
} }
private: private:
@ -58,6 +83,14 @@ MainWindow::MainWindow(QWidget *parent)
info->GetDeviceName(i,name,100,nullptr,0,nullptr,0); info->GetDeviceName(i,name,100,nullptr,0,nullptr,0);
ui->comboBox->addItem(QString::asprintf("%s",name),i); ui->comboBox->addItem(QString::asprintf("%s",name),i);
} }
origin_picture = new QLabel(this);
processed_picture = new QLabel(this);
ui->gridLayout->addWidget(origin_picture,0,1);
origin_picture->setMaximumWidth(12000);
origin_picture->setMaximumHeight(12000);
ui->gridLayout->addWidget(processed_picture,1,0);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -105,15 +138,54 @@ void MainWindow::RenderDone()
} }
void MainWindow::on_pushButton_2_clicked()
cv::Mat *QImage2cvMat(QImage image)
{ {
CPlayWidget *ptr = new CPlayWidget(this); cv::Mat *mat;
CPlayWidget *ptr1 = new CPlayWidget(this); switch(image.format())
CPlayWidget *ptr2 = new CPlayWidget(this); {
case QImage::Format_ARGB32:
ui->gridLayout->addWidget(ptr,0,1); case QImage::Format_RGB32:
ui->gridLayout->addWidget(ptr1,1,0); case QImage::Format_ARGB32_Premultiplied:
ui->gridLayout->addWidget(ptr2,1,1); mat = new cv::Mat(image.height(),
image.width(),
CV_8UC4,
image.bits(),
image.bytesPerLine());
memcpy(mat->data,image.bits(),image.bytesPerLine()*image.height());
break;
case QImage::Format_RGB888:
mat = new cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.bits(), image.bytesPerLine());
// cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = new cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.bits(), image.bytesPerLine());
break;
}
Save1BitImage(*mat,"d://tgest.png");
return mat;
}
void MainWindow::on_pushButton_2_clicked()
{
QPixmap pix = ui->openGLWidget->grab();
QImage image = pix.toImage();
image = image.scaled(1280,720);
origin_picture->setPixmap(pix);
qDebug()<<"format is "<<image.format();
// cv::Mat Img;
// Img.create(pix.height()*3/2,
// pix.width(), CV_8UC1);
// memcpy(Img.data, pix.data_ptr().data(),
// pix.height()*pix.width()*sizeof(unsigned char)*3/2);
// qDebug()<<"pixmap size is "<<pix.size();
cv::Mat pic = *QImage2cvMat(image);
// qDebug()<<pic.size().width<<image.size();
cv::imshow("img", pic);
cv::waitKey(1);
this->processed_picture->setPixmap(QPixmap::fromImage(image));
} }

View File

@ -26,5 +26,7 @@ private slots:
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
std::unique_ptr<CameraVideoSink> m_capturer; std::unique_ptr<CameraVideoSink> m_capturer;
QLabel *origin_picture;
QLabel *processed_picture;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -59,7 +59,7 @@
<item> <item>
<widget class="QPushButton" name="pushButton_2"> <widget class="QPushButton" name="pushButton_2">
<property name="text"> <property name="text">
<string>增加窗口</string> <string>拍照</string>
</property> </property>
</widget> </widget>
</item> </item>

View File

@ -1,6 +1,6 @@
QT += core gui QT += core gui
include(D:\\project\\c++qt\\qsswraper\\qsswraper.pri) include(D:\\project\\qt_project\\qsswraper\\qsswraper.pri)
RESOURCES += $$PWD/qss/qss.qrc RESOURCES += $$PWD/qss/qss.qrc
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
@ -20,11 +20,10 @@ DEFINES += QT_DEPRECATED_WARNINGS NOMINMAX WEBRTC_WIN NOMINMAX WIN32_LEAN_AND_ME
INCLUDEPATH += third/include/ INCLUDEPATH += third/include/
LIBS += -L$$PWD/third/lib libwebrtc.lib ole32.lib oleaut32.lib strmiids.lib advapi32.lib
SOURCES += \ SOURCES += \
src/camera_video_sink.cpp \ src/camera_video_sink.cpp \
src/cplaywidget.cpp \ src/cplaywidget.cpp \
src/cvhelper.cpp \
src/main.cpp \ src/main.cpp \
src/mainwindow.cpp \ src/mainwindow.cpp \
src/video_source_impl.cpp src/video_source_impl.cpp
@ -32,6 +31,7 @@ SOURCES += \
HEADERS += \ HEADERS += \
src/camera_video_sink.h \ src/camera_video_sink.h \
src/cplaywidget.h \ src/cplaywidget.h \
src/cvhelper.h \
src/mainwindow.h \ src/mainwindow.h \
src/rtc.h \ src/rtc.h \
src/video_source_impl.h src/video_source_impl.h
@ -43,3 +43,12 @@ FORMS += \
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
CONFIG(debug, debug|release){
message("debug mode")
LIBS += -L$$PWD/third/lib libwebrtc.lib ole32.lib oleaut32.lib strmiids.lib advapi32.lib opencv_core455d.lib
LIBS +=opencv_stitching455d.lib opencv_objdetect455d.lib opencv_ml455d.lib opencv_imgcodecs455d.lib opencv_imgproc455d.lib
LIBS+= opencv_highgui455d.lib
DEFINES += DEBUG_FLAG
Qt += debug
}