Commit c0bc0469cebcb61fa241159d35cb4a351f2a8f39

Authored by Austin Blanton
1 parent ffcf4a97

Add DrawSegmentationTransform

Showing 1 changed file with 53 additions and 0 deletions
openbr/plugins/draw.cpp
... ... @@ -374,6 +374,59 @@ class DrawOpticalFlow : public UntrainableTransform
374 374 };
375 375 BR_REGISTER(Transform, DrawOpticalFlow)
376 376  
  377 +/*!
  378 + * \ingroup transforms
  379 + * \brief Fill in the segmentations or draw a line between intersecting segments.
  380 + * \author Austin Blanton \cite imaus10
  381 + */
  382 +class DrawSegmentation : public UntrainableTransform
  383 +{
  384 + Q_OBJECT
  385 + Q_PROPERTY(bool fillSegment READ get_fillSegment WRITE set_fillSegment RESET reset_fillSegment STORED false)
  386 + BR_PROPERTY(bool, fillSegment, true)
  387 + Q_PROPERTY(QString original READ get_original WRITE set_original RESET reset_original STORED false)
  388 + BR_PROPERTY(QString, original, "original")
  389 +
  390 + void project(const Template &src, Template &dst) const
  391 + {
  392 + if (!src.file.contains("SegmentsMask") || !src.file.contains("NumSegments")) qFatal("Must supply a Contours object in the metadata to drawContours.");
  393 + Mat segments = src.file.get<Mat>("SegmentsMask");
  394 + int numSegments = src.file.get<int>("NumSegments");
  395 +
  396 + dst.file = src.file;
  397 + Mat drawn;
  398 + if (fillSegment) { // color the whole segment
  399 + drawn = Mat(segments.size(), CV_8UC3, Scalar::all(0));
  400 + for (int i=1; i<numSegments+1; i++) {
  401 + Mat mask = segments == i;
  402 + // set to a random color - get ready for a craaaazy acid trip
  403 + int b = theRNG().uniform(0, 255);
  404 + int g = theRNG().uniform(0, 255);
  405 + int r = theRNG().uniform(0, 255);
  406 + drawn.setTo(Scalar(r,g,b), mask);
  407 + }
  408 + } else { // draw lines where there's a color change
  409 + // TODO: i don't think this quite works yet
  410 + // use an edge detector set for any change in pixel value?
  411 + const Vec3b color(0,255,0);
  412 + if (!dst.file.contains(original)) qFatal("You must store the original image in the metadata with SaveMat.");
  413 + drawn = dst.file.get<Mat>(original);
  414 + dst.file.remove(original);
  415 + for (int i=0; i<segments.rows; i++) {
  416 + for (int j=0; j<segments.cols; j++) {
  417 + int curr = segments.at<int>(i,j);
  418 + if (curr != segments.at<int>(i,j+1) || curr != segments.at<int>(i+1,j)) {
  419 + drawn.at<Vec3b>(i,j) = color;
  420 + }
  421 + }
  422 + }
  423 + }
  424 +
  425 + dst.m() = drawn;
  426 + }
  427 +};
  428 +BR_REGISTER(Transform, DrawSegmentation)
  429 +
377 430 // TODO: re-implement EditTransform using Qt
378 431 #if 0
379 432 /*!
... ...