ui完整版

This commit is contained in:
zhenye 2024-12-18 16:41:24 +08:00
parent 5a49ca21eb
commit 643a663953
7 changed files with 177 additions and 5 deletions

8
globals.cpp Normal file
View File

@ -0,0 +1,8 @@
// globals.cpp
#include "globals.h"
// 初始化全局互斥锁
QMutex gPicMutex0;
// 初始化全局 MIL_ID
MIL_ID gCurrentPicId0 = 0;

14
globals.h Normal file
View File

@ -0,0 +1,14 @@
// globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
#include <MIL.h>
#include <QMutex>
// 全局互斥锁,保护 current_pic_id 的访问
extern QMutex gPicMutex0;
// 全局变量,用于存储当前的 MIL_ID
extern MIL_ID gCurrentPicId0;
#endif // GLOBALS_H

117
img_utils.cpp Normal file
View File

@ -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<int>(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<int>(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<int>(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<Mat> 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;
}
}

27
img_utils.h Normal file
View File

@ -0,0 +1,27 @@
// utils.h
#ifndef IMG_UTILS_H
#define IMG_UTILS_H
#include <QPixmap>
#include <QImage>
#include <opencv2/opencv.hpp>
#include <Mil.h>
#include <camera.h>
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

View File

@ -135,7 +135,9 @@ public:
camera_0_img = new QLabel(tab_2); camera_0_img = new QLabel(tab_2);
camera_0_img->setObjectName("camera_0_img"); camera_0_img->setObjectName("camera_0_img");
camera_0_img->setGeometry(QRect(160, 240, 591, 191)); 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()); tabWidget->addTab(tab_2, QString());
tab_3 = new QWidget(); tab_3 = new QWidget();
tab_3->setObjectName("tab_3"); tab_3->setObjectName("tab_3");
@ -206,7 +208,7 @@ public:
retranslateUi(Widget); retranslateUi(Widget);
tabWidget->setCurrentIndex(0); tabWidget->setCurrentIndex(1);
QMetaObject::connectSlotsByName(Widget); QMetaObject::connectSlotsByName(Widget);

View File

@ -20,7 +20,7 @@ using namespace std;
// 硬编码参数值 // 硬编码参数值
int file_delay = 1270; // 延迟时间(毫秒) int file_delay = 1270; // 延迟时间(毫秒)
int file_encoder = 12000; // 编码器值++ int file_encoder = 12000; // 编码器值++
int file_valve = 120; // 阀门通道 int file_valve = 200; // 阀门通道
Widget::Widget(QWidget *parent) Widget::Widget(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, ui(new Ui::Widget) , ui(new Ui::Widget)

View File

@ -23,7 +23,7 @@
</rect> </rect>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>1</number>
</property> </property>
<widget class="QWidget" name="tab"> <widget class="QWidget" name="tab">
<attribute name="title"> <attribute name="title">
@ -326,7 +326,11 @@
</rect> </rect>
</property> </property>
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">background-color: rgb(129, 129, 129);</string> <string notr="true">background-color: rgb(129, 129, 129);
border: 4px solid black;</string>
</property>
<property name="lineWidth">
<number>1</number>
</property> </property>
<property name="text"> <property name="text">
<string/> <string/>