Convert image to a c++ header file

5 ビュー (過去 30 日間)
Thilina Prasanga Doremure Gamage
Thilina Prasanga Doremure Gamage 2019 年 3 月 14 日
Can someone tell me why can't I get this to work?! It should be working!
The script is as below;
function export_Img_2_Header( img, filename )
%EXPORT_IMG_2_HEADER Summary of this function goes here
% Detailed explanation goes here
sizeArray = numel(img);
fileID = fopen(filename,'w');
fprintf(fileID,'//Image on header\n');
fprintf(fileID,'unsigned char img[%d] = {',sizeArray);
img_sq = reshape(img,[1 sizeArray]);
for idx = 1:numel(img_sq)
val = img_sq(idx);
if idx ~= sizeArray
fprintf(fileID,'%d ,',val);
else
fprintf(fileID,'%d',val);
end
end
fprintf(fileID,'};\n');
fclose(fileID);
end
  1 件のコメント
Jan
Jan 2019 年 3 月 14 日
Please mention, what "can't get it to work" means. The code looks fine, so I guess you made a mistake at calling it.

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

回答 (2 件)

Saipraveen
Saipraveen 2019 年 9 月 30 日
In this file exchange - https://www.mathworks.com/matlabcentral/fileexchange/72535-image-to-c-c-array-converter, I have written a script to generate a C/C++ array which can be saved as a header file for use with various embedded displays.
Hope that helps.

Jan
Jan 2019 年 3 月 14 日
編集済み: Jan 2019 年 3 月 14 日
Maybe this helps - smarter without a loop:
function export_Img_2_Header( img, filename )
% EXPORT_IMG_2_HEADER Create C++ header file containing image data
[fileID, msg] = fopen(filename, 'W');
if fileID == -1
error('Cannot open file for writing: %s, %s', filename, msg);
end
% Maybe:
img = im2uint8(img);
n = numel(img);
fprintf(fileID, '// Image on header\nunsigned char img[%d] = {', n);
fprintf(fileID,'%d ,', img(1:n-1));
fprintf(fileID,'%d};\n', img(n));
fclose(fileID);
end

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by