简体   繁体   中英

Create a grid over an image

I want to create a grid over an image on matlab. At the same time, I want the image to be clickable and color the box of the grid I clicked.

Can anyone give me some guidance for this? What functions to use?

You may use the following example:

%Load sample image.
I = imread('yellowlily.jpg');

%Resize image to be multiple of 50 in each axis.
I = imresize(im2double(I), [400, 300]);

%Draw grid of 50x50 pixels.
I(50:50:end, :, :) = 255;
I(:, 50:50:end, :) = 255;

h = figure;imshow(I);

while (ishandle(h))
    try
        [x, y] = ginput(1);
    catch me
        %break loop in case image is closed.
        break;
    end

    %Compute top left coordinate of block clicked.
    x0 = floor((x-1)/50)*50;
    y0 = floor((y-1)/50)*50;

    %Set block RGB to random color.
    I(y0+1:y0+50-1, x0+1:x0+50-1, 1) = rand();
    I(y0+1:y0+50-1, x0+1:x0+50-1, 2) = rand();
    I(y0+1:y0+50-1, x0+1:x0+50-1, 3) = rand();

    imshow(I);
end

在此处输入图片说明

The example may be used as a starting point...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM