Maximum of the image size png Matlab

23 ビュー (過去 30 日間)
K. Taieb
K. Taieb 2020 年 7 月 8 日
コメント済み: Rik 2020 年 7 月 9 日
Hello,
I have a matrix T (1, 2761843)
I want save it as like an image .png. I used the following line:
Imwrite (T,['image' num2str(5) '.png']);
I just get the following error.
Error using pngwritec
PNG library failed: image size exceeds user limits in IHDR.
Error in writepng (line 280)
pngwritec(data, map, filename, colortype, bitdepth, ...
Error in imwrite (line 546)
feval(fmt_s.write, data, map, filename, paramPairs{:});
No problem with JPEG 2000.
How I can find the maximum of image size .png (width and length) that we can create it with Matlab?
Thank you!
  1 件のコメント
Rik
Rik 2020 年 7 月 8 日
You could try if the savepng mex function on the FEX suits your needs. I'm very far from fluent in C++, but it doesn't look like there is a limit imposed beyond the png format itself.
If you want to use Matlab tools, you could consider reshaping your data, prepending the true size and padding with zeros.

サインインしてコメントする。

採用された回答

Rik
Rik 2020 年 7 月 8 日
編集済み: Rik 2020 年 7 月 8 日
Short answer: trial and error.
Long answer:
This should probably be somewhere in the doc. Only after the trial and error did I actually go look for it, but I didn't find it in the imwrite or imformats documentation.
The official png spec suggests the maximum size in either dimension would be anything that fits in 4 bytes, well above what you are trying to do. So officially there is nothing wrong with what you're trying.
However, a test on my copy of R2020a shows the maximum is 10^6. I find it curious that the limitation is a rounded decimal, and not a rounded hex (e.g. 0xFFFFF, 0xFFFFE, or 0x0FFFF).
The only other limit seems to be that 'Images must contain fewer than 2^32 - 1 bytes of data.' So your other dimension can 'only' be 4294 pixels. That required about 34GB of RAM and writes a png of just over 4GB.
clc
fn='foo.png';
%increasing either by 1 will return an error
dim1=floor(2^32/10^6);
dim2=10^6;
try
if exist(fn,'file'),delete(fn);end
IM=uint8(randi(255,4294,dim2));
imwrite(IM,fn);
fprintf('size %d written correctly\n',dim1*dim2)
catch ME
fprintf('fail:%s\n',ME.message)
if exist(fn,'file'),delete(fn);end
end
  2 件のコメント
K. Taieb
K. Taieb 2020 年 7 月 8 日
Thank you!
I’m sorry, I don’t understand your explication “ Images must contain fewer than 2^32 - 1 bytes of data.”
Because if I can create an image.png with the same number of elements but with multi rows and multi columns.
T (1642, 1682), we can see 2761843 = 1642*1682+1.
Rik
Rik 2020 年 7 月 9 日
That last limit only means you can't have an image with 2^32 pixels (grayscale) or 2^32/3 pixels (RGB), which is a reasonable limit, since 2^32/3 is 1431655765.33. So you shouldn't have any issues with that limit for any reasonable situation.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 7 月 8 日
If you want to save your 1-D vector as a 2-D image, I suggest you resize it first using reshape().

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by