Skip to content

Commit 58852ce

Browse files
committed
Latest version of the MATLAB function
the function img2data converts a .jpg, .png, etc. image to data and the residuals of this data when fit to a straight line.
1 parent 5347c59 commit 58852ce

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

matlab/img2data.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function [ residual, y ] = img2data (x)
2+
3+
gray = rgb2gray( x );
4+
BW = edge( gray );
5+
6+
[height, width] = size(BW);
7+
y = zeros(1, width);
8+
residual = zeros(1, width);
9+
10+
% for i = 1:width
11+
% if max(BW(:, i)) > 0
12+
% val = 0;
13+
% count = 0;
14+
% for j = 1: height
15+
% if BW(j,i) == 1
16+
% val = val + j;
17+
% count = count + 1;
18+
% end
19+
% end
20+
% BW(:,i) = zeros(height, 1);
21+
% val = round((val/count));
22+
% BW(val, i) = 1;
23+
% end
24+
% end
25+
26+
imshowpair(gray,BW,'falsecolor')
27+
28+
SE = strel('line', 4, 0);
29+
BWprime=imclose(BW, SE);
30+
31+
imshowpair(gray,BWprime,'falsecolor')
32+
33+
for i = 1:width
34+
for j = 0:height-1
35+
if BWprime(height-j,i) == 1
36+
y(1,i) = (j);
37+
end
38+
end
39+
end
40+
41+
t = 1:width;
42+
z = zeros(1, width);
43+
c = fit (t', y', 'poly1');
44+
45+
coeffs = coeffvalues(c);
46+
47+
slope = coeffs(1);
48+
intercept = coeffs(2);
49+
50+
for k = 1:width
51+
residual(1,k) = y(1,k) - ( slope * k + intercept );
52+
end
53+
54+
plot (t, residual, t, z)

0 commit comments

Comments
 (0)