필터 지우기
필터 지우기

Problem writing images when GUI and Figure are open at the same time

조회 수: 3 (최근 30일)
Julian
Julian 2018년 9월 14일
댓글: Julian 2018년 9월 17일
Hi everyone,
i'm having a little trouble writing images.
I have a GUI which runs some analyzes on images. At some point i want to plot some markers into an image and save it afterwards. The code for this goes like this:
figure_handle = figure;
set(figure_handle,'Visible','off');
if(cboxchecked(handles.mergecbox))
try
imshow(handles.mergedimg);
title(['# cells: ', numberOfCells]);
hold on
plot(centroidCoordinates(:,1), centroidCoordinates(:,2), ...
'go','MarkerSize',7,...
'MarkerEdgeColor','b',...
'LineWidth',2,...
'MarkerFaceColor','g');
catch
warning('No merged image available');
end
end
saveas(figure_handle,[savingName],'png')
So i actually don't want the figure to pop up, so i set the visibility off. But when i do, the plot and title won't be in the figure that i am saving afterwards. If i switch the visibility on i don't have this problem but still i got some issues. I only get the results i want if i slowly step through the code and make sure the figure is always in the foreground. But when i simply run it, my GUI (with the original image that was uploaded) will normally be displayed in the foreground and then the plot will only appear in the GUI. Is there something i can do to fix this? Thanks a lot!
  댓글 수: 4
Kevin Chng
Kevin Chng 2018년 9월 14일
will it help? if small pause() + drawnow?
Julian
Julian 2018년 9월 14일
No, still the same problem. If the GUI is in the foreground, the plot will be displayed in there.

댓글을 달려면 로그인하십시오.

채택된 답변

OCDER
OCDER 2018년 9월 14일
Always specify which axes you are drawing to. Also, use print instead of saveas to get the desired resolution.
figure_handle = figure;
axes_handle = axes; %<=== track the axes handle
set(figure_handle,'Visible','off');
if(cboxchecked(handles.mergecbox))
try
imshow(handles.mergedimg, 'parent', axes_handle); %<==== specify axes handle to use
title(['# cells: ', numberOfCells]);
hold on
plot(axes_handle, centroidCoordinates(:,1), centroidCoordinates(:,2), ... %<=== specify axes
'go','MarkerSize',7,...
'MarkerEdgeColor','b',...
'LineWidth',2,...
'MarkerFaceColor','g');
drawnow(); %<=== refresh image
catch
warning('No merged image available');
end
end
%Make sure to setup the figure PaperPosition, Position, etc
print(figure_handle, sprintf('%s.png', savingName), '-dpng', '-r600')
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 9월 15일
I would go further to
figure_handle = figure;
axes_handle = axes('Parent', figure_handle); %<=== track the axes handle
set(figure_handle,'Visible','off');
if(cboxchecked(handles.mergecbox))
try
imshow(handles.mergedimg, 'parent', axes_handle); %<==== specify axes handle to use
title(axes_handle, ['# cells: ', numberOfCells]);
hold(axes_handle, 'on')
plot(axes_handle, centroidCoordinates(:,1), centroidCoordinates(:,2), ... %<=== specify axes
'go','MarkerSize',7,...
'MarkerEdgeColor','b',...
'LineWidth',2,...
'MarkerFaceColor','g');
drawnow(); %<=== refresh image
catch
warning('No merged image available');
end
end
%Make sure to setup the figure PaperPosition, Position, etc
print(figure_handle, sprintf('%s.png', savingName), '-dpng', '-r600')
Julian
Julian 2018년 9월 17일
Great, it works now, thank you guys!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by