Modify line style with insertShape
Show older comments
Hi all,
I wrote a function to make a grid on an image. I can either draw the grid on top of the image or burn/insert it into the image, depending on user input. I can also modify line width and style- but only for the 'draw' method, because I can't seem to modify line style using insertShape. Does anyone know how to do this? I'll post the code. There is an example at the top for draw method, and for the burn method. Both should run and use default images. Thanks.
Clay
function [varargout] = imageGrid(I, varargin)
% imcirclecrop puts a grid on an image
% Example 1:
% I = imread('rice.png');
% vertLines = 10;
% horzLines = 10;
% lineWidth = 2;
% imshow(I);
% Igrid = imageGrid(I, 'vertLines', vertLines, 'horzLines', horzLines, 'lineWidth', lineWidth, 'lineStyle', ':', 'technique', 'Draw');
% Example 2:
% I = imread('cameraman.tif');
% vertLines = 15;
% horzLines = 10;
% lineWidth = 1;
% imshow(I);
% Igrid = imageGrid(I, 'vertLines', vertLines, 'horzLines', horzLines, 'lineWidth', lineWidth, 'lineStyle', ':', 'technique', 'burn');
% imshowpair(I, Igrid, 'montage')
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Set defaults
defaultVertLines = 10;
defaultHorzLines = 10;
defaultLineWidth = 1;
defaultLineStyle = ':';
defaultTechnique = 'Draw';
p = inputParser;
p.CaseSensitive = false;
p.KeepUnmatched = true; %store extra inputs instead of throwing error
addRequired(p, 'I');
addOptional(p, 'vertLines', defaultVertLines);
addOptional(p, 'horzLines', defaultHorzLines);
addOptional(p, 'lineWidth', defaultLineWidth);
addOptional(p, 'lineStyle', defaultLineStyle);
addOptional(p, 'technique', defaultTechnique);
%Parse inputs
parse(p, I, varargin{:});
vertLines = p.Results.vertLines;
horzLines = p.Results.horzLines;
lineWidth = p.Results.lineWidth;
technique = p.Results.technique;
lineStyle = p.Results.lineStyle;
drawCases = {'Draw'};
drawLogical = strcmpi(technique, drawCases); %True if 'technique' is a variation of 'draw'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
horzTicks = round(linspace(1, size(I,1), horzLines),0); %cols where we will draw vertical lines
vertTicks = round(linspace(1, size(I,2), vertLines),0); %rows where we will draw horizontal lines
maxY = size(I,1); %num pixels in Y
maxX = size(I,2); %num pixels in X
if drawLogical %draw on the current image
for i = 1:1:vertLines
line([vertTicks(i), vertTicks(i)], [maxY, 0], 'LineWidth', lineWidth, 'Color', 'k', 'LineStyle', lineStyle); %draw vertical lines
end
for i = 1:1:horzLines
line([0, maxX], [horzTicks(i), horzTicks(i)], 'LineWidth', lineWidth, 'Color', 'k', 'LineStyle', lineStyle); %draw horizontal lines
end
varargout{1} = 'Done';
else %burn/insert the lines into the image
%Put together line coordinates the way that ShapeInserter wants
% [x1_line1, y1_line1, x2_line1, y2_line1;...
% x1_line2, y1_line2, x2_line2, y2_line2]
Vlines = zeros(length(vertTicks),4);
Vlines(:,1) = vertTicks;
Vlines(:,2) = 0;
Vlines(:,3) = vertTicks;
Vlines(:,4) = maxY;
Hlines = zeros(length(horzTicks), 4);
Hlines(:,1) = 0;
Hlines(:,2) = horzTicks;
Hlines(:,3) = maxX;
Hlines(:,4) = horzTicks;
%Put the two together
lines = vertcat(Vlines, Hlines);
lines = int32(lines);
%Insert them and output the modified image
Igrid = insertShape(I, 'Line', lines, 'LineWidth', lineWidth, 'Color', 'black');
varargout{1} = Igrid;
end
end %end imageDrawGrid()
Accepted Answer
More Answers (0)
Categories
Find more on Ground Truth Labeling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!