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 ...
随机推荐
- 2018-12-25-win10-uwp-显示SVG
title author date CreateTime categories win10 uwp 显示SVG lindexi 2018-12-25 10:37:5 +0800 2018-2-13 1 ...
- Nginx的反向调度功能
1.案例实现Nginx反向代理; 2.反向代理的理论知识拓展. 一, 实验Nginx的反向代理功能 使用Nginx实现Web反向代理功能,实现如下功能: 后端Web服务器两台,可以使用httpd实现 ...
- CA认证机制的简明解释
公钥机制面临的问题: 假冒身份发布公钥! 可以用CA来认证公钥的身份.CA有点像公安局,公钥就像身份证.公安局可以向任何合法用户颁发身份证以证明其合法身份.第三方只要识别身份证的真伪就能判断身份证持有 ...
- django:一个RESTfull的接口从wsgi到函数的历程
1.wsgi将web server参数python化,封装为request对象传递给apllication命名的func对象并接受其传出的response参数,这个application在wsgi.p ...
- 03scikit-learn非监督学习
In [1]: from sklearn.decomposition import PCA from sklearn.datasets import load_iris pca = PCA(n_com ...
- ZROI2019 提高十连测
额 掰手指头一数 特么又是第三年十连测了= = 2017一场没打 那时候好像一场比赛也就100人左右 2018前几场还都好好补了 后来开始放飞自我了 这时候一场有150人还多了 2019想让今年的No ...
- CF1090J Two Prefixes
神仙题++ 还是在某校梁大讲的题qaq 我们考虑容斥 也就是本质不同字串=全部-重复的 我们只需要求重复的即可 考虑相同的s=ab 我们用长度最长的a作为代表串 如果存在一个a'b'且|a'|> ...
- Python---基础-小游戏用户猜数字2
一.使用int()将小数转换成整数,结果是向上取数还是向下取数 int(3,4) print(int(3,4)) ####写一个程序,判断给定年份是否为闰年 - 闰年的定义,能够被4整除的年份就叫闰年 ...
- spfa求次短路
思路:先算出每个点到1的最短路d1[i],记录下路径,然后枚举最短路上的边 删掉之后再求一遍最短路,那么这时的最短路就可能是答案. 但是这个做法是错误的,可以被卡掉. 比如根据下面的例题生成的一个数据 ...
- TabController定义顶部tab切换
前面通过DefaultTabController组件实现了AppBar里面的顶部导航切换,但是在项目中有数据请求,上拉加载更多等操作的时候,前面的写法,就不是很方便操作,因此,在flutter里面,还 ...