Visual Servoing Platform  version 3.6.1 under development (2024-04-28)
ViewController.mm
1 #import "ViewController.h"
2 #import "ImageConversion.h"
3 #import "ImageDisplay.h"
4 #ifdef __cplusplus
5 #import <visp3/visp.h>
6 #endif
7 
8 #ifndef DOXYGEN_SHOULD_SKIP_THIS
9 @interface ViewController ()
10 @end
11 
12 @implementation ViewController
13 
14 @synthesize myImageView;
15 #endif
16 
17 
18 - (void)viewDidLoad {
19 
20  [super viewDidLoad];
21 
22  // Load an image
23  UIImage *img = [UIImage imageNamed:@"AprilTag.png"];
24 
25  // Image view instance to display the image
26  self.myImageView = [[UIImageView alloc] initWithImage:img];
27 
28  // Set the frame for the image view
29  CGRect myFrame = CGRectMake(0.0f, 0.0f, self.myImageView.frame.size.width, self.myImageView.frame.size.height);
30  [self.myImageView setFrame:myFrame];
31 
32  // Add the image view to the current view
33  [self.view addSubview:self.myImageView];
34 
35  // View image
36  [myImageView setImage:img];
37 
38  // Convert image to visp
39  vpImage<unsigned char> I = [ImageConversion vpImageGrayFromUIImage:img];
40 
41  // Detect AprilTag
44  double tagSize = 0.053;
45  float quad_decimate = 3.0;
46  int nThreads = 1;
47  std::vector<vpHomogeneousMatrix> cMo_vec;
48 
49  // Set camera parameters
51  cam.initPersProjWithoutDistortion(615.1674805, 615.1675415, 312.1889954, 243.4373779);
52 
53  // Initialize apriltag detector
54  vpDetectorAprilTag detector(tagFamily);
55  detector.setAprilTagQuadDecimate(quad_decimate);
56  detector.setAprilTagPoseEstimationMethod(poseEstimationMethod);
57  detector.setAprilTagNbThreads(nThreads);
58 
59  // Detect all the tags in the image
60  detector.detect(I, tagSize, cam, cMo_vec);
61 
62  // Parse detected tags for printings
63  std::cout << "Number of tags in the image: " << detector.getNbObjects() << std::endl;
64  for(size_t i=0; i < detector.getNbObjects(); i++) {
65  std::cout << "- Detected tag: " << detector.getMessage(i) << std::endl;
66  std::cout << " pose: " << cMo_vec[i] << std::endl;
67  }
68 
69  // Draw red lines around each tag
70  for (size_t i = 0; i < detector.getNbObjects() ; i++) {
71  std::vector<vpImagePoint> polygon = detector.getPolygon(i);
72  for (size_t j = 0; j < polygon.size(); j++) {
73  img = [ImageDisplay displayLine:img :polygon[j] :polygon[(j+1)%polygon.size()] :[UIColor redColor] :2];
74  }
75  }
76 
77  // Draw a frame corresponding to the tag 3D location
78  for(size_t i=0; i < detector.getNbObjects(); i++) {
79  img = [ImageDisplay displayFrame:img :cMo_vec[i] :cam :0.025 :2];
80  }
81  [myImageView setImage:img];
82 }
83 
84 - (void)didReceiveMemoryWarning {
85  [super didReceiveMemoryWarning];
86  // Dispose of any resources that can be recreated.
87 }
88 
89 @end
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
@ TAG_36h11
AprilTag 36h11 pattern (recommended)