缩放并记录上一张图片像素点,保持恒定缩放

时间:2015-10-17 20:10:27

标签: image matlab

我想向用户显示一系列图像。对于用户以鼠标单击图像的形式输入的每个图像请求。将每次点击的坐标存储在矩阵中。因此,最终具有维数num_images x 2的矩阵。

function main()
    clc;
    global agent_pos;
    ....
    for i=1:numel(img_names),
        imname = [ path_img img_names{i}];
        im0 = imread(imname);
        imageHandle =imshow(im0);%_____________displays the image
        set(gcf,'units','normalized','outerposition',[0 0 1 1])
        set(imageHandle,'ButtonDownFcn',@ImageClickCallback);
        uiwait(gcf);
    end

end

function coordinates=ImageClickCallback ( objectHandle , eventData )
    axesHandle  = get(objectHandle,'Parent');
    coordinates = get(axesHandle,'CurrentPoint'); 
    coordinates = coordinates(1,1:2);
    global agent_pos;
    agent_pos=[agent_pos;coordinates]; %___ add these coordinates for each image
    uiresume;
end

我想要做的是使用GUI放大图片的一部分,然后点击图片。当我这样做时,matlab应记录像素点'相对于未经形成的图像的位置。此外,一旦在第一个图像中选择缩放位置。它应该是相同的。

当我尝试现在在第一张图像中通过GUI进行缩放,然后将指针更改回光标时,它会以某种方式停止并且不记录我的点击或转到下一张图像。 警告:图JavaFrame属性将在以后的版本中废弃。有关更多信息,请参阅MathWorks网站上的JavaFrame资源。

我尝试使用imtools而不是imshow,但它会在下面显示警告,一次打开2张图片而不记录点击次数。

  

主要在20岁       显示java.lang.NullPointerException           在com.mathworks.hg.peer.FigureFrameProxy.setMaximized(FigureFrameProxy.java:302)           在com.mathworks.hg.peer.FigureMediator.setMaximized(FigureMediator.java:468)           在com.mathworks.hg.peer.FigurePeer.doSetMaximized(FigurePeer.java:3414)           在com.mathworks.hg.peer.FigurePeer $ 26.run(FigurePeer.java:3403)           在com.mathworks.hg.util.HGPeerQueue $ HGPeerRunnablesRunner.runit(HGPeerQueue.java:294)           在com.mathworks.hg.util.HGPeerQueue $ HGPeerRunnablesRunner.runNotThese(HGPeerQueue.java:326)           在com.mathworks.hg.util.HGPeerQueue $ HGPeerRunnablesRunner.run(HGPeerQueue.java:342)           at java.awt.event.InvocationEvent.dispatch(Unknown Source)           at java.awt.EventQueue.dispatchEventImpl(Unknown Source)           在java.awt.EventQueue.access $ 200(未知来源)           在java.awt.EventQueue $ 3.run(未知来源)           在java.awt.EventQueue $ 3.run(未知来源)           at java.security.AccessController.doPrivileged(Native Method)           at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)           at java.awt.EventQueue.dispatchEvent(Unknown Source)           at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)           at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)           at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)           at java.awt.EventDispatchThread.run(Unknown Source)       用户在uiwait期间终止操作(第81行)

编辑1: 完整代码:

function main()
    clc;
    global agent_pos;
    h = figure(1);   % Initiate figure with a specific number for easy handling
    set(h,'WindowButtonDownFcn',@ImageClickCallback);
    agent_pos=[];
    path_img='.\image_files_resized\';  %__________image files path
    path_posmap='.\pos_maps\';%_________stores positions of agents
    NumOfImages = length(dir(path_img)) - 2;
    w = dir(path_img);
    img_names={};      %________stores names of all images
    for i=3:NumOfImages+2,
        img_names{i-2} = w(i).name;
    end
    for ii=1:numel(img_names),
         % Get the XLim and YLim from previous imshow session
        if ii > 1   
            XLim = h.CurrentAxes.XLim;
            YLim = h.CurrentAxes.YLim;
        end


        imname = [ path_img img_names{ii}];
        im0 = imread(imname);
        figure(1);
        imageHandle =imshow(im0);%_____________displays the image
        [ht,wid,c]=size(im0);


        if ii==1,
            pause(0.00001);
            frame_h = get(handle(gcf),'JavaFrame');
            set(frame_h,'Maximized',1);
             hold on;
            [x,y]=getPrevAgents();
            plot(x,y,'o','color','green')
        end
        xlabel(img_names{ii});
        % Set the zoom level and location the same with previous imshow session
        if ii > 1    
            set(h.CurrentAxes, 'XLim', XLim)
            set(h.CurrentAxes, 'YLim', YLim)
        end
        uiwait(gcf);
    end
    filename = datestr(now,'mm_dd-HH_MM_SS');
    csvwrite(strcat(path_posmap,filename,'.csv'),agent_pos);
    close(gcf);
    plot_posagents();
    d = dir(strcat(path_posmap,'*.csv'));
    numfiles = length(d);
    disp(sprintf('Only %d files done..',length(d)));
end
function coordinates = ImageClickCallback ( objectHandle , eventData )
    global agent_pos;
    coordinates = get(objectHandle,'CurrentPoint'); 
    coordinates = coordinates(1, 1:2);
    agent_pos = [agent_pos;coordinates]; 
    uiresume;
end

ERROR:

Invalid or deleted object.

Error in main_new (line 18)
            XLim = h.CurrentAxes.XLim;

1 个答案:

答案 0 :(得分:3)

在MATLAB中,通过设置当前轴的XLimYLim来实现缩放。因此,要获取缩放信息,只需从轴获取这些属性即可。如果所有图像的大小相同,则无需执行任何计算即可将缩放系数和缩放位置应用于不同的图像。否则,缩放系数将为(XLim[2] - XLim[1])/size(img,2)CurrentPoint是图的属性,但不是它的轴。因此,在您的代码中,您将无法获得任何坐标。如果您首先单击缩放按钮,放大或缩小,单击缩放然后转到您喜欢的位置并单击,则以下代码应该可以正常工作。如果在缩放仍处于打开状态时单击鼠标按钮,则'WindowsButtondownFcn'将是缩放对象的function main() global agent_pos; h = figure(1); % Initiate figure with a specific number for easy handling set(h,'WindowButtonDownFcn',@ImageClickCallback); for ii = 1:numel(img_names) % Get the XLim and YLim from previous imshow session if ii > 1 % for version 2014b and later % XLim = h.CurrentAxes.XLim; % YLim = h.CurrentAxes.YLim; CurrentAxes = get(h, 'CurrentAxes'); XLim = get(CurrentAxes, 'XLim'); YLim = get(CurrentAxes, 'YLim'); end imname = [ path_img img_names{ii}]; figure(1) % To ensure we're always staying on the same figure imshow(imname) % Set the zoom level and location the same with previous imshow session if ii > 1 % for version 2014b and later % set(h.CurrentAxes, 'XLim', XLim) % set(h.CurrentAxes, 'YLim', YLim) CurrentAxes = get(h, 'CurrentAxes'); set(CurrentAxes, 'XLim', XLim) set(CurrentAxes, 'YLim', YLim) end uiwait(gcf); end end function coordinates = ImageClickCallback ( objectHandle , eventData ) global agent_pos; coordinates = get(objectHandle,'CurrentPoint'); coordinates = coordinates(1, 1:2); agent_pos = [agent_pos;coordinates]; uiresume; end ,并且不会执行您希望它执行的操作。

if ii==1,
    CurrentAxes = get(h, 'CurrentAxes');
    XLim = get(CurrentAxes, 'XLim');
    YLim = get(CurrentAxes, 'YLim');

    % your code here
    pause(0.00001);
    ...
    plot(x,y,'o','color','green')

    set(CurrentAxes, 'XLim', XLim)
    set(CurrentAxes, 'YLim', YLim)
end

应该注意的是,自MATLAB 2014b以来,图形句柄的实现方式发生了重大变化。见here

为确保您的绘图命令不会更改缩放设置,您可以添加这些

<context:property-placeholder
    location="classpath:batch-sql.properties/>

<bean id="secondReader"
    class="org.springframework.batch.item.database.JdbcCursorItemReader"
    scope="step">
    <property name="dataSource" ref="dataSource" />
    <property name="sql" value="${sql1}" />
    <property name="rowMapper">
        <bean class="com.test.batchjob.process.TestPersonMapper" />
    </property>
</bean>