datamatrix_detection_gray_node.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2014, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
27 
28 #include <ros/ros.h>
29 #include <image_transport/image_transport.h>
30 #include <cv_bridge/cv_bridge.h>
31 #include <sensor_msgs/image_encodings.h>
32 #include <opencv2/imgproc/imgproc.hpp>
33 #include <opencv2/highgui/highgui.hpp>
34 
35 #include <dmtx.h>
36 
37 // static const std::string OPENCV_WINDOW = "Image window";
38 
40 {
41  ros::NodeHandle nh_;
42  image_transport::ImageTransport it_;
43  image_transport::Subscriber image_sub_;
44 // image_transport::Publisher image_pub_;
45  std::vector<std::string> leituras;
46 
47 public:
49  : it_(nh_)
50  {
51  // Subscrive to input video feed and publish output video feed
52  image_sub_ = it_.subscribe("/usb_cam/image_raw", 1,
54 // image_pub_ = it_.advertise("/image_converter/output_video", 1);
55 
56  }
57 
59  {
60 // cv::destroyWindow(OPENCV_WINDOW);
61  uint total = leituras.size();
62  std::cout << std::endl;
63  std::cout << std::endl;
64  std::cout << "Foram lidas: " << total << " DataMatrix" << std::endl;
65  int error = 0;
66  uint j;
67  for ( j=0; j < leituras.size(); j++)
68  {
69  if (leituras[j] != "111222") error++;
70 // std::cout << "leitura " << leituras[j] << std::endl;
71  }
72 
73  std::cout << "Das quais: " << error << " foram lidas erradamente" << std::endl;
74  std::cout << "Success rate: " << ((total - error)/static_cast<double>(total))*100. << " %" << std::endl;
75  std::cout << std::endl;
76  }
77 
78  void imageCallback(const sensor_msgs::ImageConstPtr& msg)
79  {
80  cv_bridge::CvImagePtr cv_ptr;
81  try
82  {
83  cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
84  }
85  catch (cv_bridge::Exception& e)
86  {
87  ROS_ERROR("cv_bridge exception: %s", e.what());
88  return;
89  }
90  DatamatrixDecode(cv_ptr->image);
91 
92  // Draw an example circle on the video stream
93 // if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
94 // cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));
95 
96  // Update GUI Window
97  cv::imshow("Imagem", cv_ptr->image);
98  cv::waitKey(3);
99 
100  // Output modified video stream
101 // image_pub_.publish(cv_ptr->toImageMsg());
102  }
103  void DatamatrixDecode(cv::Mat& cv_img)
104  {
105  static int i = 1;
106  DmtxImage *img;
107  DmtxDecode *dec;
108  DmtxRegion *reg;
109  DmtxMessage *msg;
110 
111  cv::Mat gray_img;
112  cvtColor(cv_img, gray_img, CV_BGR2GRAY, 0);
113 
114 
115 // É mais rápido ler em grayscale com DmtxPack8bppK
116 // Porém é pior na leitura à distância
117  img = dmtxImageCreate(gray_img.data, gray_img.cols, gray_img.rows, DmtxPack8bppK);
118  assert(img != NULL);
119 
120  dec = dmtxDecodeCreate(img, 1);
121  assert(dec != NULL);
122 
123  // For single matrix reading =========================================================
124  DmtxTime timeout = dmtxTimeAdd(dmtxTimeNow(),50);
125  reg = dmtxRegionFindNext(dec, &timeout);
126  if(reg != NULL) {
127  msg = dmtxDecodeMatrixRegion(dec, reg, DmtxUndefined);
128  if(msg != NULL) {
129 
130 // fputs("output: \"", stdout);
131 
132 // fwrite(msg->output, sizeof(unsigned char), msg->outputIdx, stdout);
133  std::cout << "output " << i++ << ": " << msg->output << std::endl;
134  leituras.push_back(std::string((char*)(msg->output)));
135 // cout << "Código completo: " << msg->codeSize << endl;
136 // fputs("\"\n", stdout);
137  dmtxMessageDestroy(&msg);
138  }
139 
140  int height = cv_img.rows-1;
141  cv::circle(cv_img, cv::Point (reg->leftLoc.X, height - reg->leftLoc.Y), 10, CV_RGB(255,0,0));
142  cv::circle(cv_img, cv::Point (reg->rightLoc.X, height - reg->rightLoc.Y), 10, CV_RGB(255,0,0));
143  cv::circle(cv_img, cv::Point (reg->topLoc.X, height - reg->topLoc.Y), 10, CV_RGB(255,0,0));
144  cv::circle(cv_img, cv::Point (reg->bottomLoc.X, height - reg->bottomLoc.Y), 10, CV_RGB(255,0,0));
145 
146  dmtxRegionDestroy(&reg);
147  }
148 
149 // cv::imshow("Imagem", gray_img);
150 // cv::waitKey(3);
151 
152  dmtxDecodeDestroy(&dec);
153  dmtxImageDestroy(&img);
154  }
155 
156 };
157 
158 int main(int argc, char** argv)
159 {
160  ros::init(argc, argv, "read_gray_datamatrix");
161  ImageConverter ic;
162  ros::spin();
163  return 0;
164 }
void DatamatrixDecode(cv::Mat &cv_img)
int main(int argc, char **argv)
std::vector< std::string > leituras
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
image_transport::ImageTransport it_
image_transport::Subscriber image_sub_


datamatrix_detection
Author(s): Luís Pedras Carrão
autogenerated on Mon Mar 2 2015 01:31:36