How do I create a gradient border around an image?

4 次查看(过去 30 天)
I need to create a border around an image that would gradually change it's color from the image's color to white (a gradient).
Thank you.

采纳的回答

Walter Roberson
Walter Roberson 2017-5-11
borderwidth = 7; %for example
[R, C, P] = size(YourImage);
augmented_col_coords = [-borderwidth+1, 1:C, C+borderwidth];
augmented_row_coords = [-borderwidth+1, 1:R, R+borderwidth];
resample_col_coords = -borderwidth+1 : C + borderwidth;
resample_row_coords = -borderwidth+1 : R + borderwidth;
extra_line = repmat(255, 1, C+2);
augmented_red = [extra_line; 255*ones(R,1), double(YourImage(:,:,1)), 255*ones(R,1); extra_line];
gradiented_red = uint8( interp2(augmented_col_coords, augmented_row_coords, augmented_red, resample_col_coords, resample_row_coords.') );
augmented_green = [extra_line; 255*ones(R,1), double(YourImage(:,:,2)), 255*ones(R,1); extra_line];
gradiented_green = uint8( interp2(augmented_col_coords, augmented_row_coords, augmented_green, resample_col_coords, resample_row_coords.') );
augmented_blue = [extra_line; 255*ones(R,1), double(YourImage(:,:,3)), 255*ones(R,1); extra_line];
gradiented_blue = uint8( interp2(augmented_col_coords, augmented_row_coords, augmented_red, resample_col_coords, resample_row_coords.') );
gradiented_image = cat(3, gradiented_red, gradiented_green, gradiented_blue);
  6 个评论
Ignas Sveikauskas
Ignas Sveikauskas 2017-5-20
What if i only want to fix the color change problem? What changes must be made to the original code, so that the colors wouldn't get changed?
Walter Roberson
Walter Roberson 2017-5-20
gradiented_blue = uint8( interp2(augmented_col_coords, augmented_row_coords, augmented_blue, resample_col_coords, resample_row_coords.') );

请先登录,再进行评论。

更多回答(1 个)

DGM
DGM 2022-7-16
编辑:DGM 2022-7-16
You could also do this by using inpainting tools.
Using IPT regionfill():
A = imread('peppers.png');
padwidth = [30 30];
outercolor = 255; % must be scalar (black/gray/white)
% create a mask to define the transition region
mk = false([size(A,1) size(A,2)]);
mk = padarray(mk,padwidth-1,1,'both');
mk = padarray(mk,[1 1],0,'both');
% pad the image
B = padarray(A,padwidth,outercolor,'both');
% inpaint based on the mask
for c = 1:size(B,3)
B(:,:,c) = regionfill(B(:,:,c),mk);
end
imshow(B)
Using John's inpaint_nans():
A = imread('peppers.png');
padwidth = [30 30];
outercolor = 255; % must be scalar (black/gray/white)
% pad the image, using NaNs to define the transition region
B = padarray(double(A),padwidth-1,NaN,'both');
B = padarray(B,[1 1],outercolor,'both');
% inpaint based on NaNs
for c = 1:size(B,3)
B(:,:,c) = inpaint_nans(B(:,:,c),2);
end
B = uint8(B); % cast back to uint8
imshow(B)
Note that both of these examples assume that input image is class uint8.

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by