Displaying grid lines in MATLAB imagesc function

I am trying to display solid black grid lines using the imagesc function so that every pixel has a black border around it. I've tried several methods but it seems like no matter the lines always go through the pixel. As an example, for imagesc (randn (21,21)), I'm trying to get a graph where every square (i.e. pixel) here has a black border.

I found one solution here: In Matlab, how to draw a grid over an image , but I'm not sure how to get it to work with imagesc and not Image a.jpg.

I also tried using the hold function to place the lines manually. But every solution seems like the grid lines go through the middle of the pixel. Any help would be greatly appreciated. Thank.

+3


source to share


2 answers


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: That the centers of the pixels are at integer grid points, so you need to use coordinates that end in .5 to highlight the pixels.

+5


source


pcolor

does exactly this:

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

+3


source







All Articles