From 643a6639534569e817d07f9bb79c8814e81345ac Mon Sep 17 00:00:00 2001
From: zhenye
Date: Wed, 18 Dec 2024 16:41:24 +0800
Subject: [PATCH] =?UTF-8?q?ui=E5=AE=8C=E6=95=B4=E7=89=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
globals.cpp | 8 ++++
globals.h | 14 ++++++
img_utils.cpp | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++
img_utils.h | 27 ++++++++++++
ui_widget.h | 6 ++-
widget.cpp | 2 +-
widget.ui | 8 +++-
7 files changed, 177 insertions(+), 5 deletions(-)
create mode 100644 globals.cpp
create mode 100644 globals.h
create mode 100644 img_utils.cpp
create mode 100644 img_utils.h
diff --git a/globals.cpp b/globals.cpp
new file mode 100644
index 0000000..5be4788
--- /dev/null
+++ b/globals.cpp
@@ -0,0 +1,8 @@
+// globals.cpp
+#include "globals.h"
+
+// 初始化全局互斥锁
+QMutex gPicMutex0;
+
+// 初始化全局 MIL_ID
+MIL_ID gCurrentPicId0 = 0;
diff --git a/globals.h b/globals.h
new file mode 100644
index 0000000..e804c03
--- /dev/null
+++ b/globals.h
@@ -0,0 +1,14 @@
+// globals.h
+#ifndef GLOBALS_H
+#define GLOBALS_H
+
+#include
+#include
+
+// 全局互斥锁,保护 current_pic_id 的访问
+extern QMutex gPicMutex0;
+
+// 全局变量,用于存储当前的 MIL_ID
+extern MIL_ID gCurrentPicId0;
+
+#endif // GLOBALS_H
diff --git a/img_utils.cpp b/img_utils.cpp
new file mode 100644
index 0000000..40fb322
--- /dev/null
+++ b/img_utils.cpp
@@ -0,0 +1,117 @@
+#include "img_utils.h"
+
+
+ImageUtils::ImageUtils() {}
+
+QPixmap ImageUtils::mat2QPixmap(const cv::Mat& mat)
+{
+ if(mat.empty())
+ return QPixmap(); // 返回空的 QPixmap
+
+ QImage img;
+
+ // 根据 Mat 的通道数选择不同的转换方式
+ if(mat.channels() == 1){
+ // 灰度图像
+ img = QImage(mat.data, mat.cols, mat.rows, static_cast(mat.step), QImage::Format_Grayscale8).copy();
+ }
+ else if(mat.channels() == 3){
+ // 彩色图像 (OpenCV 默认是 BGR,需要转换为 RGB)
+ cv::Mat rgb;
+ cv::cvtColor(mat, rgb, cv::COLOR_BGR2RGB);
+ img = QImage(rgb.data, rgb.cols, rgb.rows, static_cast(rgb.step), QImage::Format_RGB888).copy();
+ }
+ else if(mat.channels() == 4){
+ // 如果需要处理带有透明通道的图像 (BGRA 转 RGBA)
+ cv::Mat rgba;
+ cv::cvtColor(mat, rgba, cv::COLOR_BGRA2RGBA);
+ img = QImage(rgba.data, rgba.cols, rgba.rows, static_cast(rgba.step), QImage::Format_RGBA8888).copy();
+ }
+ else{
+ // 不支持的图像格式
+ std::cout << "Unsupported Mat format with channels:" << mat.channels();
+ return QPixmap();
+ }
+
+ return QPixmap::fromImage(img);
+}
+
+cv::Mat ImageUtils::mil2Mat(const MIL_ID mil_img)
+{
+ using namespace cv;
+
+ // 获取 MIL 图像的宽度、高度和通道数
+ MIL_INT width, height, channels, bitDepth;
+
+ MbufInquire(mil_img, M_SIZE_X, &width);
+ MbufInquire(mil_img, M_SIZE_Y, &height);
+ MbufInquire(mil_img, M_SIZE_BAND, &channels);
+ MbufInquire(mil_img, M_SIZE_BIT, &bitDepth);
+
+ if (channels == 1) {
+ // 单通道图像,直接读取整个缓冲区
+ Mat grayImage(height, width, CV_8UC1);
+ if (bitDepth == 1) {
+ MIL_ID temp_img;
+ convert_to_uint8(mil_img, temp_img);
+ MbufGet(temp_img, grayImage.data);
+ MbufFree(temp_img);
+ } else {
+ MbufGet(mil_img, grayImage.data);
+ }
+ return grayImage;
+ }
+ if (channels == 3) {
+ // 多通道图像,分通道读取
+ MIL_ID redChannel, greenChannel, blueChannel;
+ MbufAlloc2d(M_DEFAULT, width, height, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &redChannel);
+ MbufAlloc2d(M_DEFAULT, width, height, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &greenChannel);
+ MbufAlloc2d(M_DEFAULT, width, height, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &blueChannel);
+
+ // 将 MIL 图像的各个通道复制到单通道缓冲区
+ MbufCopyColor(mil_img, redChannel, M_RED);
+ MbufCopyColor(mil_img, greenChannel, M_GREEN);
+ MbufCopyColor(mil_img, blueChannel, M_BLUE);
+
+ // 分别读取每个通道的数据
+ Mat redMat(height, width, CV_8UC1);
+ Mat greenMat(height, width, CV_8UC1);
+ Mat blueMat(height, width, CV_8UC1);
+ MbufGet(redChannel, redMat.data);
+ MbufGet(greenChannel, greenMat.data);
+ MbufGet(blueChannel, blueMat.data);
+ // 释放通道缓冲区
+ MbufFree(redChannel);
+ MbufFree(greenChannel);
+ MbufFree(blueChannel);
+
+ // 合并通道
+ std::vector bgrChannels = {blueMat, greenMat, redMat};
+ Mat colorImage;
+ cv::merge(bgrChannels, colorImage);
+
+ return colorImage;
+ }
+ // 不支持的通道数
+ std::cerr << "[Error] Unsupported number of channels: " << channels << std::endl;
+ return Mat();
+
+
+}
+
+void ImageUtils::convert_to_uint8(const MIL_ID& input_img, MIL_ID& output_img) {
+ MIL_INT size_x = MbufInquire(input_img, M_SIZE_X, M_NULL);
+ MIL_INT size_y = MbufInquire(input_img, M_SIZE_Y, M_NULL);
+ MIL_INT channel_num = MbufInquire(input_img, M_SIZE_BAND, M_NULL);
+
+ MbufAlloc2d(MilSystem, size_x, size_y, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &output_img);
+ if(channel_num == 1) {
+ MimArith(output_img, input_img, output_img, M_ADD);
+ MimArith(output_img, 255.0, output_img, M_MULT_CONST);
+ } else if(channel_num == 3) {
+ MimConvert(input_img, output_img, M_RGB_TO_L);
+ MimArith(output_img, M_NULL, output_img, M_NOT);
+ } else {
+ std::cout << "Unsupported channel number!" << std::endl;
+ }
+}
diff --git a/img_utils.h b/img_utils.h
new file mode 100644
index 0000000..ab9babe
--- /dev/null
+++ b/img_utils.h
@@ -0,0 +1,27 @@
+// utils.h
+#ifndef IMG_UTILS_H
+#define IMG_UTILS_H
+
+#include
+#include
+#include
+#include
+#include
+
+extern MIL_ID MilSystem;
+
+class ImageUtils
+{
+public:
+ ImageUtils(); // 构造函数
+
+ // 将 cv::Mat 转换为 QPixmap 的静态函数
+ static QPixmap mat2QPixmap(const cv::Mat& mat);
+
+ // 将 MIL_ID 转换为 cv::Mat 的静态函数
+ static cv::Mat mil2Mat(const MIL_ID mil_img);
+
+ static void convert_to_uint8(const MIL_ID& input_img, MIL_ID& output_img);
+};
+
+#endif // IMG_UTILS_H
diff --git a/ui_widget.h b/ui_widget.h
index 8307ab4..bc0d231 100644
--- a/ui_widget.h
+++ b/ui_widget.h
@@ -135,7 +135,9 @@ public:
camera_0_img = new QLabel(tab_2);
camera_0_img->setObjectName("camera_0_img");
camera_0_img->setGeometry(QRect(160, 240, 591, 191));
- camera_0_img->setStyleSheet(QString::fromUtf8("background-color: rgb(129, 129, 129);"));
+ camera_0_img->setStyleSheet(QString::fromUtf8("background-color: rgb(129, 129, 129);\n"
+"border: 4px solid black;"));
+ camera_0_img->setLineWidth(1);
tabWidget->addTab(tab_2, QString());
tab_3 = new QWidget();
tab_3->setObjectName("tab_3");
@@ -206,7 +208,7 @@ public:
retranslateUi(Widget);
- tabWidget->setCurrentIndex(0);
+ tabWidget->setCurrentIndex(1);
QMetaObject::connectSlotsByName(Widget);
diff --git a/widget.cpp b/widget.cpp
index c17ec54..9bb6fa8 100644
--- a/widget.cpp
+++ b/widget.cpp
@@ -20,7 +20,7 @@ using namespace std;
// 硬编码参数值
int file_delay = 1270; // 延迟时间(毫秒)
int file_encoder = 12000; // 编码器值++
-int file_valve = 120; // 阀门通道
+int file_valve = 200; // 阀门通道
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
diff --git a/widget.ui b/widget.ui
index d238f26..5a77719 100644
--- a/widget.ui
+++ b/widget.ui
@@ -23,7 +23,7 @@
- 0
+ 1
@@ -326,7 +326,11 @@
- background-color: rgb(129, 129, 129);
+ background-color: rgb(129, 129, 129);
+border: 4px solid black;
+
+
+ 1