Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpDetectorDataMatrixCode.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Base class for bar code detection.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <assert.h>
40 
41 #include <visp3/core/vpConfig.h>
42 
43 #ifdef VISP_HAVE_DMTX
44 
45 #include <dmtx.h>
46 
47 #include <visp3/detection/vpDetectorDataMatrixCode.h>
48 
53 
61 {
62  bool detected = false;
63  m_message.clear();
64  m_polygon.clear();
65  m_nb_objects = 0;
66  DmtxRegion *reg;
67  DmtxDecode *dec;
68  DmtxImage *img;
69  DmtxMessage *msg;
70 
71  img = dmtxImageCreate(I.bitmap, (int)I.getWidth(), (int)I.getHeight(), DmtxPack8bppK);
72  assert(img != NULL);
73 
74  dec = dmtxDecodeCreate(img, 1);
75  assert(dec != NULL);
76 
77  bool end = false;
78  do {
79  reg = dmtxRegionFindNext(dec, 0);
80 
81  if (reg != NULL) {
82  msg = dmtxDecodeMatrixRegion(dec, reg, DmtxUndefined);
83  if (msg != NULL) {
84 
85  std::vector<vpImagePoint> polygon;
86 
87  DmtxVector2 p00, p10, p11, p01;
88 
89  p00.X = p00.Y = p10.Y = p01.X = 0.0;
90  p10.X = p01.Y = p11.X = p11.Y = 1.0;
91  dmtxMatrix3VMultiplyBy(&p00, reg->fit2raw);
92  dmtxMatrix3VMultiplyBy(&p10, reg->fit2raw);
93  dmtxMatrix3VMultiplyBy(&p11, reg->fit2raw);
94  dmtxMatrix3VMultiplyBy(&p01, reg->fit2raw);
95 
96  polygon.push_back(vpImagePoint(I.getHeight() - p00.Y, p00.X));
97  polygon.push_back(vpImagePoint(I.getHeight() - p10.Y, p10.X));
98  polygon.push_back(vpImagePoint(I.getHeight() - p11.Y, p11.X));
99  polygon.push_back(vpImagePoint(I.getHeight() - p01.Y, p01.X));
100 
101  m_polygon.push_back(polygon);
102  detected = true;
103  m_message.push_back((const char *)msg->output);
104 
105  m_nb_objects++;
106  } else {
107  end = true;
108  }
109  dmtxMessageDestroy(&msg);
110  } else {
111  end = true;
112  }
113  dmtxRegionDestroy(&reg);
114 
115  } while (!end);
116 
117  dmtxDecodeDestroy(&dec);
118  dmtxImageDestroy(&img);
119  return detected;
120 }
121 
122 #elif !defined(VISP_BUILD_SHARED_LIBS)
123 // Work arround to avoid warning:
124 // libvisp_core.a(vpDetectorDataMatrixCode.cpp.o) has no symbols
125 void dummy_vpDetectorDataMatrixCode(){};
126 #endif
unsigned int getWidth() const
Definition: vpImage.h:239
Type * bitmap
points toward the bitmap
Definition: vpImage.h:133
size_t m_nb_objects
Number of detected objects.
bool detect(const vpImage< unsigned char > &I)
std::vector< std::vector< vpImagePoint > > m_polygon
unsigned int getHeight() const
Definition: vpImage.h:178
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
std::vector< std::string > m_message
Message attached to each object.