필터 지우기
필터 지우기

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