フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

batch-process images by pasting each onto a central background image

1 回表示 (過去 30 日間)
Luc
Luc 2018 年 11 月 29 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I have a large number (> 1,000) images of different sizes ("the figures") and want to paste each image centrally onto a background image of the same size and save as a new image. The figure images are greyscale and the background is a uniform greyscale image.
I tried the padarray function to pad each image on all sides with a background colour, but this function does not work on my hardware due to graphic card incompatibility.
I also found on here code (by Image Analyst) to do this manually but this is not feasible for the large set I need to process.
My question: Is there a way to automatically paste an image centrally onto another image ?

回答 (1 件)

Kushagr Gupta
Kushagr Gupta 2018 年 11 月 29 日
The following code can help replace central portion of a larger image (uniform background) with another smaller (actual image).
%Using an example to solve the problem
B = imread('cameraman.tif');
A = zeros(512,'like',B);
% Assuming large image is A
szA = size(A);
% Assuming smaller image is B
szB = size(B);
centerA = floor(szA(1:2)/2+1);
centerWindow = floor(szB(1:2)/2+1);
offset = centerA - centerWindow + 1;
R = offset(1);
C = offset(2);
H = szB(1);
W = szB(2);
% Replace central portion of larger image with the smaller one
A(R:(R+H-1),C:(C+H-1),:) = B;
% Display the new image
imshow(A)
This can be converted into a function which can be passed the background and central image as parameters. This function can then be called in a loop to automate the entire process.
Hope this helps.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by