Skip to content

Commit 9ed5fbd

Browse files
authored
Merge pull request #2387 from alalek:fix_ximgproc_slic_empty_mat
* ximgproc(test): createSuperpixelSLIC smoke test * ximgproc: don't use 'operator+()' with empty cv::Mat
1 parent 1109906 commit 9ed5fbd

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

modules/ximgproc/src/slic.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,14 @@ void SuperpixelSLICImpl::initialize()
247247
// intitialize label storage
248248
m_klabels = Mat( m_height, m_width, CV_32S, Scalar::all(0) );
249249

250-
// storage for edge magnitudes
251-
Mat edgemag = Mat( m_height, m_width, CV_32F, Scalar::all(0) );
252-
253250
// perturb seeds is not absolutely necessary,
254251
// one can set this flag to false
255252
bool perturbseeds = true;
256253

257-
if ( perturbseeds ) DetectChEdges( edgemag );
254+
// storage for edge magnitudes
255+
Mat edgemag;
256+
if (perturbseeds)
257+
DetectChEdges(edgemag);
258258

259259
if( m_algorithm == SLICO )
260260
GetChSeedsK();
@@ -268,7 +268,8 @@ void SuperpixelSLICImpl::initialize()
268268
m_numlabels = (int)m_kseeds[0].size();
269269

270270
// perturb seeds given edges
271-
if ( perturbseeds ) PerturbSeeds( edgemag );
271+
if (perturbseeds)
272+
PerturbSeeds(edgemag);
272273

273274
if( m_algorithm == MSLIC )
274275
{
@@ -569,12 +570,21 @@ inline void SuperpixelSLICImpl::DetectChEdges( Mat &edgemag )
569570
Sobel( m_chvec[c], dy, CV_32F, 0, 1, 1, 1.0f, 0.0f, BORDER_DEFAULT );
570571

571572
// acumulate ^2 derivate
572-
S_dx = S_dx + dx.mul(dx);
573-
S_dy = S_dy + dy.mul(dy);
574-
573+
MatExpr dx2 = dx.mul(dx);
574+
MatExpr dy2 = dy.mul(dy);
575+
if (S_dx.empty())
576+
{
577+
S_dx = dx2;
578+
S_dy = dy2;
579+
}
580+
else
581+
{
582+
S_dx += dx2;
583+
S_dy += dy2;
584+
}
575585
}
576586
// total magnitude
577-
edgemag += S_dx + S_dy;
587+
edgemag = S_dx + S_dy;
578588
}
579589

580590
/*

modules/ximgproc/test/test_slic.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
#include "test_precomp.hpp"
5+
6+
namespace opencv_test { namespace {
7+
8+
TEST(ximgproc_SuperpixelSLIC, smoke)
9+
{
10+
Mat img = imread(cvtest::findDataFile("cv/shared/lena.png"), IMREAD_COLOR);
11+
Mat labImg;
12+
cvtColor(img, labImg, COLOR_BGR2Lab);
13+
Ptr< SuperpixelSLIC> slic = createSuperpixelSLIC(labImg);
14+
slic->iterate(5);
15+
Mat outLabels;
16+
slic->getLabels(outLabels);
17+
EXPECT_FALSE(outLabels.empty());
18+
int numSuperpixels = slic->getNumberOfSuperpixels();
19+
EXPECT_GT(numSuperpixels, 0);
20+
}
21+
22+
}} // namespace

0 commit comments

Comments
 (0)