JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
SuperPixel.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
19
20#include <opencv2/ximgproc/slic.hpp>
21#include <opencv2/ximgproc/seeds.hpp>
22
23void SuperPixel::process(cv::Mat const & inimg, cv::Mat & outimg)
24{
25 if (inimg.rows != outimg.rows || inimg.cols != outimg.cols || inimg.type() != CV_8UC3 || outimg.type() != CV_8UC1)
26 LFATAL("Need RGB byte input and gray byte output images of same dims");
27
28 // Process depending on the algo chosen by the user:
29 switch (algo::get())
30 {
31 case superpixel::Algo::SLIC:
32 case superpixel::Algo::SLICO:
33 {
34 auto sp = cv::ximgproc::createSuperpixelSLIC(inimg, algo::get() == superpixel::Algo::SLIC ?
35 cv::ximgproc::SLIC : cv::ximgproc::SLICO,
36 regionsize::get());
37
38 sp->iterate(iterations::get());
39
40 sp->enforceLabelConnectivity(25);
41
42 cv::Mat res; sp->getLabels(res);
43
44 // The labels are in CV_32SC1 format, so we convert them to byte:
45 switch (output::get())
46 {
47 case superpixel::OutType::Labels:
48 {
49 if (sp->getNumberOfSuperpixels() > 255) LERROR("More than 255 superpixels, graylevel confusion will occur");
50
51 int const * src = reinterpret_cast<int const *>(res.data);
52 unsigned char * dst = reinterpret_cast<unsigned char *>(outimg.data);
53 int const siz = inimg.rows * inimg.cols;
54
55 for (int i = 0; i < siz; ++i) *dst++ = *src++;
56 }
57 break;
58
59 case superpixel::OutType::Contours:
60 {
61 cv::cvtColor(inimg, outimg, cv::COLOR_RGB2GRAY);
62
63 cv::Mat mask; sp->getLabelContourMask(mask, false);
64
65 // in OpenCV 3.1 release, the mask is now created as 8SC1 image, with -1 on contours, and that
66 // does not work with setTo(), so let's set those contour pixels by hand:
67 char const * s = reinterpret_cast<char const *>(mask.data);
68 unsigned char * d = outimg.data; int sz = mask.total();
69 for (int i = 0; i < sz; ++i) { if (*s < 0) *d = 255; ++s; ++d; }
70 }
71 break;
72 }
73 }
74 break;
75
76 case superpixel::Algo::SEEDS:
77 {
78 auto sp = cv::ximgproc::createSuperpixelSEEDS(inimg.cols, inimg.rows, inimg.channels(), numpix::get(),
79 4, 2, 5, false);
80
81 sp->iterate(inimg, iterations::get());
82
83 cv::Mat res; sp->getLabels(res);
84
85 // The labels are in CV_32SC1 format, so we convert them to byte:
86 switch (output::get())
87 {
88 case superpixel::OutType::Labels:
89 {
90 if (sp->getNumberOfSuperpixels() > 255) LERROR("More than 255 superpixels, graylevel confusion will occur");
91
92 int const * src = reinterpret_cast<int const *>(res.data);
93 unsigned char * dst = reinterpret_cast<unsigned char *>(outimg.data);
94 int const siz = inimg.rows * inimg.cols;
95
96 for (int i = 0; i < siz; ++i) *dst++ = *src++;
97 }
98 break;
99
100 case superpixel::OutType::Contours:
101 {
102 cv::cvtColor(inimg, outimg, cv::COLOR_RGB2GRAY);
103
104 cv::Mat mask; sp->getLabelContourMask(mask, false);
105
106 // in OpenCV 3.1 release, the mask is now created as 8SC1 image, with -1 on contours, and that
107 // does not work with setTo(), so let's set those contour pixels by hand:
108 char const * s = reinterpret_cast<char const *>(mask.data);
109 unsigned char * d = outimg.data; int sz = mask.total();
110 for (int i = 0; i < sz; ++i) { if (*s < 0) *d = 255; ++s; ++d; }
111 }
112 break;
113 }
114 }
115 break;
116
117 default: LFATAL("oops");
118 }
119}
120
121
void process(cv::Mat const &inimg, cv::Mat &outimg)
Process an RGB image and get some greyscale results.
Definition SuperPixel.C:23
#define LFATAL(msg)
#define LERROR(msg)