Read OpenCV Mat 16bit to QImage 8bit Greyscale
Read OpenCV Mat 16bit to QImage 8bit Greyscale
我想做什么
- 文件: TIFF, 2 页, 灰度, 16 位
- OpenCV: 2x 垫子, 灰度, 16 位 (CV_16UC1(
- Qt: 2x QImage, 灰度, 8 位 (Format_Greyscale8(
我想读取一个 16 位 Tiff,然后将其转换为 8 位。
读数
void ImgProc::Load_Image_2xTiff_16bit_G(Mat *Mat_A_in, Mat *Mat_B_in, string file_name)
{
vector<Mat> vec_Mat;
vec_Mat.reserve(2);
imreadmulti(file_name, vec_Mat);
*Mat_A_in = vec_Mat[0];
*Mat_B_in = vec_Mat[1];
}
转换
void ImgProc::Convert_Mat16_2_QIm8(QImage *QIm_Out, Mat *Mat_In, double scale_factor)
{
unsigned int rows = Mat_In->rows;
unsigned int cols = Mat_In->cols;
*QIm_Out = QImage(cols, rows, QImage::Format_Grayscale8);
unsigned char* line_QIm;
if (!Mat_In->data)
return;
for(unsigned int y = 0; y < rows; y++)
{
line_QIm = QIm_Out->scanLine(y);
for(unsigned int x = 0; x < cols; x++)
{
line_QIm[x] = (unsigned char)(Mat_In->at<ushort>(y, x) * scale_factor);
}
}
}
问题
当我使用Mat_In->at<ushort>(y, x)
(读取 16 位(时,它会崩溃并显示abort() has been called
.如果我改用<short>
,也会发生同样的情况。
当我使用Mat_In->at<uchar>(y, x)
(读取 8 位(时,它可以工作,但会切断信息而不缩放。这在图像的较亮区域显示为"黑洞",可能是溢出效应。
我想我应该提到,拍摄图像的相机只使用14位深度。
我遇到了同样类型的问题。 以下代码是我用来修复这种情况的解决方案。
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <QImage>
int main(void)
{
//Read the 16 bit per pixel image.
cv::Mat I = cv::imread('16BitsPerPixelImage.tiff',cv::IMREAD_ANYDEPTH|cv::IMREAD_ANYCOLOR);
//Convert from 16 bit per pixel to 8 bit per pixel using a min max normalization and store it a 8 bit per pixel.
cv::normalize(I,I,0.,255.,cv::NORM_MINMAX,CV_8U);
// Then actualy the easiest way to convert it to a QImage is to save a temporary file and open it using QT functions.
// PNG use a compression without loss algorithm.
cv::imwrite("/tmp/convert16to8.png",I);
QImage QI;
QI.load("/tmp/convert16to8.png");
return EXIT_SUCCESS;
}
相关文章:
- 以线程安全的方式调用"QQuickPaintedItem::updateImage(const QImage&image)"(no QThread)
- 将 cv::mat 转换为 QImage
- 将 QImage 从 c++ 客户端发送到 python 服务器
- Read OpenCV Mat 16bit to QImage 8bit Greyscale
- QImage::p ixel 和 QImage::setPixel 坐标超出范围误差
- 如何从路径加载 QImage?
- 在 QListWidget 中旋转 QImage
- 如何为QImage检测足够的格式(数据类型)以消耗最少的RAM
- 优化从文件加载QImage
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- QImage:加载图像时自动检测格式
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 我可以在将项目编译为静态库(未定义的引用)时在代码中使用 QImage 吗?
- 将 QImage 保存到字节数组
- QImage@@QEAA@$$QEAV 0@@Z 无法在 DLL 中找到
- Qimage setPixel with openmp 并行 for 不起作用
- 如何将QImage从C++发送到QML到Qt3D实体以将其用作纹理?
- 警告在将 OpenCV 垫映像投射到 QImage 时使用旧式转换
- 无法在QImage上绘制或更改像素的颜色
- Qimage :: Pixel函数的10位输出特别是指的是什么