主线程用qt 设计师建了一个qslider用来调参,camera子线程使用opencv打开摄像头,想要在ui主线程中滑块移动时获取参数connect与槽函数连接,实现capture.set(CV_PROPS_BRIGHTNESS)对摄像头参数调整,
camera线程.h
class Camera : public QObject
{
Q_OBJECT
VideoCapture capture; //视频流捕获器
static int cameracount; //存储相机数目
signals:
void operate(int)
.cpp使用打开摄像头
bool Camera::open(int _index)
{
qDebug() << "open index= " << _index;
if (capture.open(_index, CAP_DSHOW))
return true;
else
return false;
}
void Camera::Operate(int _index)
{
if (open(_index)) { qDebug() << "Camera open success!"; }
else { qDebug() << "Camera open failed!"; return; }
while (1)
{
//clear cam index, create ini,output
/*for (int i = 0; i < 20;) {
capture.get(i);
}*/
qApp->processEvents();
Mat matin = read(); //视频流读入
matnow = matin;
QImage image = Mat2QImage(matin);
emit updateImage(image);
}
}
ui app0.h文件
class app0 : public QMainWindow
{
Q_OBJECT
Camera* camera;
QThread thread;
QThread ocr_thread;
public:
app0(QWidget *parent = Q_NULLPTR);
~app0();
private:
Ui::app0Class* ui=new Ui::app0Class();
VideoCapture capture;
QTimer *timer;
Mat frame;
bool isCamera = 0;
int imgcount = 0;
private slots:
void on_camera_index1_clicked(int);
void set_brightness();
protected:
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
signals:
void cameraOperate(int);
ui .c文件
app0::app0(QWidget *parent)
: QMainWindow(parent)
{
ui->setupUi(this);
this->setFixedSize(this->size());
this->setWindowTitle("Camera Test by St, 2020.2.9");
//Camera::getCameraCount()==0,//camera.open == false, QMessageBox::information(this, "System Failure", "没有找到可用摄像头", QMessageBox::OK);
for (int i = 0; i<Camera::getCameraCount();)//摄像头启动时禁止调参?
ui->comboBox->addItem(QString::number(i++)); //name camera
camera = new Camera();
camera->moveToThread(&thread); //将camera对象放在子线程,不推荐放在主线程执行。
connect(this, SIGNAL(cameraOperate(int)), camera, SLOT(Operate(int))); //camera的槽函数将在thread所在的线程执行
connect(camera, SIGNAL(updateImage(QImage)), this, SLOT(updateImage(QImage))); //将采集的图像传入主线程(UI线程)
//connect(ui->camera_index2, &QPushButton::click, thread, SLOT(set_brightness()));
//connect(ui->camera_index1, SIGNAL(clicked()), this, SLOT(onClicked()));//连接函数
}
void app0::set_brightness() {
capture.set(CAP_PROP_BRIGHTNESS, 滑块参数); //value changed 和get value
}