- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在我的 QT 应用程序中将视频帧提取到图像中。我事先不知道源视频/帧(yuv、rgb ...)的像素格式,但我需要获得可靠的图像像素格式,以便以后可以始终如一地处理图像。
我正在使用 ffmpeg 库来获取已经解码的帧。我试图避免使用不推荐使用的函数,并且我需要优化速度。
我尝试实现这个例子:https://stackoverflow.com/a/42615610/7360943哪个
// we will convert the original color format to rgb24
SwsContext* img_convert_ctx = sws_getContext(
pFrame->width,
pFrame->height,
(AVPixelFormat)pFrame->format,
pFrame->width,
pFrame->height,
AV_PIX_FMT_RGB24,
SWS_BICUBIC, NULL, NULL, NULL);
QImage image(pFrame->width,
pFrame->height,
QImage::Format_RGB888);
int rgb_linesizes[8] = {0};
rgb_linesizes[0] = 3*pFrame->width;
sws_scale(img_convert_ctx,
pFrame->data,
pFrame->linesize, 0,
pFrame->height,
(uint8_t *[]){image.bits()},
rgb_linesizes);
ffmpeg::sws_freeContext(img_convert_ctx);
问题是
对于一些特定的视频 ,它会输出一些看起来有点黑白的奇怪图像(并且可能似乎显示输入宽度和输出宽度之间的偏移量为 1 ......?我无法完全解释可能导致这种情况的原因):
最佳答案
最后我想通了,使用手动分配的缓冲区,这不是很干净的 C++ 代码,但运行速度更快,而且没有过时的调用。无法将 image.bits 直接传递给 sws_scale,因为 QImages 至少 32 位对齐( https://doc.qt.io/qt-5/qimage.html#scanLine ),这意味着根据图像宽度,每行末尾的内存中有“空白空间”,sws_scale 确实如此不跳过/考虑。太糟糕了,因为我们现在有两个内存复制操作,在 sws_scale 和 memcpy,而不是一个,但我还没有找到更好的方法。
我仍然有缓冲区分配大小的问题,需要至少 64 个额外字节,我无法理解,但否则我们有时会遇到段错误。这可能是由于 memcpy 的工作方式,复制整个 32 或 64 字节 block ......但无论如何,这是新的实现:
(注意:我在专用命名空间下导入 ffmpeg 函数,在每次调用之前解释 ffmpeg::)
QImage getQImageFromFrame(const ffmpeg::AVFrame* pFrame) const
{
// first convert the input AVFrame to the desired format
ffmpeg::SwsContext* img_convert_ctx = ffmpeg::sws_getContext(
pFrame->width,
pFrame->height,
(ffmpeg::AVPixelFormat)pFrame->format,
pFrame->width,
pFrame->height,
ffmpeg::AV_PIX_FMT_RGB24,
SWS_BICUBIC, NULL, NULL, NULL);
if(!img_convert_ctx){
qDebug() << "Failed to create sws context";
return QImage();
}
// prepare line sizes structure as sws_scale expects
int rgb_linesizes[8] = {0};
rgb_linesizes[0] = 3*pFrame->width;
// prepare char buffer in array, as sws_scale expects
unsigned char* rgbData[8];
int imgBytesSyze = 3*pFrame->height*pFrame->width;
// as explained above, we need to alloc extra 64 bytes
rgbData[0] = (unsigned char *)malloc(imgBytesSyze+64);
if(!rgbData[0]){
qDebug() << "Error allocating buffer for frame conversion";
free(rgbData[0]);
ffmpeg::sws_freeContext(img_convert_ctx);
return QImage();
}
if(ffmpeg::sws_scale(img_convert_ctx,
pFrame->data,
pFrame->linesize, 0,
pFrame->height,
rgbData,
rgb_linesizes)
!= pFrame->height){
qDebug() << "Error changing frame color range";
free(rgbData[0]);
ffmpeg::sws_freeContext(img_convert_ctx);
return QImage();
}
// then create QImage and copy converted frame data into it
QImage image(pFrame->width,
pFrame->height,
QImage::Format_RGB888);
for(int y=0; y<pFrame->height; y++){
memcpy(image.scanLine(y), rgbData[0]+y*3*pFrame->width, 3*pFrame->width);
}
free(rgbData[0]);
ffmpeg::sws_freeContext(img_convert_ctx);
return image;
}
关于c++ - 通过像素格式转换将 AVFrame 转换为 QImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68048292/
我正在寻找一种方法来简单地将一些Qimage粘贴到更大的图像中,从一些给定的(x,y)开始。现在,我正在逐像素复制所有Qimage。 最佳答案 QImage srcImage = QImage(100
我正在开发一个图像处理程序,当程序不断收到这些消息时,这里有一些示例,但我收到了数百条消息,以至于程序无法完全执行。 QImage::setPixel: coordinate (1043968,0)
我尝试将具有 RGB32 的 QImage 转换为具有 RGB24 的 QImage。我的代码在这种情况下不起作用: 当图片在BMP头中多出1024字节时,图片的大小为X*Y*3+54+未知的1024
我找到了类似的主题,但不是这个特定的主题。知道以下代码有什么问题吗?它返回'loaded=false',这显然意味着无法加载图像数据。 cv::Mat mymat; QImage qimg; myma
我创建了一个 Publisher 类,它定期发出 QImage 对象。 但是,我很难将 QImage 绘制到 QML 元素。 Image 和 Canvas QML 组件似乎需要 QUrl 而不是 QI
我是图形编程的新手(像素、图像等) 我正在尝试将原始数据转换为 QImage 并将其显示在 QLabel 上。问题是,原始数据可以是任何数据(它实际上不是图像原始数据,而是二进制文件。) 原因是,为了
是 QImage基于 QSharedData ?做 Qimage关注 pimpl或 copy on write ? 例如复制(通过复制骗局或分配)Qimage 会复制像素的深拷贝吗? 最佳答案 QIm
我正在调试一个巨大的 Qt 应用程序,其中包含数百个类、凌乱的代码等。在跟踪错误的同时,我想单步执行 gdb(kdbg、ddd 等等),同时观察使用 QPainter 绘制到 QImage 中的内容这
如何清除或清理 QIMage 我的以下方法获取对 QIMage 的常量引用。 MyMethod(const QImage & img) { // save it to a file img.s
我正在尝试将Format_Grayscale8图像转换为Format_ARGB32(或任何其他8bpp RGB格式)。我尝试使用QImage::convertToFormat和颜色表,但无法正常工作。
我正在使用 Qt 库创建 QImages。 我可以使用 this constructor : QImage image("example.jpg"); 但是我遇到了 this static funct
我知道 QImage 的高度和宽度,以及每个像素值(从 x、y 位置)。我如何构建一个知道这些值的 QImage? 最佳答案 setPixel() 的第二个参数是单个 int 中的 24 位 RGB
我正在开发用于编辑光栅图形的应用程序。在这个应用程序中,我必须创建扫描线函数,它与 QImage 类中的扫描线函数做同样的事情。但是我对扫描线函数的工作方式和一般的扫描线有点困惑。例如,当我为高度为
我的工作基于 this answer ,我正在尝试使用 Qt 的 QGLWidget 来呈现视频,但我遇到了一些问题。在我的视频解码线程中解码后立即保存帧时,结果很好: 但是当画出来的时候,它被严重破
我目前使用的是 Qt 5.12.3,在理解 QImage::pixel 函数的 10 位数字输出时遇到问题。对于此代码: QImage image; image.load("D:/qtProjects
我有一个简单的绘图应用程序,但是当我调整窗口大小时图像被删除,我想保留它。我尝试使用 image.scaled(current size) 但它不起作用。 qDebug() size(); QImag
我正在尝试显示 QImage在 QML 中,可能带有 Image .目前,我发现了类似 QQuickImageProvider 的内容,我实现并尝试使用,但没有成功。 我有QList我自己的对象( I
QImage具有以下构造函数: QImage(const QString &fileName, const char *format = nullptr); 但是,我正在尝试解析已作为字节数组加载的
我在我的 qt quick 应用程序中用相机捕获了一个图像。我想将路径发送到我的 c++ 代码并在 c++ QImage 中加载该图像。但是路径是 image://camera/preview_1 我
在我控制窗口的主类中,我有这个函数,其中 pixmapItem 是类头中定义的 QGraphicsPixmapItem*: void updateDisplay() { uchar *data
我是一名优秀的程序员,十分优秀!