Youssi
Youssi

Reputation: 63

Changing the position of an image inside another image

I need your help please. I'm working in Matlab and I have tow images one with size 256*256 and the other one with size 64*64. I want to embed the small image into the first one but i would like to control the position of the embedding process. For example having the small image in the corner of the big image and then I want to change it in the middle ... do you have any idea how ? thank you

Upvotes: 1

Views: 523

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

Here is a way using ginput, with which you select a number of points and get their x-y coordinates. Here the user selects a single point (so we use ginput(1)), which becomes the top-left corner of the small image, placed "inside" the big image. I suggest copying this exact code in a .m file and play around with it.

If you have questions please ask.

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

And sample output after pressing the button. If you press it again the original image appears and you can thus change the position of the small image as you wish.

enter image description here

Upvotes: 2

Related Questions