border of random colour around grey image
2 views (last 30 days)
Show older comments
Hi,
I was wondering how would you put a border of random colour (50 mpixels wide) around the grey image?? Just stuck on how to approach it. I have attached the iamge I have used.
An image in what it suppose to look like:
![IMG_20190303_183049.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206663/IMG_20190303_183049.jpeg)
The code i have done so far:
owl = imread('owl.jpg');
owl_eye=owl;
owl_eye=owl_eye(350:450,600:710,:)
figure
imshow(owl_eye,'InitialMagnification', 'fit');
owleye_gray=rgb2gray(owl_eye);
figure
imshow(owleye_gray,'InitialMagnification', 'fit');
0 Comments
Answers (2)
Image Analyst
on 3 Mar 2019
Use the color thresholder app on the Apps tab of the tool ribbon to create a mask for the yellow owl eyes. Use hsv color space. Then use bwareafilt() to get only the two eyes
mask = bwareafilt(mask, 2); % Extract 2 largest blobs.
Then use bwconvhull to get the convex hull of them and then use regionprops to get the bounding box:
mask = bwconvhull(mask); % Combine two eyes into one blob.
props = regionprops(mask, 'BoundingBox'); % Get bounding box of the single blob.
hold on;
% Put random colored box in the overlay above the image.
rectangle('Position', props.BoundingBox, 'Color', rand(1, 3)); % Box in random color over image.
Post your code with any problems.
9 Comments
Image Analyst
on 5 Mar 2019
Oscar:
Yes, that's weird. Same for me. Maybe I'll call the Mathworks tomorrow and ask them about it. Meanwhile, my code is attached.
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206844/0000%20Screenshot.png)
DGM
on 16 Jul 2022
The length of padsize defines which dimensions get padded; padarray() does not expand padsize if it's specified as a scalar. As a consequence, only the first dimension will get padded.
DGM
on 16 Jul 2022
Edited: DGM
on 16 Jul 2022
While this answer covers a number of ways to get a border on an image, it also demonstrates that getting a colored border is a bit inconvenient using MATLAB/IPT tools. Considering that, I'm going to use MIMT addborder() as described therein.
A = imread('peppers.png');
bcolor = rand(1,3); % random color
B = addborder(A,10,bcolor,'normalized');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1067225/image.png)
Of course, instead of an entirely random color, you could draw colors randomly from a color table:
A = imread('peppers.png');
cmap = parula(64);
bcolor = cmap(randi([1 size(cmap,1)],1,1),:);
B = addborder(A,10,bcolor,'normalized');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1067230/image.png)
... that would at least keep everything within a specified color scheme:
A = imread('peppers.png');
A = imresize(A,0.5); % don't need a bunch of big images for a demo
cmap = parula(64);
B = cell(4,1);
for f = 1:numel(B)
bcolor = cmap(randi([1 size(cmap,1)],1,1),:);
B{f} = addborder(A,5,bcolor,'normalized');
end
montage(B)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1067235/image.png)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!