How do I add custom gridlines on my image?
Mostrar comentarios más antiguos
So I'm trying to program an image converter and trying to get some gridlines on it.
axis equal;
imagesc(secondlevelTIFF, scale) %read matrix into figure
axis equal;
colormap(jet) %makes it into jet
axis on;
grid on;
colorbar;
temp=[[sprintf('fig%03d', i)],'.tif']; %name current figure file
fpath = [I have my path here]; %set file directory
saveas(gcf, fullfile(fpath, temp));
This is my current code with the 'secondlevelTIFF' being my image. I'm trying to add custom gridlines. The image file I have is 500*500 pixels and i want gridlines at 50 and 150 pixels. How do I add these gridlines so that they are on the top of my final image file?
1 comentario
Clay Swackhamer
el 17 de Oct. de 2018
If you are still working on this I wrote a simple function to put a grid on an image. It is on the file exchange here
Respuestas (2)
Image Analyst
el 21 de En. de 2017
Editada: Image Analyst
el 21 de En. de 2017
Either burn the lines into your image like this:
[rows, columns, numberOfColorChannels] = size(secondlevelTIFF);
for row = 1 : 50 : rows
secondlevelTIFF(row, :) = 255;
end
for col = 1 : 50 : columns
secondlevelTIFF(:, col) = 255;
end

(this will change your image), or draw a line in the overlay above your image like this
hold on;
for row = 1 : 50 : rows
line([1, columns], [row, row], 'Color', 'r');
end
for col = 1 : 50 : columns
line([col, col], [1, rows], 'Color', 'r');
end
This will not make changes to the image array itself - it will just place lines into the graphic overlay. Adapt spacing and offset as needed.
2 comentarios
Hosup Song
el 21 de En. de 2017
Image Analyst
el 21 de En. de 2017
Editada: Image Analyst
el 21 de En. de 2017
You implement it by coping and pasting that code I gave you into your code in the text editor window after you've displayed your image. It's very basic MATLAB skills. You have to learn how to write code into editor windows and how to debug them. If you already know how to do that, then I really don't understand your question at all. Here is a complete m-file for you:
secondlevelTIFF = imread('moon.tif');
imshow(secondlevelTIFF);
axis on;
[rows, columns, numberOfColorChannels] = size(secondlevelTIFF);
hold on;
for row = 1 : 50 : rows
line([1, columns], [row, row], 'Color', 'r');
end
for col = 1 : 50 : columns
line([col, col], [1, rows], 'Color', 'r');
end
And here is what it looks like:

Star Strider
el 21 de En. de 2017
You have to create a figure object, then put the gridlines on top of the image.
figure(1)
imagesc(secondlevelTIFF, scale) %read matrix into figure
set(gca, 'Layer','top')
... REST OF YOUR CODE ...
7 comentarios
Walter Roberson
el 21 de En. de 2017
The layer top is the obscure bit. Other than that you use the fact that grid lines are placed at tick positions.
Image Analyst
el 21 de En. de 2017
I used this:
secondlevelTIFF = imread('moon.tif');
% imshow(secondlevelTIFF);
imagesc(secondlevelTIFF) %read matrix into figure
axis on;
set(gca, 'Layer','top')
and I don't see any grid lines on top of the image.

What did I do wrong?
Star Strider
el 21 de En. de 2017
You didn’t toggle the grid.
figure(1)
secondlevelTIFF = imread('moon.tif');
% imshow(secondlevelTIFF);
imagesc(secondlevelTIFF) %read matrix into figure
grid
axis image
set(gca, 'GridColor','r', 'GridAlpha',1)
set(gca, 'Layer','top')
and add a few tweaks to make the grid lines more visible:

Hosup Song
el 26 de En. de 2017
Walter Roberson
el 26 de En. de 2017
Change the xtick to only the place you want the lunes
Star Strider
el 26 de En. de 2017
@Walter — Thank you.
I had to be away for a while. Life intrudes.
Image Analyst
el 26 de En. de 2017
Hosup, if you have R2016b you can run the new function xticks():
xticks([50,300]);
There is a similar function, yticks().
Categorías
Más información sobre Images en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!