在 MySQL 中存储 matlab 数组.再次 [英] Storing matlab array in MySQL. Again

查看:37
本文介绍了在 MySQL 中存储 matlab 数组.再次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 uint16 的 Matlab 中有一个 3D 数组(基本上它只是一个 1080x1920x3 的图像).我想将它存储在mysql中.这是我正在做的:

I have a 3D array in Matlab of uint16(basically it is just an image 1080x1920x3). I want to store it in mysql. Here is what I'm doing:

MySQL:

create table imgtest(img longblob);

MATLAB:

% image_data - is my image as described before
raw_im = reshape(image_data,1,[]);
conn = database('test','root','root','Vendor','MySQL','Server','localhost')
x = conn.Handle;
insertcommand = ['INSERT INTO imtest (img) values (?)'];
StatementObject = x.prepareStatement(insertcommand);
StatementObject.setObject(1,raw_im)
StatementObject.execute

问题是我在这个 blob 字段中写入了大约 600k uint16 值.但是当我从数据库中取出这个字段时,我总是得到大约 120 万个 uint8 元素(正好是两倍).

The problem is that I'm writing about 600k uint16 values into this blob field. But when I take this field from the DB, I always getting about 1.2 million of uint8 elements(exactly two times more).

那么,有没有办法将这个字节字段读取为一组uint16,而不是uint8?

So, is there a way to read this byte field as a set of uint16, but not uint8?

谢谢.

推荐答案

好的,谢谢你们.我已经总结了你的答案,这是我得到的:

OK, thank you both. I've summarized your answers and this what I've got:

由于blob字段只不过是字节数组,因此我们应该在将数据写入数据库之前将其转换为matlab.从 DB 读取后,我们应该将它们返回.

Since blob field is nothing more than byte array, then we should cast our data in matlab before writing it to the DB. After reading it from DB, we should cast them back.

最小工作示例是:MySQL

Minimum working example is: MySQL

create table imgtest(img longblob);

MATLAB

% image_data - is my image as described before
raw_im = typecast(reshape(image_data,1,[]),'uint8'); %! the main string
conn = database('test','root','root','Vendor','MySQL','Server','localhost')
x = conn.Handle;
insertcommand = ['INSERT INTO imtest (img) values (?)'];
StatementObject = x.prepareStatement(insertcommand);
StatementObject.setObject(1,raw_im)
StatementObject.execute

在我们可以读回来之后:

After we can read it back:

res = exec(conn,'Select * from imtest')
array_uint8 = fetch(res);
array_uint8 = array_uint8{1};
array_uint16 = typecast(array_uint8,'uint16').

希望这会对某人有所帮助.

Hope this will help someone.

这篇关于在 MySQL 中存储 matlab 数组.再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆