Skip to content

Commit 07215bb

Browse files
committed
add files to repo
1 parent 1cd0ff3 commit 07215bb

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed

examples/morphdemo.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
'''%MORPHDEMO Demonstrate morphology using animation
2+
%
3+
% MORPHDEMO(IM, SE, OPTIONS) displays an animation to show the principles
4+
% of the mathematical morphology operations dilation or erosion. Two
5+
% windows are displayed side by side, input binary image on the left and
6+
% output image on the right. The structuring element moves over the input
7+
% image and is colored red if the result is zero, else blue. Pixels in
8+
% the output image are initially all grey but change to black or white
9+
% as the structuring element moves.
10+
%
11+
% OUT = MORPHDEMO(IM, SE, OPTIONS) as above but returns the output image.
12+
%
13+
% Options::
14+
% 'dilate' Perform morphological dilation
15+
% 'erode' Perform morphological erosion
16+
% 'delay' Time between animation frames (default 0.5s)
17+
% 'scale',S Scale factor for output image (default 64)
18+
% 'movie',M Write image frames to the folder M
19+
%
20+
% Notes::
21+
% - This is meant for small images, say 10x10 pixels.
22+
%
23+
% See also IMORPH, IDILATE, IERODE.
24+
'''
25+
26+
def morphdemo(input, se, op='erode', delay=0.1, scale=64):
27+
28+
print("hello from morphdemo")
29+
30+
# # movie option
31+
32+
# input = idouble(input>0);
33+
34+
# clf
35+
36+
# %se = [0 1 0; 1 1 1; 0 1 0];
37+
38+
# white = [1 1 1] * 0.5;
39+
# red = [1 0 0] * 0.8;
40+
# blue = [0 0 1] * 0.8;
41+
42+
# result = ones(size(input)) * 0.5;
43+
44+
# subplot(121);
45+
# h1 = gca;
46+
# im = icolor(input);
47+
# h1 = image(im);
48+
# set(h1, 'CDataMapping', 'direct');
49+
# axis equal
50+
51+
# subplot(122);
52+
# h2 = image(result);
53+
# colormap(gray);
54+
# set(h2, 'CDataMapping', 'scaled');
55+
# set(gca, 'CLim', [0 1]);
56+
# set(gca, 'CLimMode', 'manual');
57+
# axis equal
58+
59+
# nr_se = (numrows(se)-1)/2;
60+
# nc_se = (numcols(se)-1)/2;
61+
62+
# for r=nr_se+1:numrows(input)-nr_se
63+
# for c=nc_se+1:numcols(input)-nc_se
64+
# im = icolor(input*0.7);
65+
66+
# win = input(r-nr_se:r+nr_se, c-nc_se:c+nc_se);
67+
68+
# rr = win .* se;
69+
# switch opt.op
70+
# case {'erode', 'min'}
71+
# if all(rr(find(se)))
72+
# color = blue;
73+
# result(r,c) = 1;
74+
# else
75+
# color = red;
76+
# result(r,c) = 0;
77+
# end
78+
# case {'dilate', 'max'}
79+
# if any(rr(find(se)))
80+
# color = blue;
81+
# result(r,c) = 1;
82+
# else
83+
# color = red;
84+
# result(r,c) = 0;
85+
86+
87+
# for i=-nr_se:nr_se
88+
# for j=-nc_se:nc_se
89+
# if se(i+nr_se+1,j+nc_se+1) > 0
90+
# im(r+i,c+j,:) = min(1, im(r+i,c+j,:) + reshape(color, [1 1 3]));
91+
92+
93+
# set(h1, 'CData', im);
94+
95+
# set(h2, 'CData', result);
96+
97+
# if isempty(opt.movie)
98+
# pause(opt.delay);
99+
# else
100+
# frame1 = ireplicate(im, opt.scale);
101+
# frame2 = ireplicate(icolor(result), opt.scale);
102+
# frame = cat(2, frame1, frame2);
103+
# imwrite(frame, sprintf('%s/%04d.png', opt.movie, framenum));
104+
# framenum = framenum+1;
105+
106+
107+
# if nargout > 0
108+
# out = result > 0.6;
109+
# end

examples/ransac_line.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import numpy as np
2+
3+
def ransac_line(x, y, npoints=2, maxiter=20, t=1e-6, d=4):
4+
5+
iterations = 0
6+
n = len(x)
7+
best_fit = np.inf
8+
best_model = None
9+
10+
while True:
11+
inliers = list(np.random.randint(0, len(x), npoints))
12+
13+
m, c, r2, *_ = sp.stats.linregress(x[inliers], y[inliers])
14+
new_inliers = []
15+
for k in range(len(x)):
16+
if k in inliers:
17+
continue
18+
if np.linalg.norm(m * x[k] + c - y[k]) < t:
19+
new_inliers.append(k)
20+
21+
inliers.extend(new_inliers)
22+
if len(inliers) >= d:
23+
# decent model
24+
m, c, r2, *_ = sp.stats.linregress(x[inliers], y[inliers])
25+
err = np.linalg.norm(m * x[inliers] + c - y[inliers])
26+
if err < best_fit:
27+
best_fit = err
28+
best_model = (m, c)
29+
best_inliers = inliers
30+
31+
iterations += 1
32+
if iterations > maxiter:
33+
break
34+
35+
return best_model, best_inliers
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
3+
from bdsim.blocks.linalg import *
4+
5+
import unittest
6+
import numpy.testing as nt
7+
8+
from machinevisiontoolbox import CentralCamera, mkcube
9+
from spatialmath import SE3
10+
11+
from machinevisiontoolbox.blocks import *
12+
13+
14+
class CameraBlockTest(unittest.TestCase):
15+
def test_camera(self):
16+
17+
cam = CentralCamera.Default()
18+
block = Camera(cam)
19+
20+
P = np.array([1, 2, 5])
21+
T = SE3()
22+
p = cam.project_point(P, pose=T)
23+
24+
nt.assert_array_almost_equal(block._output(P, T)[0], p)
25+
26+
T = SE3.Trans(0.2, 0.3, 0.4)
27+
p = cam.project_point(P, pose=T)
28+
29+
nt.assert_array_almost_equal(block._output(P, T)[0], p)
30+
31+
def test_visjac(self):
32+
33+
cam = CentralCamera.Default()
34+
block = Visjac_p(cam, 5)
35+
36+
P = np.array([1, 2, 5])
37+
p = cam.project_point(P)
38+
39+
J = cam.visjac_p(p, 5)
40+
nt.assert_array_almost_equal(block._output(p)[0], J)
41+
42+
def test_estpose(self):
43+
44+
cam = CentralCamera.Default()
45+
46+
P = mkcube(0.2)
47+
T_unknown = SE3.Trans(0.1, 0.2, 1.5) * SE3.RPY(0.1, 0.2, 0.3)
48+
p = cam.project_point(P, objpose=T_unknown)
49+
50+
T_est = cam.estpose(P, p)
51+
52+
block = EstPose_p(cam, P)
53+
54+
nt.assert_array_almost_equal(block._output(p)[0], T_est)
55+
56+
def test_imageplane(self):
57+
58+
cam = CentralCamera.Default()
59+
block = ImagePlane(cam)
60+
61+
# block._start()
62+
# block._step()
63+
64+
65+
# ---------------------------------------------------------------------------------------#
66+
if __name__ == "__main__":
67+
68+
unittest.main()

0 commit comments

Comments
 (0)