Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

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

3 visualizaciones (últimos 30 días)
Luc
Luc el 29 de Nov. de 2018
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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 ?

Respuestas (1)

Kushagr Gupta
Kushagr Gupta el 29 de Nov. de 2018
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.

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by