Newer
Older
CasicIrisIdentify / utils / ImageUtil.cpp
tanyue on 16 Dec 2023 3 KB 20231216 debug on ubuntu arm
#include "ImageUtil.h"

ImageUtil::ImageUtil()
{

}

QImage ImageUtil::MatImageToQImage(const cv::Mat &src)
{
    //CV_8UC1 8位无符号的单通道---灰度图片
    if(src.type() == CV_8UC1)
    {
        QImage qImage((const unsigned char *)(src.data), src.cols, src.rows, src.cols, QImage::Format_Grayscale8);
        return qImage;
    }
    //为3通道的彩色图片
    else if(src.type() == CV_8UC3)
    {
        //得到图像的的首地址
        const uchar *pSrc = (const uchar*)src.data;
        //以src构造图片
        QImage qImage(pSrc, src.cols, src.rows, src.step, QImage::Format_RGB888);
        //在不改变实际图像数据的条件下, 交换红蓝通道
        return qImage.rgbSwapped();
    }
    //四通道图片, 带Alpha通道的RGB彩色图像
    else if(src.type() == CV_8UC4)
    {
        const uchar *pSrc = (const uchar*)src.data;
        QImage qImage(pSrc, src.cols, src.rows, src.step, QImage::Format_ARGB32);
        //返回图像的子区域作为一个新图像
        return qImage.copy();
    }
    else
    {
        return QImage();
    }
}

cv::Mat ImageUtil::ucharToMat(unsigned char *data, int row, int col)
{
    cv::Mat img(row, col, CV_8UC1, (unsigned char *)data);
    return img;
}

cv::Mat ImageUtil::QImageToMat(QImage image)
{
    cv::Mat mat;
    switch(image.format())
    {
    case QImage::Format_ARGB32:
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32_Premultiplied:
        mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
        break;
    case QImage::Format_RGB888:
        mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
        cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB);
        break;
    case QImage::Format_Indexed8:
        mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
        break;
    case QImage::Format_Grayscale8:
        mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void *)image.bits(), image.bytesPerLine());
        break;

    default:
        break;
    }

    mat = mat.clone();

    return mat;
}

/*
QImage ImageUtil::UcharToQImage(unsigned char* data, int width, int height, QImage::Format format)
{
    QImage qImage(data, width, height, format);
    //在不改变实际图像数据的条件下, 交换红蓝通道
    return qImage.rgbSwapped();
}

QString ImageUtil::QImageToBase64(QImage image)
{
    QByteArray ba;
    QBuffer buf(&ba);
    image.save(&buf, "bmp");
    QString base64String = ba.toBase64();
    return base64String;
}

cv::Mat ImageUtil::MatImageRect(const cv::Mat &src, cv::Rect rect, int delta)
{
    cv::Mat matClone = src.clone();
    cv::Rect bigRect;

    bigRect.x = rect.x - delta < 0 ? 0 : rect.x - delta;
    bigRect.y = rect.y - delta < 0 ? 0 : rect.y - delta;
    bigRect.width = rect.x + rect.width + 2 * delta > src.cols ? src.cols - rect.x : rect.width + 2 * delta;
    bigRect.height = rect.y + rect.height + 2 * delta > src.rows ? src.rows - rect.y : rect.height + 2 * delta;

    cv::Mat roi = matClone(bigRect);

    return roi;
}
*/