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 ...
随机推荐
- Express 2015 RC for Windows 10 安装
支持的操作系统 Windows 10 Technical Preview 硬件要求 1.6 GHz 或更快的处理器 1 GB RAM(如果在虚拟机上运行,则为 1.5 GB) 4 GB 可用硬盘空间 ...
- openstack stein部署手册 9. neutron
# 安装程序包 yum -y install openstack-neutron-linuxbridge ebtables ipset # 变更配置文件 mv /etc/neutron/neutron ...
- 字符串format函数使用
#format拼接字符串,format()内的参数必须为可迭代的对象p1="i am {2},age {1},{0}".format("seven",18,'a ...
- Sass @warn
@warn 和 @debug 功能类似,用来帮助我们更好的调试 Sass.如: @mixin adjust-location($x, $y) { @if unitless($x) { @warn &q ...
- 前端每日实战:93# 视频演示如何用纯 CSS 创作一根闪电连接线
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/RBjdzZ 可交互视频 此视频是可 ...
- ArrayList,Vector, LinkedList的存储性能和特性?
ArrayList,Vector, LinkedList的存储性能和特性? ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入或删除时非常麻烦. ...
- tensorflow图像处理函数(1)
1.tensorflow中对jpeg格式图像的编码/解码函数: import matplotlib.pyplot as plt import tensorflow as tf image_raw_da ...
- Change the environment variable for python code running
python程序运行中改变环境变量: Trying to change the way the loader works for a running Python is very tricky; pr ...
- Java中的栈和队列
栈: public class Stack<E> extends Vector<E> { // 使用数组实现栈 // 构造一个空栈 public Stack() { } // ...
- 如何查看本机上安装的.NET Framework版本
在开始菜单选择"运行", 或者快捷键 “windows键+R” 在命令窗口输入regedit.exe,打开注册表 在注册表中定位到如下节点 HKEY_LOCAL_MACHINE\S ...