How to remove the black outlines when using 'image' command to display a image.

Hi everyone,
I wanna display an image but also want to drag and drop this image. As far as I know, if I use command 'imshow' to display the image it is not draggable. I therefore have to use command 'Image' to display a single pixel image.
To simplify my question, I use the minimum code to this problem. See below.
Figure;
axes; % to draw an axes
I = ones(1,1,3); I(:,:,1) = 255; I(:,:,2) = 255;I(:,:,3) = 0; % to create a single pixel image file (yellow of course).
image(I) % display the single pixel yellow image in the axes.
ALL THE OUTLINES AROUND THE YELLOW IMAGE ARE UNWANTED. THEREFORE,
set(gca,'xtick',[]); % remove ticks on x-axis
set(gca,'ytick',[]); % remove ticks on y-axis
It then left with the black outlines around the yellow image which is unwanted. See below.
My question is: how to remove these black outlines if I use command 'Image' to show an image which can be dragged later on?
Many thanks!
Lily

 Risposta accettata

I = ones(1,1,3);
I(:,:,1) = 255; I(:,:,2) = 255;I(:,:,3) = 0;
AxesH = axes('Visible', 'off', 'NextPlot', 'add');
image(I, 'Parent', AxesH);
Setting the Visiblility to 'off', hides the ticks and the box.

5 Commenti

@Qianqian Pan, where you go wrong is in your statement y-axis is removed. No; All you've done is remove the tick marks. The axis is still visible. As Jan shows you, you want to hide the axis, not just the tick marks.
@Guillaume, sorry my expression wasn't so accurate or precise. Lucky I used some images which more accurately demonstrated the problem.:)
@ Jan Simon, thanks for your answer. It worked perfectly!
@ Jan Simon, can I ask why it needs 'NextPlot','add' here? I'm not sure what that is for. Many thanks!
Hhigh-level commands lile plot and image clean up the axes before they insert the object. This resets the limits, ticks, and many other properties, as also the value of 'Visible'.
Using hold('on') sets the property 'NextPlot' of the axes from the default 'replace' to 'add', such that imgae is instructed just to add the new object and not to clean up the properties. Instead of using the hold command, I set the 'NextPlot' property manually.
Try it:
figure;
pause(1)
AxesH = axes();
title(AxesH, 'Title');
xlabel('X');
ylabel('Y');
pause(1)
set(AxesH, 'Visible', 'off');
pause(1)
image(I, 'Parent', AxesH);
get(AxesH, 'Visible') % 'on' !!!
and now insert the 'NextPlot', 'add' in the axes() command: you will see, that the properties are preserved
figure;
pause(1)
AxesH = axes('NextPlot', 'add');
title(AxesH, 'Title');
xlabel('X');
ylabel('Y');
% pause(1)
% set(AxesH, 'Visible', 'off');
pause(1)
image(I, 'Parent', AxesH);
I've omitted to disable the visibility, because otherwise you could not see, that nothing is changed... ;-)
I find axes('NextPlot', 'add') cleaner and more direct than axes(); hold('on'), but this is a question of taste.
Alternatively you can call image() at first and set the visibility or the box afterwards.

Accedi per commentare.

Più risposte (1)

Set Box to off:
ax = gca
properties(ax) % Display properties if you want to see what you can control.
ax.Box = 'off'; % Turn off the box

Community Treasure Hunt

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

Start Hunting!

Translated by