You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Giving a frame to a image
7 views (last 30 days)
Show older comments
How to make a function which frames a image with input as image, frame size and frame color. Where frame colour is a vector of 3 numbers from 0 to 1. The proportion of frame width should be taken from smaller two dimensions of image.
Accepted Answer
Ameer Hamza
on 5 Sep 2020
Edited: Ameer Hamza
on 5 Sep 2020
Try something like this
im = imread('pears.png');
im = im2double(im);
frame_size = 100;
frame_color = [1, 0, 1];
framed_image = frameImage(im, frame_color, frame_size);
imshow(framed_image);
function im_out = frameImage(im, frame_color, frame_size)
im_out = repmat(reshape(frame_color, 1, 1, []), ...
size(im, [1 2])+2*frame_size);
im_out(frame_size+1:end-frame_size, frame_size+1:end-frame_size, :) = im;
end
19 Comments
Mayur Deogade
on 5 Sep 2020
This code has a error and it says
Error: File: frameImage.m Line: 8 Column: 11
Function definitions are not permitted in this context.
Ameer Hamza
on 6 Sep 2020
Which version of MATLAB are you using?
Mayur Deogade
on 6 Sep 2020
Matlab R2016a
Ameer Hamza
on 6 Sep 2020
In that case, create a new file named frameImage.m and paste the following code inside it
function im_out = frameImage(im, frame_color, frame_size)
im_out = repmat(reshape(frame_color, 1, 1, []), ...
size(im, [1 2])+2*frame_size);
im_out(frame_size+1:end-frame_size, frame_size+1:end-frame_size, :) = im;
end
and then run the following code
im = imread('pears.png');
im = im2double(im);
frame_size = 100;
frame_color = [1, 0, 1];
framed_image = frameImage(im, frame_color, frame_size);
imshow(framed_image);
Mayur Deogade
on 6 Sep 2020
frameImage has the function
today1 has the code
Tried this too it says the following error.
Error using size
Dimension argument must be a positive integer scalar within indexing range.
Error in frameImage (line 2)
im_out = repmat(reshape(frame_color, 1, 1, []),size(im, [1 2])+2*frame_size);
Error in today1 (line 5)
framed_image = frameImage(im, frame_color, frame_size);
Ameer Hamza
on 6 Sep 2020
Can you show your input image?
Mayur Deogade
on 6 Sep 2020
Ameer Hamza
on 6 Sep 2020
Since you are using an older version of MATLAB, the size() function have different input parameters. Change the function definition to this
function im_out = frameImage(im, frame_color, frame_size)
im_out = repmat(reshape(frame_color, 1, 1, []), ...
[size(im, 1) size(im, 2)]+2*frame_size);
im_out(frame_size+1:end-frame_size, frame_size+1:end-frame_size, :) = im;
end
Mayur Deogade
on 6 Sep 2020
Thank you so much for your guidance.
Now i need to give the border inside of the image. The outer dimensions should be same for image. How can i do it?
Ameer Hamza
on 6 Sep 2020
Try this
function im_out = frameImage(im, frame_color, frame_size)
im_out = repmat(reshape(frame_color, 1, 1, []), ...
[size(im, 1) size(im, 2)]);
im_out(frame_size+1:end-frame_size, frame_size+1:end-frame_size, :) = ...
im(frame_size+1:end-frame_size, frame_size+1:end-frame_size, :);
end
Mayur Deogade
on 6 Sep 2020
Thank you so much for your kind help. Now, i am facing a problem with the montage. I need to create a montage for 20 images.
the 20 images are derived from the same image with a change in border_size and then i have to make a montage with the help of using a loop
Ameer Hamza
on 6 Sep 2020
Can you show an example of how 20 images will be displayed?
Mayur Deogade
on 6 Sep 2020
I did this my writing a code 20 times as i am not able to store the images and plotted the image using array.
But i need to use montage.
Ameer Hamza
on 6 Sep 2020
Try this code
im = imread('Autumn.png');
im = im2double(im);
frame_size = randi([1, 100], 20, 1); % random frame sizes
frame_color = rand(20, 3); % random colors
images = cell(20, 1);
for i = 1:20
images{i} = frameImage(im, frame_color(i,:), frame_size(i));
end
montage(images, 'Size', [4 5]);
Mayur Deogade
on 6 Sep 2020
Error using images.internal.getImageFromFile (line 7)
The specified filename is not a string.
Error in montage>getImagesFromFiles (line 355)
[img, map] = images.internal.getImageFromFile(fileNames{1});
Error in montage>parse_inputs (line 241)
[I,cmap] = getImagesFromFiles(varargin{1});
Error in montage (line 114)
[I,cmap,mSize,indices,displayRange,parent] = parse_inputs(varargin{:});
Error in today2 (line 9)
montage(images, 'Size', [4 5]);
Image Analyst
on 6 Sep 2020
Try imtile() instead of montage(), or else pass montage() the filenames, not the images themselves.
Ameer Hamza
on 7 Sep 2020
An alternate solution for R2016a is
im = imread('Autumn.png');
im = im2double(im);
frame_size = randi([1, 100], 20, 1); % random frame sizes
frame_color = rand(20, 3); % random colors
images = cell(20, 1);
for i = 1:20
images{i} = frameImage(im, frame_color(i,:), frame_size(i));
end
images = cat(4, images{:});
montage(images, 'Size', [4 5]);
Mayur Deogade
on 8 Sep 2020
Thank you so much for your help. It really helped me a lot.
Ameer Hamza
on 9 Sep 2020
I am glad to be of help!
More Answers (0)
See Also
Tags
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)