项目场景:
最近在vs上联调qt+opencv,光配置环境就配置了一个星期555,配置好环境后仿照教程
https://blog.csdn.net/skeeee/article/details/10187561
编写了第一个联调的demo
后来参考浅墨大神的例程
https://blog.csdn.net/poem_qianmo/article/details/21479533
准备将该项目中的部分功能移植到qt项目之中。
<1>创建轨迹条——Qslider
在界面中插入轨迹条Qslider,分别用于控制视频亮度和对比度。
并连接起信号value_change()与自定义槽函数lumichange(),与ContrastChange();
槽函数定义:
void QTGrapher::LumiChange()
{
int pos = ui.slider->value();
int con_pos=ui.con_slider->value();
QString str = QString("%1").arg(pos);
if( !image2.data )
{
image2 = cv::imread("C:\\Users\\14405\\Desktop\\xiao\\C++\\test3.jpg");//读取图像
cv::cvtColor(image2, image2, CV_RGB2RGBA);//图像在QT显示前,必须转化成QImage格式,将RGBA格式转化成RGB
cv::resize(image2, image2, Size(200, 200));
}
if(!dst.data)
{
dst = cv::imread("C:\\Users\\14405\\Desktop\\xiao\\C++\\test3.jpg");//读取图像
cv::resize(dst, dst, Size(200, 200));
}
//三个for循环,执行运算 new_image(i,j) =a*image(i,j) + b
for(int y = 0; y < image2.rows; y++ )
{
for(int x = 0; x < image2.cols-50; x++ )
{
for(int c = 0; c < 3; c++ )
{
// dst.at<Vec3b>(y,x)[c]= cv::saturate_cast<uchar>( (con_pos*0.01)*(image2.at<Vec3b>(y,x)[c] ) + pos );
dst.at<cv::Vec3b>(y,x)[c]= image2.at<cv::Vec3b>(y,x)[c] ;
}
}
}
cv::cvtColor(dst, dst, CV_RGB2RGBA);//图像在QT显示前,必须转化成QImage格式,将RGBA格式转化成RGB
// cv::resize(dst, dst, Size(400, 500));
QImage img = QImage((const unsigned char*)(dst.data),
dst.cols, dst.rows, QImage::Format_RGB32);
ui.label->setPixmap(QPixmap::fromImage(img));
ui.label->resize(ui.label->pixmap()->size());
ui.label->show();
ui.lineEdit_light->setText(str);
}
void QTGrapher::ContrastChange()
{
int pos = ui.slider->value();
int con_pos=ui.con_slider->value();
QString str = QString("%1").arg(pos);
if( !image2.data )
{
image2 = cv::imread("C:\\Users\\14405\\Desktop\\xiao\\C++\\test3.jpg");//读取图像
}
if(!dst.data)
{
dst = cv::imread("C:\\Users\\14405\\Desktop\\xiao\\C++\\test3.jpg");//读取图像
}
//三个for循环,执行运算 new_image(i,j) =a*image(i,j) + b
for(int y = 0; y < image2.rows; y++ )
{
for(int x = 0; x < image2.cols; x++ )
{
for(int c = 0; c < 3; c++ )
{
dst.at<cv::Vec3b>(y,x)[c]= saturate_cast<uchar>( (con_pos*0.01)*(image2.at<cv::Vec3b>(y,x)[c] ) + pos );//防溢出保护
}
}
}
cv::cvtColor(dst, dst, CV_RGB2RGBA);//图像在QT显示前,必须转化成QImage格式,将RGBA格式转化成RGB
cv::resize(dst, dst, Size(400, 500));
QImage img = QImage((const unsigned char*)(dst.data),
dst.cols, dst.rows, QImage::Format_RGB32);
ui.label->setPixmap(QPixmap::fromImage(img));
ui.label->resize(ui.label->pixmap()->size());
ui.label->show();
ui.lineEdit_con->setText(str);
}
<2>添加各类功能按钮——Qbutton
信号与槽函数的连接关系如图
对应代码如下:
void QTGrapher::to_gray()
{
if( !image2.data )
{
image2 = cv::imread("C:\\Users\\14405\\Desktop\\xiao\\C++\\test3.jpg");//读取图像
}
if(!dst.data)
{
dst = cv::imread("C:\\Users\\14405\\Desktop\\xiao\\C++\\test3.jpg");//读取图像
}
cvtColor(image2, dst, CV_BGR2GRAY);
cvtColor(dst, dst, CV_GRAY2RGBA);
cv::resize(dst, dst, Size(400, 500));
QImage img = QImage((const unsigned char*)(dst.data),
dst.cols, dst.rows, QImage::Format_RGB32);
ui.label->setPixmap(QPixmap::fromImage(img));
ui.label->resize(ui.label->pixmap()->size());
ui.label->show();
}
void QTGrapher::ReadFrame()
{
//获取图像帧
capture>>frame;
/*
//将抓取到的帧,转换为QImage格式,QImage::Format_RGB888使用24位RGB格式(8-8-8)存储图像
//此时没有使用rgbSwapped()交换所有像素的红色和蓝色分量的值,底色偏蓝
QImage image = QImage((const uchar*)frame.data,frame.cols, frame.rows,QImage::Format_RGB888);
//将图片显示到label上
ui->label->setPixmap(QPixmap::fromImage(image));
*/
//将视频显示到label上
QImage img = QImage((const uchar*)frame.data,frame.cols,frame.rows,QImage::Format_RGB888).rgbSwapped();
ui.label->setPixmap(QPixmap::fromImage(img));
ui.label->resize(ui.label->pixmap()->size());
ui.label->setPixmap(QPixmap::fromImage(img));
}
//打开摄像头
void QTGrapher::OpenCameraClicked()
{
capture.open(0);//打开摄像头
timer->start(25);//开启定时器,一次25ms
}
//关闭摄像头
void QTGrapher::CloseCameraClicked()
{
timer->stop();//关闭定时器
capture.release();//释放图像
}
void QTGrapher::OpenImageClick()
{
if( !image.data )
{
image = cv::imread("C:\\Users\\14405\\Desktop\\xiao\\C++\\test3.jpg");//读取图像
cv::cvtColor(image, image,CV_RGB2RGBA);//图像在QT显示前,必须转化成QImage格式,将RGBA格式转化成RGB
cv::resize(image, image, Size(400, 500));
}
QImage img = QImage((const unsigned char*)(image.data),
image.cols, image.rows, QImage::Format_RGB32);
ui.label->setPixmap(QPixmap::fromImage(img));
ui.label->resize(ui.label->pixmap()->size());
ui.label->show();
}
void QTGrapher::ExitClick()
{
cv::flip(image,result,1);//对图像进行翻转
cv::resize(result, result, Size(400, 500));
QImage img = QImage((const unsigned char*)(result.data),
result.cols, result.rows, QImage::Format_RGB32);
ui.label->setPixmap(QPixmap::fromImage(img));
ui.label->resize(ui.label->pixmap()->size());
ui.label->show();
}
运行起来的整体界面如下: