Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

3 次查看(过去 30 天)
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