How do I create a gradient border around an image?

조회 수: 4 (최근 30일)
Ignas Sveikauskas
Ignas Sveikauskas 2017년 5월 11일
편집: DGM 2022년 7월 16일
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