有人可以帮助我显示来自相机的视频吗?摄像机的值存储在QVector中。有可能以这种方式显示它吗?
答案 0 :(得分:0)
RGB源图像
除非QVector
是内存映射文件,否则无法进行直接转换。如果它包含原始字节(如图所示),我建议从原始数据构建一个QImage
作为中间对象,然后将其转换为QPixmap
:
// assuming 'v' is the vector, and 'w' and 'h' the size of the image
const QImage tmpImage((const char*)v.data(), w, h, QImage::Format_RGB16);
QPixmap pixmap(QPixmap::convertFromQImage(tmpImage));
鉴于原始元素大小为16位,您可能需要QImage::Format_RGB16
(对于5-6-5)或QImage::Format_RGB555
(对于5-5-5)。查看可用的formats以获取更多选项。当然,假设是RGB图像。
灰度图像
如果您的图像只有一个颜色通道,那么您必须进行手动转换,因为Qt无法处理16位灰度图像。
简单的代码,但可能无法产生您想要的结果:
const QImage tmpImage((const char*)v.data(), w, h, QImage::Format_RGB16);
QPixmap pixmap(QPixmap::convertFromQImage(
image.convertToFormat(QImage::Format::Format_Grayscale8)));
否则,您必须减少图像的深度。检查this answer以获取更详细的实施。然而,这是一个缓慢的操作。如果您的Qt版本允许,请使用QImage::Format_Grayscale8
作为目标图片,而不是链接答案中提出的QImage::Format_RGB32
,您可以减少3倍的元素。
// 'image' is a QImage(w, h, QImage::Format_Grayscale)
// if possible, re-use it on each frame to reduce construction overhead
// 'ptr' is the pointer to your 16-bits data (const short* ptr)
auto ptr = v.constData();
#pragma omp parallel for // if can use OpenMP you can gain some speed here
for (int ii = 0; ii < image.height(); ++ii) {
auto scanLine = image.scanLine(ii);
for (int jj = 0; jj < image.width(); ++jj, ++ptr, ++scanLine) {
*scanLine = (unsigned char)(*ptr >> 8); // high byte, most significative bits
}
}
答案 1 :(得分:0)
以下是代码:
void MainWindow::slotDataUpdate(QVector<uint16_t> vec)
{
ui->setupUi(this);
double min = *std::min_element(vec.begin(), vec.end());
double max = *std::max_element(vec.begin(), vec.end());
std::transform(vec.begin(), vec.end(), vec.begin(), std::bind2nd(std::plus<double>(), (-1)*min));
int co = 65536/(max - min);
std::transform(vec.begin(), vec.end(), vec.begin(), std::bind2nd(std::multiplies<double>(), co));
const QImage tmpImage((const uchar*)vec.data(), 160, 120, QImage::Format_RGB16);
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << vec; //conversion from Q vector to QByteArray
// QByteArray data = QByteArray::fromRawData(reinterpret_cast<const char*>(vec.constData()),sizeof(double)* vec.size()); //conversion from Q vector to QByteArray
for (int i = 0; i < data.size(); i++)
{
std::cout << data[i] << " " << std::endl;
}
QImage tmpImage(160,120, QImage::Format_RGB32); //prazna slika pravih dimenzij
QRgb *pixels=reinterpret_cast<QRgb*>(tmpImage.bits());
for (size_t i=0;2*i<data.size();++i)
{
uchar pixel_msb=data[2*i+];
pixels[i]=qRgb(pixel_msb, pixel_msb, pixel_msb);
}
QPixmap pixmap(QPixmap::fromImage(tmpImage));
ui->label_img->setPixmap(pixmap.scaled(480,360,Qt::KeepAspectRatio));
}