File: random_sample_consensus.py

package info (click to toggle)
python-pcl 0.3.0~rc1%2Bdfsg-14
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 31,828 kB
  • sloc: python: 3,094; cpp: 283; makefile: 181; sh: 24; ansic: 12
file content (199 lines) | stat: -rw-r--r-- 7,960 bytes parent folder | download | duplicates (3)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# -*- coding: utf-8 -*-
# How to use Random Sample Consensus model
# http://pointclouds.org/documentation/tutorials/random_sample_consensus.php#random-sample-consensus

import numpy as np
import pcl
import random
import pcl.pcl_visualization
import math
import sys # W[ argv 擾邽


# boost::shared_ptr<pcl::visualization::PCLVisualizer> simpleVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
# {
#   // -----Open 3D viewer and add point cloud-----
#   boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
#   viewer->setBackgroundColor (0, 0, 0);
#   viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");
#   viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
#   //viewer->addCoordinateSystem (1.0, "global");
#   viewer->initCameraParameters ();
#   return (viewer);
# }

argvs = sys.argv  # R}hCi[Xg̎擾
argc = len(argvs) # ̌

# // initialize PointClouds
# pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
# pcl::PointCloud<pcl::PointXYZ>::Ptr final (new pcl::PointCloud<pcl::PointXYZ>);
cloud = pcl.PointCloud()
final = pcl.PointCloud()

# // populate our PointCloud with points
# cloud->width    = 500;
# cloud->height   = 1;
# cloud->is_dense = false;
# cloud->points.resize (cloud->width * cloud->height);
# for (size_t i = 0; i < cloud->points.size (); ++i)
# {
#   if (pcl::console::find_argument (argc, argv, "-s") >= 0 || pcl::console::find_argument (argc, argv, "-sf") >= 0)
#   {
#     cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0);
#     cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0);
#     if (i % 5 == 0)
#       cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0);
#     else if(i % 2 == 0)
#       cloud->points[i].z =  sqrt( 1 - (cloud->points[i].x * cloud->points[i].x)
#                                     - (cloud->points[i].y * cloud->points[i].y));
#     else
#       cloud->points[i].z =  - sqrt( 1 - (cloud->points[i].x * cloud->points[i].x)
#                                       - (cloud->points[i].y * cloud->points[i].y));
#   }
#   else
#   {
#     cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0);
#     cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0);
#     if( i % 2 == 0)
#       cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0);
#     else
#       cloud->points[i].z = -1 * (cloud->points[i].x + cloud->points[i].y);
#   }
# }
points = np.zeros((1000, 3), dtype=np.float32)
RAND_MAX = 1024

for i in range(0, 1000):
    if argc > 1:
        if argvs[1] == "-s" or argvs[1] == "-sf":
            points[i][0] = 1024 * random.random () / (RAND_MAX + 1.0)
            points[i][1] = 1024 * random.random () / (RAND_MAX + 1.0)
            # print("x : " + str(points[i][0]))
            # print("y : " + str(points[i][1]))
            if i % 5 == 0:
                points[i][2] = 1024 * random.random () / (RAND_MAX + 1.0)
            elif i % 2 == 0:
                points[i][2] = math.sqrt(math.fabs(1 - (points[i][0] * points[i][0]) - (points[i][1] * points[i][1])))
                # points[i][2] = math.sqrt(1 )
            else:
                points[i][2] = -1 * math.sqrt(math.fabs(1 - (points[i][0] * points[i][0]) - (points[i][1] * points[i][1])))
                # points[i][2] = -1 * math.sqrt( 1 )
        else:
            points[i][0] = 1024 * random.random () / RAND_MAX
            points[i][1] = 1024 * random.random () / RAND_MAX
            if i % 2 == 0:
                points[i][2] = 1024 * random.random () / RAND_MAX
            else:
                points[i][2] = -1 * (points[i][0] + points[i][1])
    else:
        points[i][0] = 1024 * random.random () / RAND_MAX
        points[i][1] = 1024 * random.random () / RAND_MAX
        if i % 2 == 0:
            points[i][2] = 1024 * random.random () / RAND_MAX
        else:
            points[i][2] = -1 * (points[i][0] + points[i][1])

cloud.from_array(points)

# std::vector<int> inliers;
# // created RandomSampleConsensus object and compute the appropriated model
# pcl::SampleConsensusModelSphere<pcl::PointXYZ>::Ptr model_s(new pcl::SampleConsensusModelSphere<pcl::PointXYZ> (cloud));
# pcl::SampleConsensusModelPlane<pcl::PointXYZ>::Ptr model_p (new pcl::SampleConsensusModelPlane<pcl::PointXYZ> (cloud));
# if(pcl::console::find_argument (argc, argv, "-f") >= 0)
# {
# pcl::RandomSampleConsensus<pcl::PointXYZ> ransac (model_p);
# ransac.setDistanceThreshold (.01);
# ransac.computeModel();
# ransac.getInliers(inliers);
# }
# else if (pcl::console::find_argument (argc, argv, "-sf") >= 0 )
# {
# pcl::RandomSampleConsensus<pcl::PointXYZ> ransac (model_s);
# ransac.setDistanceThreshold (.01);
# ransac.computeModel();
# ransac.getInliers(inliers);
# }
###
# inliers = vector[int]
model_s = pcl.SampleConsensusModelSphere(cloud)
model_p = pcl.SampleConsensusModelPlane(cloud)
if argc > 1:
    if argvs[1] == "-f":
        ransac = pcl.RandomSampleConsensus (model_p)
        ransac.set_DistanceThreshold (.01)
        ransac.computeModel()
        inliers = ransac.get_Inliers()
    elif argvs[1] == "-sf":
        ransac = pcl.RandomSampleConsensus (model_s)
        ransac.set_DistanceThreshold (.01)
        ransac.computeModel()
        inliers = ransac.get_Inliers()
    else:
        inliers = []
else:
    inliers = []


# // copies all inliers of the model computed to another PointCloud
# pcl::copyPointCloud<pcl::PointXYZ>(*cloud, inliers, *final);
# final = pcl.copyPointCloud(cloud, inliers)
# pcl.copyPointCloud(cloud, inliers, final)
# final = cloud
print(str(len(inliers)))
if len(inliers) != 0:
    finalpoints = np.zeros((len(inliers), 3), dtype=np.float32)
    
    for i in range(0, len(inliers)):
        finalpoints[i][0] = cloud[inliers[i]][0]
        finalpoints[i][1] = cloud[inliers[i]][1]
        finalpoints[i][2] = cloud[inliers[i]][2]

    final.from_array(finalpoints)


# current(0.3.0) Windows Only Test
isWindows = False
if isWindows == True:
    # creates the visualization object and adds either our orignial cloud or all of the inliers
    # depending on the command line arguments specified.
    # boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
    # if (pcl::console::find_argument (argc, argv, "-f") >= 0 || pcl::console::find_argument (argc, argv, "-sf") >= 0)
    #   viewer = simpleVis(final);
    # else
    #   viewer = simpleVis(cloud);

    if argc > 1:
        if argvs[1] == "-f" or argvs[1] == "-sf":
            viewer = pcl.pcl_visualization.PCLVisualizering('3D Viewer')
            viewer.SetBackgroundColor (0, 0, 0)
            viewer.AddPointCloud (final, b'sample cloud')
            viewer.SetPointCloudRenderingProperties (pcl.pcl_visualization.PCLVISUALIZER_POINT_SIZE, 3, b'sample cloud')
            viewer.InitCameraParameters ()
        else:
            viewer = pcl.pcl_visualization.PCLVisualizering('3D Viewer')
            viewer.SetBackgroundColor (0, 0, 0)
            viewer.AddPointCloud (cloud, b'sample cloud')
            viewer.SetPointCloudRenderingProperties (pcl.pcl_visualization.PCLVISUALIZER_POINT_SIZE, 3, b'sample cloud')
            viewer.InitCameraParameters ()
    else:
        viewer = pcl.pcl_visualization.PCLVisualizering('3D Viewer')
        viewer.SetBackgroundColor (0, 0, 0)
        viewer.AddPointCloud (cloud, b'sample cloud')
        viewer.SetPointCloudRenderingProperties (pcl.pcl_visualization.PCLVISUALIZER_POINT_SIZE, 3, b'sample cloud')
        viewer.InitCameraParameters ()


    # while (!viewer->wasStopped ())
    # {
    # viewer->spinOnce (100);
    # boost::this_thread::sleep (boost::posix_time::microseconds (100000));
    # }
    isStopped = False
    while isStopped == False:
        isStopped = viewer.WasStopped()
        viewer.SpinOnce (100)
        # boost::this_thread::sleep (boost::posix_time::microseconds (100000));
else:
    pass