maskelement.cpp
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#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"