QImage 如何和 Tensor 相互转换?
torch::Tensor fromQImage(QImage image)
{
int width = image.width();
int height = image.height();
int depth = image.depth();
int channels = depth / 8;
const torch::TensorOptions option(torch::kUInt8);
torch::Tensor tensor = torch::from_blob(image.bits(),{width * height,channels},option);//R G B A
auto result = torch::zeros({1,channels,height*width});//N C H C
if(channels == 4){
/*!
R G B A
R G B A
R G B A
=>
R R R
G G G
B B B
A A A
*/
tensor = tensor.transpose(0,1);
auto R = tensor[0];
auto G = tensor[1];
auto B = tensor[2];
auto A = tensor[3];
result[0][0] = B;
result[0][1] = G;
result[0][2] = R;
result[0][3] = A;
result = result.view({1,channels,height,width}).div(255.0);//N C H C
//std::cout << result << std::endl;
return result;
}
if(channels == 3){
/*!
R G B
R G B
R G B
=>
R R R
G G G
B B B
*/
tensor = tensor.transpose(0,1);
auto R = tensor[0];
auto G = tensor[1];
auto B = tensor[2];
result[0][0] = B;
result[0][1] = G;
result[0][2] = R;
result = result.view({1,channels,height,width}).div(255.0);//N C H C
//std::cout << result << std::endl;
return result;
}
if(channels == 1){
return result;
}
return result;
}
QImage TensorToQImage(const torch::Tensor &tensor)
{
QImage image;
int dim = tensor.dim();
if(dim != 4){
qFatal("dim must be 4.");
}
//std::cout << tensor.size(0) << tensor.size(1) << tensor.size(2) << tensor.size(3) << std::endl;
int channels = tensor.size(1);
int width = tensor.size(3);
int height = tensor.size(2);
// fill QImage
if(channels == 1){
#pragma omp simd
image = QImage(width,height,QImage::Format_Grayscale8);
for(int w = 0;w < width;++w){
for(int h = 0;h < height;++h){
QRgb gray = tensor[0][0][h][w].item<float>() * 255.0;
image.setPixel(w,h,gray);
}
}
}
// fill QImage
if(channels == 3){
image = QImage(width,height,QImage::Format_RGB888);
#pragma omp simd
for(int w = 0;w < width;++w){
for(int h = 0;h < height;++h){
int r = tensor[0][0][h][w].item<float>() * 255.0;
int g = tensor[0][1][h][w].item<float>() * 255.0;
int b = tensor[0][2][h][w].item<float>() * 255.0;
QRgb rgb = qRgb(r,g,b);
image.setPixel(w,h,rgb);
}
}
}
// fill QImage
if(channels == 4){
image = QImage(width,height,QImage::Format_RGB32);
#pragma omp simd
for(int w = 0;w < width;++w){
for(int h = 0;h < height;++h){
int r = tensor[0][0][h][w].item<float>() * 255.0;
int g = tensor[0][1][h][w].item<float>() * 255.0;
int b = tensor[0][2][h][w].item<float>() * 255.0;
int a = tensor[0][3][h][w].item<float>() * 255.0;
QRgb rgb = qRgba(r,g,b,a);
image.setPixel(w,h,rgb);
}
}
}
return QImage();
}
/*!
QImage to torch::Tensor N x C x H x W
*/
torch::Tensor QImageToTensor(const QImage &image)
{
int width = image.width();
int height = image.height();
int depth = image.depth();
int channels = depth / 8;
// create tensor
torch::TensorOptions option(torch::kFloat32);
torch::Tensor tensor = torch::zeros({1,channels,height,width},option);//N C H W
bool isOk = false;
// fill tensor
if(channels == 1){
#pragma omp simd
for(int w = 0;w < width;++w){
for(int h = 0;h < height;++h){
QRgb rgb = image.pixel(w,h);
tensor[0][0][h][w] = qGray(rgb)/255.0;//GRAY
}
}
isOk = true;
}
// fill tensor
if(channels == 3){
#pragma omp simd
for(int w = 0;w < width;++w){
for(int h = 0;h < height;++h){
QRgb rgb = image.pixel(w,h);
tensor[0][0][h][w] = qRed(rgb)/255.0;//R
tensor[0][1][h][w] = qGreen(rgb)/255.0;//G
tensor[0][2][h][w] = qBlue(rgb)/255.0;//B
}
}
isOk = true;
}
// fill tensor
if(channels == 4){
#pragma omp simd
for(int w = 0;w < width;++w){
for(int h = 0;h < height;++h){
QRgb rgb = image.pixel(w,h);
tensor[0][0][h][w] = qRed(rgb)/255.0;//R
tensor[0][1][h][w] = qGreen(rgb)/255.0;//G
tensor[0][2][h][w] = qBlue(rgb)/255.0;//B
tensor[0][3][h][w] = qAlpha(rgb)/255.0;//A
}
}
isOk = true;
}
if(!isOk){
qFatal("channels must be 1, 3, or 4.");
}
//std::cout << tensor << std::endl;
return tensor;
}
QImage 如何和 Tensor 相互转换?的更多相关文章
- QT 二维图形 原理、发展及应用
转载自 网易博客:sun的博客 http://zhouyang340.blog.163.com/blog/static/3024095920126710504178/ 2D绘图 Qt4中的2D绘图部分 ...
- torch 中各种图像格式转换
PIL:使用python自带图像处理库读取出来的图片格式 numpy:使用python-opencv库读取出来的图片格式 tensor:pytorch中训练时所采取的向量格式(当然也可以说图片) PI ...
- qt 2D绘图技巧
2D绘图 Qt4中的2D绘图部分称为Arthur绘图系统.它由3个类支撑整个框架,QPainter,QPainterDevice和QPainterEngine.QPainter用来执行具体的绘图相关操 ...
- IplImage 与 QImage 相互转换
在使用Qt和OpenCV编程时,对于它们各自的图像类QImage和IplImage难以避免的需要互相之间的转换,下面我们就来看它们的相互转换. 1. QImage 转换为 IplImage IplIm ...
- Qt OpenCV::Mat与Qt::QImage相互转换
Mat转QImage QImage mat2qim(Mat & mat) { cvtColor(mat, mat, COLOR_BGR2RGB); QImage qim((const unsi ...
- Tensor神经网络进行知识库推理
本文是我关于论文<Reasoning With Neural Tensor Networks for Knowledge Base Completion>的学习笔记. 一.算法简介 网络的 ...
- QImage 与 cv::Mat 之间的相互转换
近期做图像处理方面的项目比較多,非常多算法自己从头写的话太浪费时间,并且自己写的也不一定完好,早就听说OpenCV在图像处理算法方面功能非常强大,一直没时间学习,这次正好项目用到了.暂时抱佛脚学习些O ...
- [开发技巧]·AdaptivePooling与Max/AvgPooling相互转换
[开发技巧]·AdaptivePooling与Max/AvgPooling相互转换 个人网站--> http://www.yansongsong.cn/ 1.问题描述 自适应池化Adaptive ...
- (十一)QPainter绘图, QPixmap,QImage,QPicture,QBitmap
#include "widget.h" #include "ui_widget.h" #include <QPainter> #include &l ...
随机推荐
- nodejs 报错
vue不是内部或外部命令的解决方法 1.在nodejs的安装目录下,找到vue.cmd,将此路径加到环境变量中,我是通过nvm管理node版本的,路径是C:\Users\hy\AppData\Roam ...
- SQL 1 数据库 表的操作
数据库:是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库.一句话就是存储数据的仓库 数据库的分类:网络数据库.层级数据库.关系结构数据库. 倘若按照数据库的存储介质来分:关系型数据库 ...
- Kconfig的简单例子
https://cloud.tencent.com/developer/article/1431908 使用Kconfig时,需要注意的地方 1.在Kconfig中定义的配置宏,前缀都没有" ...
- 【串线篇】spring boot自动配置精髓
一.SpringBoot启动会加载大量的自动配置类即绿色部分 二.我们看我们需要的功能有没有SpringBoot默认写好的自动配置类: 比如HttpEncodingAutoConfiguration ...
- 记录ViewPager配合Fragment使用中遇到的一个问题
java.lang.IllegalStateException: FragmentManager is already executing transactions 如图所示: 当调用 notifyD ...
- XML 语法
XML 语法规则 本节的目的是想让你了解 XML 中的语法所依据的规则,避免在编写 XML 文档的时候遇到错误. XML 的语法规则很简单,且很有逻辑.这些规则很容易学习,也很容易使用. 所有的 XM ...
- ECS主动运维事件--让你HOLD住全场 (二)
背景 数月前,我们推出了新的功能:ECS主动运维事件--让你HOLD住全场 https://yq.aliyun.com/articles/573782?spm=a2c4e.11155435.0.0.7 ...
- OC + RAC (二) Command 命令的用法
-(void)_test2{ ///////////////////////////////////////Command 命令的用法 注意使用command.executionSignals去订阅时 ...
- C# Label换行解决方法
一.label太短,无法完成显示所要显示信息长度,要换行,解决方法如下: (1) string aa =(长串) ; string cc= aa.Substring(0,10);//取前10个字符 s ...
- python中用os.walk查找全部的子文件
import os import shutil # 要遍历查找的文件所在的父文件夹 trajectory_filename =r"D:\mapping" # 要粘贴到的目标文件夹 ...