![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/534629/image.png)
How to center and add margin around image?
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Here is my code, what im trying to do is to put image I2 on top of image I1 center it and create a white margin around it that is 50 pixels. Ive googled some possible solutions for the margin and im not having any luck in implementing those solutions. Any help?
I1 = imread('blue-lake.jpg');
I2 = imread('portrait.jpg');
I1 = imresize(I1, 1);
IM2 = imresize(I2, 2);
I1 = image(I1)
hold on;
image(IM2);
0 Kommentare
Antworten (4)
KALYAN ACHARJYA
am 28 Feb. 2021
Bearbeitet: KALYAN ACHARJYA
am 28 Feb. 2021
You can manually adjust the indices and overlap with the main image. Note that the main image pixel values will be lost in the overlap portion. Considering main image is larget than marker image.
main_image=imread('......');
marker=imread('.......');
[r,c]=size(main_image);
[r1,c1,ch]=size(marker);
%Make the white border at the boundary
pix_add=50; % In each boundary, 100 for Both sides
im_data=uint8(255*ones(r1+2*pix_add,c1+2*pix_add,3));
im_data(pix_add+1:r1+pix_add,pix_add+1:c1+pix_add,:)=marker;
% White Border in the marker is done
imshow(im_data);
Next: Find the coordinates where you want to place the marker in the main image. You can find data of approximately middle rows and columns from R and C. Once you get the coordinates, the next task is to put im_data in the main_image as I did in the marker (last line of code)
Note: A fairly simple problem, first use pen and paper to decide the coordinates.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/534629/image.png)
Hope you can do the next step. :)
0 Kommentare
Image Analyst
am 28 Feb. 2021
Have you tried padarray()? Then use indexing to paste it onto the other image. I'm attaching a copying and pasting demo. Check out the pasting part, or Kalyan's code to see how to paste your padded array onto the image.
0 Kommentare
alexander Mcghee
am 27 Apr. 2023
Bearbeitet: alexander Mcghee
am 27 Apr. 2023
% Read the input images
I1 = imread('path/to/your/image1.jpg');
I2 = imread('path/to/your/image2.jpg');
% rescale the second image height and width by 1/2
I2 = imresize(I2, 0.5);
% Set the margin size
margin_size = 50;
% Get the size of the input images
[h1, w1, c1] = size(I1);
[h2, w2, c2] = size(I2);
% Create a white margin around Image I2
I2_margin = padarray(I2, [margin_size margin_size], 255, 'both');
% Get the size of the image with margin
[h2_margin, w2_margin, ~] = size(I2_margin);
% Initialize a new image with the same size as Image I1
merged_image = I1;
% Calculate the position to center Image I2 with margin on Image I1
top_left_row = floor((h1 - h2_margin) / 2) + 1;
top_left_col = floor((w1 - w2_margin) / 2) + 1;
% Place the Image I2 with margin on top of Image I1
merged_image(top_left_row:top_left_row+h2_margin-1, top_left_col:top_left_col+w2_margin-1, :) = I2_margin;
% Display the merged image
imshow(merged_image);
0 Kommentare
DGM
am 27 Apr. 2023
FWIW, this is how I'd do it with MIMT.
% contrive two images of different scale, class, size, and depth
BG = imread('peppers.png'); % RGB, uint8 (0 to 255)
FG = im2int16(imread('cameraman.tif')); % I, int16 (-32768 to 32767)
% add matting to FG image
fgmatcolor = [0.2 0.1 1 0.5]; % pick any tuple (I/IA/RGB/RGBA)
fgmatwidth = [20 40]; % widths can be 1,2,or 4 terms
FG = addborder(FG,fgmatwidth,fgmatcolor,'normalized');
% assemble the output image
outpict = imstacker({FG,BG}); % stack
outpict = mergedown(outpict,1,'normal'); % composite
outpict = splitalpha(outpict); % discard unneeded alpha channel
imshow(outpict)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1368404/image.png)
It's more expensive than a minimal approach, but it's simple to use and generalized. Note that I never once had to consider the fact that the images are incompatible class, scale, or size. That makes it ideal for one-off ad-hoc usage like this.
See also:
Other ways to add black/colored/patterned borders on images
Adding a graduated border on an image
0 Kommentare
Siehe auch
Kategorien
Mehr zu Display Image finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!