MATLAB Answers

Burn Grid onto a Series of Images

39 views (last 30 days)
Hi all-I have a lot of image files and onto each I am trying to place a grid. I'd like to save the grid onto the file, and so produce a new image with the grid in a separate folder. In the end I will have one folder with the original files and a second with the files+grid.
Just now I have this:
for i = 1:fileCount2
png=d2(i).name;
image = imread(png);
imshow(image);
axis on;
[rows, columns, numberOfColorChannels] = size(image);
hold on;
stepSize = 20; % Whatever you want.
for row = 1 : stepSize : rows
line([1, columns], [row, row], 'Color', 'r', 'LineWidth', 1);
end
for col = 1 : stepSize : columns
line([col, col], [1, rows], 'Color', 'r', 'LineWidth', 1);
end
% for x=1:length(fileCount2)
% outputBaseFileName = sprintf('-%4.4d.png', x); %output filename, each frame numbered from 0001
outputFullFileName = fullfile(strcat(outputFolder, '\FramesGrids'), strcat(baseFileName, outputBaseFileName)); %full output filename with .avi file number
imwrite(image, outputFullFileName, 'png'); %write output png file
end
This successfully adds a grid onto the first photo in the folder, and saves a new photo in a separate folder, but the grid is not saved. The grid only shows up when I run the code and produce the figure within Matlab.
So, any advice on how to save the grid? And how to fix my loop so that every photo has a grid added, not just the first?
Thanks in advance!!

  4 Comments

Show 1 older comment
Louise Wilson
Louise Wilson on 11 Jul 2019
Hi Walter, thank you. This looks useful but to use it I need to know the points in the photo where the gridlines go and I don't know how I could get this to control the size of the grid lines so that a square grid is produced?
Walter Roberson
Walter Roberson on 12 Jul 2019
There is a linewidth option for insertshape
You know, it might be easiest to just
%red
YourArray(1:StepSize:Columns, 1:StepSize:Rows, 1) = 255;
YourArray(1:StepSize:Columns, 1:StepSize:Rows, [2 3]) = 0;
if your line width is 1.
Louise Wilson
Louise Wilson on 12 Jul 2019
Thanks! This doesn't give me lines though, it gives me a dotted grid? Kind of looks like the skeleton of a grid. How do I join the dots?
I don't mean linewidth, I mean the distance between the lines, and where exactly the lines go? How do I specify this accurately so the spacing is consistent.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 12 Jul 2019
Simply burn it into the image itself:
grayImage = imread('cameraman.tif');
[rows, columns, numberOfColorchannels] = size(grayImage);
% Make grid every 64 rows
for row = 8 : 64 : rows
grayImage(row, :, :) = 255;
end
% Make grid every 32 columns
for col = 8 : 32 : rows
grayImage(:, col, :) = 255;
end
imshow(grayImage);
0000 Screenshot.png

  5 Comments

Show 2 older comments
Louise Wilson
Louise Wilson on 12 Jul 2019
Thanks, I think the error is basically that I need to extract the information from the file before I then read it in. If I switch the commands around in the 2 and 3 lines it works.
Louise Wilson
Louise Wilson on 12 Jul 2019
Thanks so much for your help!! I finished this code and everything is working great, thanks very much to you.
Image Analyst
Image Analyst on 13 Jul 2019
No, swapping positions wouldn't do it because then the badly-named image would not have been defined yet by the time you call fileparts().
[folder, baseFileName, extentions] = fileparts(image); % Will throw error because image is not defined yet.
image = imread('02052019-0006.png');
But whatever, glad you got it working and thanks for accepting.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!