改变一个图像在另一个图像中的位置

Changing the position of an image inside another image

我需要你的帮助。 我在 Matlab 工作,我有两个图像,一个大小为 256*256,另一个大小为 64*64。我想将小图像嵌入到第一个图像中,但我想控制嵌入过程的位置。 例如在大图像的角落里有小图像,然后我想在中间改变它...... 你知道怎么做吗? 谢谢

这是一种使用 ginput 的方法,您可以用它 select 一些点并获得它们的 x-y 坐标。这里用户 select 是一个点(所以我们使用 ginput(1)),它成为小图像的左上角,放置 "inside" 大图像。我建议将此确切代码复制到 .m 文件中并使用它。

如有疑问请提问。

function PlaceImage

clear
clc
close all

%// Create figure and uielements
handles.fig = figure('Position',[440 400 500 230]);

handles.DispButton = uicontrol('Style','Pushbutton','Position',[20 70 80 40],'String','Select point','Callback',@PlaceImageCallback);

%// Set up big and small images
OriginalBigIm = imread('coins.png');

SmallIm = imread('circuit.tif');

SmallIm = imresize(SmallIm,[60 60]);


%// Get size of small image
[heightSmall,WidthSmall] = size(SmallIm);

imshow(OriginalBigIm,[]);


    function PlaceImageCallback(~,~)

        BigIm = OriginalBigIm;

        imshow(OriginalBigIm,[]);

        %// Select a point where to put the top-left corner of the small image
        [xTopLeft,yTopLeft] = ginput(1);

        xTopLeft = round(xTopLeft);
        yTopLeft = round(yTopLeft);

        %// Replace pixels in the big image with the small image
        BigIm(yTopLeft:yTopLeft+heightSmall-1,xTopLeft:xTopLeft+WidthSmall-1) = SmallIm;

        %// Display result
        imshow(BigIm,[]);
    end

end

以及按下按钮后的示例输出。再按一次,原图出现,小图的位置可以随心所欲地改变。