Matlab:正确的选择?

时间:2014-05-29 17:58:35

标签: matlab

我希望使用鼠标在matlab中选择图像区域,将角落的x / y返回给用户。

在线查看,图像处理工具箱中的正确功能正是如此,但我没有图像处理工具箱。

是开源替代品,即在matlab文件交换中吗?

标记

1 个答案:

答案 0 :(得分:4)

如果您可以使用全局变量和回调,那么可能会执行以下操作。创建一个传递(例如)您想要显示的图像的函数

function extractblock(I)

    % clear all global variables (or just those specific to this app)
    clear GLOBAL;

    % display the image
    figure;
    image(I);

    % set callbacks for the handling of the mouse button down, motion, and
    % up events against this figure
    set(gcf,'WindowButtonDownFcn',   @myButtonDownCallback, ...
            'WindowButtonUpFcn',     @myButtonUpCallback,   ...
            'WindowButtonMotionFcn', @myButtonMotionCallback);
end

现在定义回调。从鼠标按钮向下事件开始,该事件将记录鼠标按钮已被按下及其位置(此回调和其他回调可以全部放在与上述功能相同的文件中):

function myButtonDownCallback(~,~)

    global IS_BUTTON_DOWN;
    global RECT_START_COORD;

    IS_BUTTON_DOWN   = true;

    % get top left corner of rectangle
    RECT_START_COORD = get(gca,'CurrentPoint'); 
end

现在使用回调来处理鼠标移动,当用户在图像上移动鼠标指针时,回调将绘制(或重新绘制)矩形:

function myButtonMotionCallback(~,~)

    global IS_BUTTON_DOWN;
    global RECT_START_COORD;
    global RECT_END_COORD;
    global RECTANGLE_HANDLE;

    if ~isempty(IS_BUTTON_DOWN) && IS_BUTTON_DOWN

        % get bottom right corner of rectangle
        RECT_END_COORD = get(gca,'CurrentPoint');

        % get the top left corner and width and height of 
        % rectangle (note the absolute value forces it to "open"
        % from left to right - need smarter logic for other direction)
        x = RECT_START_COORD(1,1);
        y = RECT_START_COORD(1,2);
        w = abs(x-RECT_END_COORD(1,1));
        h = abs(y-RECT_END_COORD(1,2));

        % only draw the rectangle if the width and height are positive
        if w>0 && h>0

            % rectangle drawn in white (better colour needed for different
            % images?)
            if isempty(RECTANGLE_HANDLE)
                % empty so rectangle not yet drawn
                RECTANGLE_HANDLE = rectangle('Position',[x,y,w,h],'EdgeColor','w');
            else
                % need to redraw
                set(RECTANGLE_HANDLE,'Position',[x,y,w,h],'EdgeColor','w');
            end
        end
    end  
end

现在处理鼠标向上事件,该事件将从图中移除矩形并将矩形的角写出到命令窗口(如果需要返回值,则必须添加某种矩阵的意思) :

function myButtonUpCallback(~,~)

    global IS_BUTTON_DOWN;
    global RECTANGLE_HANDLE;
    global RECT_START_COORD;
    global RECT_END_COORD;

    % reset the button down flag
    IS_BUTTON_DOWN = false;

    % delete the rectangle from the figure
    delete(RECTANGLE_HANDLE);

    % clear the handle
    RECTANGLE_HANDLE = [];

    % compute the top left (tl) and bottom right (br) coordinates
    tl = [RECT_START_COORD(1,1)  RECT_START_COORD(1,2)];
    br = [RECT_END_COORD(1,1)    RECT_END_COORD(1,2)];

    % compute the top right (tr) and bottom left (bl) coordinates
    tr = [br(1)  tl(2)];
    bl = [tl(1)  br(2)];

    % write coordinates to command window
    fprintf('(%f,%f)\t',tl(1),tl(2));
    fprintf('(%f,%f)\n',tr(1),tr(2));
    fprintf('(%f,%f)\t',bl(1),bl(2));
    fprintf('(%f,%f)\n',br(1),br(2));
    fprintf('\n');

    % optionally display the block from the image
end

以上是从图像中提取用户定义的块的快速方法,缺少一些逻辑来处理从右到左绘制的矩形。希望这有帮助!