118 lines
4.3 KiB
C++
118 lines
4.3 KiB
C++
#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;
|
||
}
|
||
}
|