Can we have an image in a matrix that can be called by a function?

1 回表示 (過去 30 日間)
Gopichandh Danala
Gopichandh Danala 2018 年 5 月 14 日
コメント済み: Gopichandh Danala 2018 年 5 月 14 日
Hi,
This may sound odd, but I want to have a image data defined as a matrix.
Which should be created by a function call.
I don't want to use imread or load a mat file to get image data.
Is this possible?
Example:
imageData = createImg() % function call
function imageData = createImg()
% imageData = imread('peppers.png') % usual way
% imageData = %idk somehow get this by definition
end
The reason for why I am doing this, I have a image as a GUI logo, which I am not planning to always put in a specific directory or copy to different computers when I have to run my GUI.
Update:
I came up with this way:
imgData = imread('logo_Img.png');
csvwrite('imgDataLogical.txt',logical(imgData(:,:,1)));
function logical_idx = my_createImg()
% imgData = imread('peppers.png'); % usual way
logical_idx = [... % data...]; % copy and paste data from txt file
end
logical_img = my_createImg();
[red_img,green_img,blue_img] = deal(uint8(255*(logical_img == 1)));
red_img(logical_img == 1) = 220; red_img(logical_img ~= 1) = 230;
green_img(logical_img == 1) = 20; green_img(logical_img ~= 1) = 230;
blue_img(logical_img == 1) = 60; blue_img(logical_img ~= 1) = 230;
fullImg = cat(3,red_img,green_img,blue_img);
figure, imshow(fullImg)
But I feel this is not well written any ideas to improve this?
Any ideas,
Gopi

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 5 月 14 日
How can you expect a function to create an image, without having the actual image? Unless your image is defined mathematically, i.e. containing circles, rectangles etc. If your image contains some other irregular shapes (i.e. shaped which cannot be defined easily by mathematical functions), it is quite difficult to create it by using a function. Unless you function file contain RGB values of all the pixels, which is not a good idea than carrying the image file. The best solution is to create a compressed zipped file, so that it is easy to carry files from one place to another.
  4 件のコメント
Ameer Hamza
Ameer Hamza 2018 年 5 月 14 日
In your code, you are only saving red channel in your csv file. Also, you are defining just two discrete level for each channel, it will decrease the quality of your image. If your image is small and you are ready to store the data in the file, why don't just use base64 encoding as mentioned by @Guillaume. It will be far more easy to use and compact. Here is a general sketch
imgData = imread('logo_Img.png');
encodedString = matlab.net.base64encode(imgData(:));
this will generate a long string. write it in a text file,
f = fopen('temp.txt');
fwrite(f, encodedString);
now copy the string from your file and place it in your image creating function
function image = my_createImg()
encodedString = % paste string here from temp.txt file
image = reshape(matlab.net.base64decode(encodedString), [...])); % fill [...] with your image dimension becuase it will decode the string as a vector.
end
Gopichandh Danala
Gopichandh Danala 2018 年 5 月 14 日
Thanks Ameer. It works

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

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 5 月 14 日
  1. An image is a matrix
  2. imread is one of many functions that returns an image as a matrix.
So, I guess your question is answered. Yes, it's possible, you have been doing it all along.
Now, if you want to store the actual image pixel as a function, of course, you can also do that. The easiest, assuming a fairly small image is to load the image into the workspace, right-click on the workspace variable and select Save As and save as a .m file. This will create the code for you as long as the matrix does not exceed a certain size (which you can adjust in the Preferences under Workspace.
For larger images, you could base64 encode it and store it as a long text string.
However, if your image is part of the gui, it will be saved as part of the .fig file so it'll be embedded anyway.
  5 件のコメント
Guillaume
Guillaume 2018 年 5 月 14 日
I don't particularly understand why you convert your image to logical, in the process going from possibly many colours to just two.
As I said, you can easily encode your image as a text string. base64 is the most commonly used method. Ameer has shown you how to do it. This is reliable and fast.
Gopichandh Danala
Gopichandh Danala 2018 年 5 月 14 日
ok. Thanks it worked

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

カテゴリ

Help Center および File ExchangeMarine and Underwater Vehicles についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by