border of random colour around grey image

조회 수: 8(최근 30일)
Oscar Tsang
Oscar Tsang 2019년 3월 3일
편집: DGM 2022년 7월 16일
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:
The code i have done so far:
owl = imread('owl.jpg');
imshow(owl_eye,'InitialMagnification', 'fit');
imshow(owleye_gray,'InitialMagnification', 'fit');


Image Analyst
Image Analyst 2019년 3월 3일
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
DGM 2022년 7월 16일
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 2022년 7월 16일
편집: DGM 2022년 7월 16일
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');

Community Treasure Hunt

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

Start Hunting!

Translated by