RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
qr_detector.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright 2015 RAPP
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 ******************************************************************************/
17 
19 
24 {
25 }
26 
32 cv::Mat QrDetector::loadImage(std::string file_name)
33 {
34  cv::Mat img = cv::imread(file_name);
35  return img;
36 }
37 
43 std::vector<QrCode> QrDetector::detectQrs(const cv::Mat& input_frame)
44 {
45  cv::Mat gray_frame;
46 
47  unsigned int channels = input_frame.channels();
48  if( channels == 3 )
49  {
50  cv::cvtColor(input_frame, gray_frame, CV_BGR2GRAY);
51  }
52 
53  int gaussiansharpenblur = 5;
54  float gaussiansharpenweight = 0.8;
55 
56  cv::Mat blured;
57  normalize(gray_frame, gray_frame, 255, 0, cv::NORM_MINMAX);
58  cv::GaussianBlur(gray_frame, blured, cv::Size(0, 0), gaussiansharpenblur);
59  cv::addWeighted(gray_frame, 1 + gaussiansharpenweight, blured,
60  -gaussiansharpenweight, 0, gray_frame);
61 
62  int width = gray_frame.cols;
63  int height = gray_frame.rows;
64  uchar *raw = gray_frame.data;
65 
66  zbar::Image image(width, height, "Y800", raw, width * height);
67 
69  zbar::ImageScanner scanner;
70  scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 0);
71  scanner.set_config(zbar::ZBAR_QRCODE, zbar::ZBAR_CFG_ENABLE, 1);
72  scanner.scan(image);
73 
74  std::vector<QrCode> qrs;
75  for (zbar::Image::SymbolIterator symbol = image.symbol_begin();
76  symbol != image.symbol_end(); ++symbol)
77  {
78  //QrCode detected_code;
79  QrCode temp_qr;
80  temp_qr.message = symbol->get_data();
81 
82  for(int i = 0; i < symbol->get_location_size(); i++)
83  {
84  temp_qr.center.x += symbol->get_location_x(i);
85  temp_qr.center.y += symbol->get_location_y(i);
86  }
87 
88  temp_qr.center.x /= symbol->get_location_size();
89  temp_qr.center.y /= symbol->get_location_size();
90 
91  qrs.push_back(temp_qr);
92  }
93  return qrs;
94 }
95 
101 std::vector<QrCode> QrDetector::findQrs(std::string file_name)
102 {
103  cv::Mat input_frame;
104 
105  input_frame = loadImage(file_name);
106 
107  if( input_frame.empty() )
108  {
109  return std::vector<QrCode>();
110  }
111 
112  return detectQrs(input_frame);
113 }
cv::Mat loadImage(std::string file_name)
Loads an image into a cv::Mat structure.
Definition: qr_detector.cpp:32
cv::Point center
Definition: qr_detector.h:37
QrDetector(void)
Default constructor.
Definition: qr_detector.cpp:23
std::string message
Definition: qr_detector.h:38
std::vector< QrCode > findQrs(std::string file_name)
Detects QRs in an image file.
std::vector< QrCode > detectQrs(const cv::Mat &img)
Detects QRs in a cv::Mat.
Definition: qr_detector.cpp:43
Structure holding the essential info for a QR code.
Definition: qr_detector.h:35