Skip to content

Commit

Permalink
Merge pull request #3 from gsaponaro/make_colour_image_optional
Browse files Browse the repository at this point in the history
Make colour image optional
See #1
  • Loading branch information
Giovanni Saponaro authored Sep 29, 2016
2 parents b6827ca + 6801bac commit c38e726
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 50 deletions.
6 changes: 3 additions & 3 deletions src/shapeDescriptor/shapeDescriptor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
<input>
<type>Image</type>
<port carrier="udp">/shapeDescriptor/rawImg:i</port>
<required>yes</required>
<required>no</required>
<priority>no</priority>
<description>Port receiving incoming colour images from robot camera or recording.</description>
<description>Port receiving optional incoming colour images from robot camera or recording.</description>
</input>

<input>
Expand All @@ -84,7 +84,7 @@
<output>
<type>Image</type>
<port carrier="udp">/shapeDescriptor/annotatedImg:o</port>
<description>Port with overlay visual representation of blobs and their descriptors.</description>
<description>Port with overlay visual representation of blobs and their descriptors. Requires incoming colour image.</description>
</output>

<output>
Expand Down
110 changes: 63 additions & 47 deletions src/shapeDescriptor/src/DescriptorThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,38 @@ void ShapeDescriptorThread::run()

void ShapeDescriptorThread::run2d()
{
bool useColor = false;

if (inRawImgPort.getInputCount())
useColor = true;

// acquire new input images
ImageOf<PixelBgr> *inRawImg = inRawImgPort.read(true);
ImageOf<PixelBgr> *inRawImg;
if (useColor)
inRawImg = inRawImgPort.read(true);

ImageOf<PixelBgr> *inBinImg = inBinImgPort.read(true);
ImageOf<PixelInt> *inLabImg = inLabImgPort.read(true);

// read timestamps
Stamp tsRaw, tsBin, tsLab;
if ( (inRawImgPort.getInputCount() && !inRawImgPort.getEnvelope(tsRaw)) ||
if ( (useColor && !inRawImgPort.getEnvelope(tsRaw)) ||
(inBinImgPort.getInputCount() && !inBinImgPort.getEnvelope(tsBin)) ||
(inLabImgPort.getInputCount() && !inLabImgPort.getEnvelope(tsBin)) )
{
yWarning("timestamp(s) missing");
}

if (inRawImg!=NULL && inBinImg!=NULL && inLabImg!=NULL)
if ((useColor && inRawImg!=NULL) ||
inBinImg!=NULL &&
inLabImg!=NULL)
{
// check dimensions of input images to be equal
const int refWidth = inRawImg->width();
const int refHeight = inRawImg->height();
if ( refWidth!=inBinImg->width() || refHeight!=inBinImg->height() ||
refWidth!=inLabImg->width() || refHeight!=inLabImg->height() )
const int refWidth = inBinImg->width();
const int refHeight = inBinImg->height();
if (refWidth!=inLabImg->width() || refHeight!=inLabImg->height())
{
yWarning("image dimensions mismatch");
yWarning("dimension mismatch between binary and labelled images");
}

// initialize object instances using labelled image
Expand Down Expand Up @@ -219,13 +228,17 @@ void ShapeDescriptorThread::run2d()

// construct temporary Obj2D with validity,contour,area
objs.push_back( Obj2D(isValid, cont[intIdx][largest], largestArea) );
// compute remaining shape descriptors and colour histogram
// compute remaining shape descriptors
objs[intIdx].computeDescriptors();
Mat inRaw = iplToMat(*inRawImg);
objs[intIdx].setMask( inRaw(objs[intIdx].getBoundingRect()) );
if (!objs[intIdx].computeHueHistogram())
yWarning("error computing hue histogram");
//for (int i=0; i<16; i++) yDebug() << "value" << i << "=" << objs[intIdx].getHueHistogram().at<float>(i);
Mat inRaw;
if (useColor)
{
inRaw = iplToMat(*inRawImg);
objs[intIdx].setMask( inRaw(objs[intIdx].getBoundingRect()) );
if (!objs[intIdx].computeHueHistogram())
yWarning("error computing hue histogram");
//for (int i=0; i<16; i++) yDebug() << "value" << i << "=" << objs[intIdx].getHueHistogram().at<float>(i);
}
}

// output shape descriptors of whole blobs
Expand Down Expand Up @@ -419,41 +432,44 @@ void ShapeDescriptorThread::run2d()

// annotated output image
Mat outAnnotatedMat;
outAnnotatedMat = iplToMat(*inRawImg);

const Scalar Blue(255,0,0);
const Scalar Yellow(255,255,0);
const int thickness = 2;
for (std::vector<Obj2D>::iterator it = objs.begin();
it != objs.end();
++it)
if (useColor)
{
// intIdx is an auxiliary current index: 0, 1, ...
int intIdx = std::distance(objs.begin(),it);

if (it->isValid())
{
drawContours(outAnnotatedMat,
cont[intIdx],
-1, // draw all the contours of cont[intIdx]
Blue,
thickness);
}
else
outAnnotatedMat = iplToMat(*inRawImg);

const Scalar Blue(255,0,0);
const Scalar Yellow(255,255,0);
const int thickness = 2;
for (std::vector<Obj2D>::iterator it = objs.begin();
it != objs.end();
++it)
{
drawContours(outAnnotatedMat,
cont[intIdx],
-1, // draw all the contours of cont[intIdx]
Yellow,
thickness);
// intIdx is an auxiliary current index: 0, 1, ...
int intIdx = std::distance(objs.begin(),it);

if (it->isValid())
{
drawContours(outAnnotatedMat,
cont[intIdx],
-1, // draw all the contours of cont[intIdx]
Blue,
thickness);
}
else
{
drawContours(outAnnotatedMat,
cont[intIdx],
-1, // draw all the contours of cont[intIdx]
Yellow,
thickness);
}
}
}

ImageOf<PixelBgr> &outAnnotatedYarp = outAnnotatedImgPort.prepare();
outAnnotatedYarp.resize(outAnnotatedMat.cols,
outAnnotatedMat.rows);
outAnnotatedMat.copyTo(iplToMat(outAnnotatedYarp));
outAnnotatedImgPort.setEnvelope(tsRaw);
outAnnotatedImgPort.write();
}
ImageOf<PixelBgr> &outAnnotatedYarp = outAnnotatedImgPort.prepare();
outAnnotatedYarp.resize(outAnnotatedMat.cols,
outAnnotatedMat.rows);
outAnnotatedMat.copyTo(iplToMat(outAnnotatedYarp));
outAnnotatedImgPort.setEnvelope(tsRaw);
outAnnotatedImgPort.write();
} // end if (useColor)
} // end if (inBinImg!=NULL)
}

0 comments on commit c38e726

Please sign in to comment.