How Import image from MS SQL?

In stock:
1. MS SQL Database
2. Table of images Images
3. Running BigBase ODBC connection ('Microsoft SQL Server' driver '13 .00.1601 ')
4. Code reading photos using MatLab:
clear all;
close all;
cd 'D:\NEWDoc\MATLAB';
% Make connection to database. Note that the password has been omitted.
% Using ODBC driver.
conn = database ('BigBase', 'user', 'pass');
% Read data from database.
st = 'select top 10 RecID, Image from images';
data = fetch (conn, st);
5. The results:
RecID Image
6143047 5999x1 uint8
224233 8000x1 uint8
4200382 6130x1 uint8
13687913 8000x1 uint8
2212046 8000x1 uint8
8346866 6400x1 uint8
1305479 6760x1 uint8
7806085 6641x1 uint8
6851834 7164x1 uint8
7084710 6550x1 uint8
As you can see, some fields, when imported, are cropped to 8000x1 uint8 it turns out that when importing, large images are lost.
Why? How to fix it?
Additional Information:
In the database, the field Image type - (blob)

5 个评论

Could provide more information such as versions of SQL Server and MATLAB? The image stored in SQL Server are actually 8000 bytes by checking with SSMS(SQL Server Management Studio)?
Version MatLab:
>> version
ans =
'9.5.0.944444 (R2018b)'
Version MS SQL : 2008 and last 2016
In the running application, photos from this database are displayed normally.
When writing imported data to a file, it turns out something like this:
25.12.png
Anticipating a further question - this is the code for how the images are saved:
for i = 1 : size(data)
filename = 'temp.jpg';
fileID = fopen(filename,'w');
fwrite(fileID,data.Image{i});
fclose(fileID);
im=imread(filename);
figure;
imshow(im);
end
The game with parametrs
opts = databaseImportOptions(conn,'Images');
varnames = {'RecID', 'Image'};
varOpts = getoptions(opts,varnames);
varnames = 'Image';
opts = setoptions(opts,varnames,'Type','unknown');
did not give any result
What is the data type of Image column? Are you using varbinary or varbinary(MAX)?
[Image] [varbinary](max) NOT NULL

请先登录,再进行评论。

回答(1 个)

Vladimir Malin
Vladimir Malin 2018-12-27

0 个投票

I personally managed to solve my problem.
But there were questions.
Solution: I used the MS SQL driver
11.png
In theory, all these drivers should work fine.
But only one MS SQL gave the correct result:
6143047 5999x1 uint8
224233 24889x1 uint8
4200382 6130x1 uint8
13687913 28456x1 uint8
2212046 8511x1 uint8
8346866 6400x1 uint8
1305479 6760x1 uint8
7806085 6641x1 uint8
6851834 7164x1 uint8
7084710 6550x1 uint8

产品

版本

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by