maskelement.cpp 3.3 KB
#include <qrgb.h>
#include <akfrac.h>
#include <akpacket.h>
#include <akvideocaps.h>
#include <akvideoconverter.h>
#include <akvideopacket.h>

#include "maskelement.h"
#include "yolov8_seg.h"

class MaskElementPrivate
{
    public:
        AkVideoConverter m_videoConverter {{AkVideoCaps::Format_argbpack, 0, 0, {}}};
};

MaskElement::MaskElement(): AkElement()
{
    this->d = new MaskElementPrivate;
}

MaskElement::~MaskElement()
{
    delete this->d;
}

AkPacket MaskElement::iVideoStream(const AkVideoPacket &packet)
{
    this->d->m_videoConverter.begin();
    auto src = this->d->m_videoConverter.convert(packet);
    this->d->m_videoConverter.end();

    if (!src)
        return {};

    // 创建OpenCV Mat并逐个像素填充
    cv::Mat frame(src.caps().height(), src.caps().width(), CV_8UC4);
    for (int y = 0; y < src.caps().height(); y++) {
        auto iLine = reinterpret_cast<const QRgb *>(src.constLine(0, y));
        auto oLine = frame.ptr<cv::Vec4b>(y);

        for (int x = 0; x < src.caps().width(); x++) {
            auto &pixel = iLine[x];
            oLine[x] = cv::Vec4b(qBlue(pixel),  // OpenCV使用BGR顺序
                                qGreen(pixel),
                                qRed(pixel),
                                qAlpha(pixel));
        }
    }
    // 检测人体分割
    std::vector<Object> objects;
    // 将4通道ARGB转换为3通道BGR
    cv::Mat bgrFrame;
    cv::cvtColor(frame, bgrFrame, cv::COLOR_BGRA2BGR);
    detect_yolov8_seg(bgrFrame, objects);

    // 创建输出帧
    AkVideoPacket dst(src.caps());
    dst.copyMetadata(src);



    // 绘制人体分割遮盖
    bool hasMask = false;
    for (const auto& obj : objects) {
        if (obj.label != 0) // 0是person类别
            continue;
        hasMask = true;
        for (int y = 0; y < (int)obj.rect.height; y++) {
            const uchar* maskptr = obj.mask.ptr<const uchar>(y);
            cv::Vec4b* dstptr = frame.ptr<cv::Vec4b>((int)obj.rect.y + y) + (int)obj.rect.x;
            
            for (int x = 0; x < (int)obj.rect.width; x++) {
                if (!maskptr[x]) {
                    // 将非人体区域设为半透明红色
                    dstptr[x] = cv::Vec4b(0, 0, 255, 128);
                }
            }
        }
        // 将obj.rect以外的区域变红
        for (int y = 0; y < frame.rows; y++) {
            cv::Vec4b* frameLine = frame.ptr<cv::Vec4b>(y);
            for (int x = 0; x < frame.cols; x++) {
                if (x < obj.rect.x || x >= obj.rect.x + obj.rect.width - 2 ||
                    y < obj.rect.y || y >= obj.rect.y + obj.rect.height - 2) {
                    frameLine[x] = cv::Vec4b(0, 0, 255, 128);
                }
            }
        }

    }




    for (int y = 0; y < src.caps().height(); y++) {
        auto oLine = reinterpret_cast<QRgb *>(dst.line(0, y));
        auto matLine = frame.ptr<cv::Vec4b>(y);
    
        for (int x = 0; x < src.caps().width(); x++) {
            auto &pixel = matLine[x];
            oLine[x] = qRgba(pixel[2],  // OpenCV是BGR顺序,所以R在索引2
                    pixel[1],  // G
                    pixel[0],  // B
                    pixel[3]); // Alpha
            

            
        }
    }


    if (dst)
        emit this->oStream(dst);

    return dst;
}

#include "moc_maskelement.cpp"