border of random colour around grey image

2 visualizzazioni (ultimi 30 giorni)
Oscar Tsang
Oscar Tsang il 3 Mar 2019
Modificato: DGM il 16 Lug 2022
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
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');

Risposte (2)

Image Analyst
Image Analyst il 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 Commenti
Image Analyst
Image Analyst il 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
DGM
DGM il 16 Lug 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.

Accedi per commentare.


DGM
DGM il 16 Lug 2022
Modificato: DGM il 16 Lug 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');
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');
... 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)

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by