Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying gridlines in MATLAB imagesc function

I have been trying to display solid black gridlines using the imagesc function, such that each pixel has a black boundary around it. I have tried a few methods, but it seems that no matter what, the lines always go through the pixel. As an example, for imagesc(randn(21,21)), I am trying to get a plot where each square (ie. pixel) here has a black border.

I found one solution here: In matlab, how to draw a grid over an image, but I am unsure how to get it to work with imagesc, and not a.jpg image.

I have also tried using the hold on function to place the lines manually. But every solution, it seems that the grid lines pass through the middle of the pixel. Any help would be appreciated. Thank you.

like image 392
AJA Avatar asked Dec 19 '22 03:12

AJA


2 Answers

pcolor does exactly that:

pcolor(randn(15,21))
axis image %// equal scale on both axes
axis ij %// use if you want the origin at the top, like with imagesc

enter image description here

like image 143
Luis Mendo Avatar answered Jan 02 '23 02:01

Luis Mendo


Try the following:

imagesc(randn(21,21))
hold on;
for i = 1:22
   plot([.5,21.5],[i-.5,i-.5],'k-');
   plot([i-.5,i-.5],[.5,21.5],'k-');
end

EDIT: The thing is the centers of the pixels are at the integer lattice points, so to outline the pixels you need to use coordinates that ends on .5.

like image 42
Jens Boldsen Avatar answered Jan 02 '23 02:01

Jens Boldsen