|
@@ -283,8 +283,6 @@ Frames::setVideoSink( QVideoSink * newVideoSink )
|
|
|
m_videoSink = newVideoSink;
|
|
m_videoSink = newVideoSink;
|
|
|
m_capture.setVideoSink(m_videoSink);
|
|
m_capture.setVideoSink(m_videoSink);
|
|
|
|
|
|
|
|
- //connect(m_videoSink, &QVideoSink::videoFrameChanged,this, &Frames::newFrame);
|
|
|
|
|
-
|
|
|
|
|
emit videoSinkChanged();
|
|
emit videoSinkChanged();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -552,44 +550,6 @@ Frames::setCameraSw(bool stat_cam)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
- * movx b
|
|
|
|
|
- * movz b
|
|
|
|
|
- * movx f
|
|
|
|
|
-*/
|
|
|
|
|
-void
|
|
|
|
|
-Frames::newFrame( const QVideoFrame & frame )
|
|
|
|
|
-{
|
|
|
|
|
- if(!isDecoding()&&m_processThread.isFinished()){
|
|
|
|
|
- m_decoding = true;
|
|
|
|
|
- m_processThread = QtConcurrent::run([=]() {
|
|
|
|
|
- m_image = frame.toImage().convertToFormat(QImage::Format_RGB32);
|
|
|
|
|
- if (m_image.isNull()) {
|
|
|
|
|
- m_decoding = false;
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- m_fps_timer->incrementFrame(); //启动fps刷新定时器
|
|
|
|
|
- { //opencv实现的清晰度评价
|
|
|
|
|
- #if 1
|
|
|
|
|
- Mat f_mat = QImageToCvMat(m_image,true);
|
|
|
|
|
- m_qulityString = m_opencvfunc.brenner(f_mat);
|
|
|
|
|
- #else //
|
|
|
|
|
- m_qulityString = m_opencvfunc.calculateSharpness(m_image);
|
|
|
|
|
- #endif
|
|
|
|
|
- }
|
|
|
|
|
- m_decoding = false;
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- //m_frame = frame;
|
|
|
|
|
- //qDebug() << "newFrame:"<<m_fps;
|
|
|
|
|
- //++m_fps;
|
|
|
|
|
- //if(m_fps%10==0)
|
|
|
|
|
- //emit frameAvailable(m_frame);
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
void
|
|
void
|
|
|
Frames::upload_handle()
|
|
Frames::upload_handle()
|
|
@@ -722,75 +682,101 @@ Frames::mov_x_z_run(){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-void __stdcall Frames::ImageCallBackEx(
|
|
|
|
|
- unsigned char* pData,
|
|
|
|
|
- MV_FRAME_OUT_INFO_EX* pFrameInfo,
|
|
|
|
|
- void* pUser
|
|
|
|
|
- )
|
|
|
|
|
-{
|
|
|
|
|
- Frames* self = static_cast<Frames*>(pUser);
|
|
|
|
|
-
|
|
|
|
|
- // ❗禁止阻塞、禁止 Qt、禁止 OpenCV
|
|
|
|
|
- FramePacket pkt;
|
|
|
|
|
- pkt.data = pData;
|
|
|
|
|
- pkt.width = pFrameInfo->nWidth;
|
|
|
|
|
- pkt.height = pFrameInfo->nHeight;
|
|
|
|
|
- pkt.pixelType = pFrameInfo->enPixelType;
|
|
|
|
|
- pkt.timestamp = pFrameInfo->nDevTimeStampHigh;
|
|
|
|
|
-
|
|
|
|
|
- self->pushFrame(pkt); // lock-free
|
|
|
|
|
- self->m_fps_timer->incrementFrame();
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void Frames::convertAndSend(const FramePacket& pkt)
|
|
|
|
|
-{
|
|
|
|
|
- cv::Mat raw(pkt.height, pkt.width, CV_8UC1, pkt.data);
|
|
|
|
|
-
|
|
|
|
|
- cv::cvtColor(raw, m_frame, cv::COLOR_BayerGR2RGBA);
|
|
|
|
|
-
|
|
|
|
|
- QVideoFrameFormat fmt(
|
|
|
|
|
- QSize(pkt.width, pkt.height),
|
|
|
|
|
- QVideoFrameFormat::Format_BGRA8888
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- QVideoFrame frame(fmt);
|
|
|
|
|
- if (frame.map(QVideoFrame::WriteOnly)) {
|
|
|
|
|
- memcpy(frame.bits(0), m_frame.data, m_frame.total() * m_frame.elemSize());
|
|
|
|
|
- frame.unmap();
|
|
|
|
|
|
|
+void
|
|
|
|
|
+Frames::saveMatAsJpeg(const cv::Mat &mat){
|
|
|
|
|
+ QString ts = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
|
|
|
|
|
+ if (!mat.empty()) {
|
|
|
|
|
+ std::vector<int> params = {
|
|
|
|
|
+ cv::IMWRITE_JPEG_QUALITY,
|
|
|
|
|
+ 90
|
|
|
|
|
+ };
|
|
|
|
|
+ //QImage img = f.toImage().convertToFormat(QImage::Format_RGB32);
|
|
|
|
|
+ QString name = picture_path_floader + "/" + ts +
|
|
|
|
|
+ "z" + QString::number(z_step) +
|
|
|
|
|
+ "x" + QString::number(x_step) + ".jpg";
|
|
|
|
|
+ bool ok = cv::imwrite(name.toStdString(), mat, params);
|
|
|
|
|
+ if (!ok) {
|
|
|
|
|
+ WARN("[saveMatAsJpeg] write failed:{}",name.toStdString());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ param::ptlist.append(name);
|
|
|
|
|
+ LOG("[saveMatAsJpeg] saved:{}",name.toStdString());
|
|
|
|
|
+ mov_x_z_run();
|
|
|
|
|
+ }
|
|
|
|
|
+ }else{
|
|
|
|
|
+ ERROR("[saveMatAsJpeg] empty image!");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- QMetaObject::invokeMethod(
|
|
|
|
|
- m_videoSink,
|
|
|
|
|
- [this, frame]() {
|
|
|
|
|
- if (m_videoSink)
|
|
|
|
|
- m_videoSink->setVideoFrame(frame);
|
|
|
|
|
- },
|
|
|
|
|
- Qt::QueuedConnection
|
|
|
|
|
- );
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-void Frames::processLoop(bool sta)
|
|
|
|
|
-{
|
|
|
|
|
- while (sta) {
|
|
|
|
|
- if (readIndex == writeIndex) {
|
|
|
|
|
- QThread::usleep(100);
|
|
|
|
|
- continue;
|
|
|
|
|
|
|
+void
|
|
|
|
|
+Frames::processFrameAsync(const RawFrame& frame){
|
|
|
|
|
+ QtConcurrent::run([=]() {
|
|
|
|
|
+ cv::Mat raw;
|
|
|
|
|
+ switch (frame.pixelType) {
|
|
|
|
|
+ case PixelType_Gvsp_BayerRG8:
|
|
|
|
|
+ raw = cv::Mat(frame.height, frame.width, CV_8UC1,
|
|
|
|
|
+ (void*)frame.data.constData());
|
|
|
|
|
+ cv::cvtColor(raw, m_frame, cv::COLOR_BayerRG2BGRA);
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case PixelType_Gvsp_BayerGR8:
|
|
|
|
|
+ raw = cv::Mat(frame.height, frame.width, CV_8UC1,
|
|
|
|
|
+ (void*)frame.data.constData());
|
|
|
|
|
+ cv::cvtColor(raw, m_frame, cv::COLOR_BayerGR2BGRA);
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case PixelType_Gvsp_BayerBG8:
|
|
|
|
|
+ raw = cv::Mat(frame.height, frame.width, CV_8UC1,
|
|
|
|
|
+ (void*)frame.data.constData());
|
|
|
|
|
+ cv::cvtColor(raw, m_frame, cv::COLOR_BayerBG2BGRA);
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case PixelType_Gvsp_YUV422_YUYV_Packed:
|
|
|
|
|
+ raw = cv::Mat(frame.height, frame.width, CV_8UC2,
|
|
|
|
|
+ (void*)frame.data.constData());
|
|
|
|
|
+ cv::cvtColor(raw, m_frame, cv::COLOR_YUV2RGBA_YUYV);
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- FramePacket pkt = frameQueue[readIndex];
|
|
|
|
|
- readIndex.store((readIndex + 1) % QUEUE_SIZE,
|
|
|
|
|
- std::memory_order_release);
|
|
|
|
|
|
|
+ if(param::camera_tc==1||param::camera_tc==100){
|
|
|
|
|
+ cv::Mat snapshot = m_frame.clone(); // 只 clone 一次
|
|
|
|
|
+ QtConcurrent::run([=](){
|
|
|
|
|
+ saveMatAsJpeg(snapshot);
|
|
|
|
|
+ });
|
|
|
|
|
+ if (param::camera_tc == 100) {
|
|
|
|
|
+ param::camera_tc = 0;
|
|
|
|
|
+ emit stopGrabbing(false);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ param::camera_tc = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ QVideoFrameFormat vfmt(
|
|
|
|
|
+ QSize(m_frame.cols, m_frame.rows),
|
|
|
|
|
+ QVideoFrameFormat::Format_RGBA8888
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ QVideoFrame vf(vfmt);
|
|
|
|
|
+ if (!vf.map(QVideoFrame::WriteOnly))
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ memcpy(vf.bits(0), m_frame.data,
|
|
|
|
|
+ m_frame.total() * m_frame.elemSize());
|
|
|
|
|
+ vf.unmap();
|
|
|
|
|
+
|
|
|
|
|
+ // ✅ 投递回 UI 线程
|
|
|
|
|
+ QMetaObject::invokeMethod(m_videoSink, [this, vf]() {
|
|
|
|
|
+ if (m_videoSink) {
|
|
|
|
|
+ m_videoSink->setVideoFrame(vf);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, Qt::QueuedConnection);
|
|
|
|
|
|
|
|
- convertAndSend(pkt);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
void
|
|
void
|
|
|
Frames::handleGrab(bool sta_cam){
|
|
Frames::handleGrab(bool sta_cam){
|
|
|
- #if 1
|
|
|
|
|
if (m_grabThread.isRunning()) return;
|
|
if (m_grabThread.isRunning()) return;
|
|
|
if(m_grabThread.isFinished()){
|
|
if(m_grabThread.isFinished()){
|
|
|
m_grabThread = QtConcurrent::run([=]() {
|
|
m_grabThread = QtConcurrent::run([=]() {
|
|
@@ -811,8 +797,6 @@ Frames::handleGrab(bool sta_cam){
|
|
|
if(!handle) return;
|
|
if(!handle) return;
|
|
|
const uint32_t payload = stParam.nCurValue;
|
|
const uint32_t payload = stParam.nCurValue;
|
|
|
LOG("payload size:{}",payload);
|
|
LOG("payload size:{}",payload);
|
|
|
- //MV_FRAME_OUT_INFO_EX stImageInfo = {0};
|
|
|
|
|
- QVideoFrameFormat vfmt(QSize(0,0), QVideoFrameFormat::Format_BGRA8888);
|
|
|
|
|
MV_FRAME_OUT stImageInfo = {0};
|
|
MV_FRAME_OUT stImageInfo = {0};
|
|
|
|
|
|
|
|
bool flag_cam = sta_cam;
|
|
bool flag_cam = sta_cam;
|
|
@@ -824,76 +808,30 @@ Frames::handleGrab(bool sta_cam){
|
|
|
ERROR("GetOneFrameTimeout:{:x}",nRet);
|
|
ERROR("GetOneFrameTimeout:{:x}",nRet);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
-#if 1
|
|
|
|
|
- cv::Mat raw(stImageInfo.stFrameInfo.nHeight, stImageInfo.stFrameInfo.nWidth, CV_8UC1, stImageInfo.pBufAddr);
|
|
|
|
|
- switch (stImageInfo.stFrameInfo.enPixelType) {
|
|
|
|
|
- case PixelType_Gvsp_BayerRG8:
|
|
|
|
|
- cv::cvtColor(raw, m_frame, cv::COLOR_BayerRG2BGRA);
|
|
|
|
|
- break;
|
|
|
|
|
- case PixelType_Gvsp_BayerGR8:
|
|
|
|
|
- cv::cvtColor(raw, m_frame, cv::COLOR_BayerGR2BGRA);
|
|
|
|
|
- break;
|
|
|
|
|
- case PixelType_Gvsp_BayerBG8: //MV-CU050-90GC
|
|
|
|
|
- cv::cvtColor(raw, m_frame, cv::COLOR_BayerBG2BGRA);
|
|
|
|
|
- break;
|
|
|
|
|
- default:
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- vfmt = QVideoFrameFormat(QSize(m_frame.cols, m_frame.rows),
|
|
|
|
|
- QVideoFrameFormat::Format_RGBA8888);
|
|
|
|
|
- QVideoFrame f(vfmt);
|
|
|
|
|
- if (f.map(QVideoFrame::WriteOnly)) {
|
|
|
|
|
- memcpy(f.bits(0), m_frame.data, m_frame.total() * m_frame.elemSize());
|
|
|
|
|
- f.unmap();
|
|
|
|
|
-
|
|
|
|
|
- QMetaObject::invokeMethod(m_videoSink, [this,f]() {
|
|
|
|
|
- if (m_videoSink) {
|
|
|
|
|
- m_videoSink->setVideoFrame(f);
|
|
|
|
|
- }
|
|
|
|
|
- }, Qt::QueuedConnection);
|
|
|
|
|
- }
|
|
|
|
|
-#endif
|
|
|
|
|
|
|
+ RawFrame frame;
|
|
|
|
|
+ frame.width = stImageInfo.stFrameInfo.nWidth;
|
|
|
|
|
+ frame.height = stImageInfo.stFrameInfo.nHeight;
|
|
|
|
|
+ frame.pixelType = stImageInfo.stFrameInfo.enPixelType;
|
|
|
|
|
+ const int dataSize = stImageInfo.stFrameInfo.nFrameLen;
|
|
|
|
|
+ frame.data = QByteArray(
|
|
|
|
|
+ (const char*)stImageInfo.pBufAddr,
|
|
|
|
|
+ dataSize
|
|
|
|
|
+ );
|
|
|
|
|
+ MV_CC_FreeImageBuffer(handle, &stImageInfo);
|
|
|
|
|
+ // ✅ 像素处理放到新的 QFuture
|
|
|
|
|
+ processFrameAsync(frame);
|
|
|
// ----- FPS -----
|
|
// ----- FPS -----
|
|
|
m_fps_timer->incrementFrame();
|
|
m_fps_timer->incrementFrame();
|
|
|
#if 1
|
|
#if 1
|
|
|
- if(param::camera_tc==1||param::camera_tc==100){
|
|
|
|
|
- QString ts = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
|
|
|
|
|
- if (f.isValid()) {
|
|
|
|
|
- QImage img = f.toImage().convertToFormat(QImage::Format_RGB32);
|
|
|
|
|
- QString name = picture_path_floader + "/" + ts +
|
|
|
|
|
- "z" + QString::number(z_step) +
|
|
|
|
|
- "x" + QString::number(x_step) + ".jpg";
|
|
|
|
|
-
|
|
|
|
|
- if(img.save(name, "jpg", 80)){
|
|
|
|
|
- param::ptlist.append(name);
|
|
|
|
|
- LOG("图片已保存:{}", name.toStdString());
|
|
|
|
|
- mov_x_z_run();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (param::camera_tc == 100) {
|
|
|
|
|
- param::camera_tc = 0;
|
|
|
|
|
- emit stopGrabbing(false);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- param::camera_tc = 0;
|
|
|
|
|
- }
|
|
|
|
|
if (param::camera_tc == -1) {
|
|
if (param::camera_tc == -1) {
|
|
|
emit stopGrabbing(false);
|
|
emit stopGrabbing(false);
|
|
|
flag_cam = false;
|
|
flag_cam = false;
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
#endif
|
|
#endif
|
|
|
- MV_CC_FreeImageBuffer(handle, &stImageInfo);
|
|
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
-#else
|
|
|
|
|
- // 使用 pthread 创建线程
|
|
|
|
|
- pthread_t pthreadId;
|
|
|
|
|
- pthread_create(&pthreadId, NULL, pthreadGrab, NULL);
|
|
|
|
|
- //pthread_join(pthreadId, NULL); // 等待线程完成
|
|
|
|
|
-#endif
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1077,27 +1015,24 @@ Frames::saveImage()
|
|
|
{
|
|
{
|
|
|
//停止清晰度发送,关闭定时器m1s
|
|
//停止清晰度发送,关闭定时器m1s
|
|
|
LOG("saveImage start!");
|
|
LOG("saveImage start!");
|
|
|
- #ifdef focus_board
|
|
|
|
|
- if(camera_sw()==true)
|
|
|
|
|
- param::camera_tc = 1;
|
|
|
|
|
- #else
|
|
|
|
|
//灰度值检测,全黑则不再往下进行,上报预警消息
|
|
//灰度值检测,全黑则不再往下进行,上报预警消息
|
|
|
if(m_frame.empty()) return;
|
|
if(m_frame.empty()) return;
|
|
|
- Mat image_gray;
|
|
|
|
|
- cv::cvtColor(m_frame,image_gray,cv::COLOR_RGBA2GRAY);
|
|
|
|
|
- double grayvalue = cv::mean(image_gray)[0];
|
|
|
|
|
- m_qulityString = m_opencvfunc.Tenengrad(m_frame);
|
|
|
|
|
- emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
|
|
|
|
|
- emit qulityStringChanged();
|
|
|
|
|
- LOG("image_gray:{}",grayvalue);
|
|
|
|
|
- LOG("Tenengrad:{}",m_qulityString.toStdString());
|
|
|
|
|
- if(grayvalue<10.0){
|
|
|
|
|
- emit dataUpload(m_json.MakeJsonWarn("Photo ERROR",QString::number(grayvalue)));
|
|
|
|
|
- setCameraSw(false); //关闭相机
|
|
|
|
|
- emit signal_photoerr();
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- #endif
|
|
|
|
|
|
|
+ QtConcurrent::run([=](){
|
|
|
|
|
+ Mat image_gray;
|
|
|
|
|
+ cv::cvtColor(m_frame,image_gray,cv::COLOR_RGBA2GRAY);
|
|
|
|
|
+ double grayvalue = cv::mean(image_gray)[0];
|
|
|
|
|
+ m_qulityString = m_opencvfunc.Tenengrad(m_frame);
|
|
|
|
|
+ emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
|
|
|
|
|
+ emit qulityStringChanged();
|
|
|
|
|
+ LOG("image_gray:{}",grayvalue);
|
|
|
|
|
+ LOG("Tenengrad:{}",m_qulityString.toStdString());
|
|
|
|
|
+ if(grayvalue<10.0){
|
|
|
|
|
+ emit dataUpload(m_json.MakeJsonWarn("Photo ERROR",QString::number(grayvalue)));
|
|
|
|
|
+ setCameraSw(false); //关闭相机
|
|
|
|
|
+ emit signal_photoerr();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|