-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathhatch.m
More file actions
63 lines (58 loc) · 1.6 KB
/
hatch.m
File metadata and controls
63 lines (58 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function H = hatch(im,levels,thickness)
% HATCH computes dithering hatched image with a number of levels of specified
% thickness
%
% H = hatch(im,levels)
%
% Inputs:
% im original image
% levels number of hatching levels {3}
% thickness thickness of individual hatch lines
% Outputs:
% H hatched image
%
% See also: dith
%
assert(levels>1);
% make sure image is grayscale
switch size(im,3)
case 1
% do nothing, already "grayscale"
case 3
warning('Converting input image to grayscale using rgb2gray');
im = rgb2gray(im);
otherwise
error('Input image should be grayscale');
end
% make sure image is double
if ~isfloat(im)
warning([ ...
'Converting image to double, using im2double, ' ...
'output will also be double']);
im = im2double(im);
end
H = zeros(size(im));
for i = 0:(levels-1)
% compute ratio of black to white for this level
ratio = i/(levels-1);
lo = i/levels;
% compile pattern with this ratio of black to white
pattern = zeros(1,levels-1)';
for j = 1:(levels-1)
pattern(j) = sum(pattern(1:j))/j < ratio;
end
pattern = repmat(pattern,1,thickness)';
pattern = pattern(:);
scale = ceil(size(im)./size(pattern));
pattern_mask = repmat(pattern,scale(1),scale(2));
pattern_mask = pattern_mask(1:size(im,1),1:size(im,2));
%if(i == round((levels-1)/2))
% imshow(pattern_mask);
%end
H( im > lo ) = pattern_mask(im > lo);
end
% get rid of small lines
% b = 1-bwareaopen(1-bwareaopen(h,10),10);
% get rid of speckles in lines
% m = medfilt2(b,[1,5]);
end