Search code examples
matlabfigureimshowedges

How can I change figure edges of a figure created with imshow?


I'm using imshow to create this binary image. When it shows the figure I see a grey background and no edges in the figure. If I save the plot in as .png, I see the background as white and I can't see any edges on the figure. How can I add edges to this plot?

Image as shown by imshow:

image showed by imshow command

Image as saved to PNG:

image saved in a .png format


Solution

  • By default, saved figures have a white background. Ensure that the colors of the saved figure match the colors on the display by setting the InvertHardcopy property of the figure to 'off'.

    Example:

    A = rand(300, 300) > 0.1;
    
    f = figure();
      f.InvertHardcopy = 'off';
      imshow(A);
      title('Binary Image threshold 0.9');
      saveas(f, 'test.png'); 
    

    gives:

    enter image description here

    Alternatively, it is possible to set the visibility of the axes in the imshow and make the ticks empty:

    A = rand(300, 300) > 0.1;
    
    f = figure();
      iptsetpref('ImshowAxesVisible', 'on');
      imshow(A);
      xticks({});
      yticks({});
      title('Binary Image threshold 0.9');
      saveas(f, 'test.png');
    

    that gives:

    enter image description here

    Source: Matlab Documentation