オブジェクトを生成する MATLAB の .NET アセンブリを使用していSystem.Drawing.Bitmap
ます。ピクセルを含む MATLAB マトリックスを取得したいと思います。どうやってそれをしますか?
画像をディスクに保存してから使用したくありませんimread
。
オブジェクトを生成する MATLAB の .NET アセンブリを使用していSystem.Drawing.Bitmap
ます。ピクセルを含む MATLAB マトリックスを取得したいと思います。どうやってそれをしますか?
画像をディスクに保存してから使用したくありませんimread
。
Jeroenの回答に基づいて、変換を行う MATLAB コードを次に示します。
% make sure the .NET assembly is loaded
NET.addAssembly('System.Drawing');
% read image from file as Bitmap
bmp = System.Drawing.Bitmap(which('football.jpg'));
w = bmp.Width;
h = bmp.Height;
% lock bitmap into memory for reading
bmpData = bmp.LockBits(System.Drawing.Rectangle(0, 0, w, h), ...
System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
% get pointer to pixels, and copy RGB values into an array of bytes
num = abs(bmpData.Stride) * h;
bytes = NET.createArray('System.Byte', num);
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, bytes, 0, num);
% unlock bitmap
bmp.UnlockBits(bmpData);
% cleanup
clear bmp bmpData num
% convert to MATLAB image
bytes = uint8(bytes);
img = permute(flipdim(reshape(reshape(bytes,3,w*h)',[w,h,3]),3),[2 1 3]);
% show result
imshow(img)
最後のステートメントは理解しにくい場合があります。これは、実際には次のものと同等です。
% bitmap RGB values are interleaved: b1,g1,r1,b2,g2,r2,...
% and stored in a row-major order
b = reshape(bytes(1:3:end), [w,h])';
g = reshape(bytes(2:3:end), [w,h])';
r = reshape(bytes(3:3:end), [w,h])';
img = cat(3, r,g,b);
結果: