void opticalFlow::occMatpEst( Mat_<Vec2f> &flow12, Mat_<Vec2f> &flow21, Mat_<uchar>&occMap) { int iy, ix; const float FLOW_PIXEL_THRESH = 2; occMap.setTo(255); for (iy=0; iy<height1; ++iy) { for (ix=0; ix<width1; ++ix) { Vec2f fFlow = flow12[iy][ix]; int ny, nx; ny = floor(iy+fFlow[1]+0.5); nx = floor(ix+fFlow[0]+0.5); if (ny>=0 && ny<height1 && nx>=0 && nx<width1) { cv::Vec2f bFlow = flow21[ny][nx]; if (fabs(bFlow[1]+ny-iy)<FLOW_PIXEL_THRESH && fabs(bFlow[0]+nx-ix)<FLOW_PIXEL_THRESH) { continue; } } occMap[iy][ix] = 0; } } Mat bw = occMap; Mat labelImage(occMap.size(), CV_32S); int nLabels = connectedComponents(bw, labelImage, 8); occMap[iy][ix] = 0; vector<int> hist(nLabels,0); for (iy=0; iy<height1; ++iy) for (ix=0; ix<width1; ++ix) hist[labelImage.at<int>(iy,ix)]++; vector<int> rmv_list; rmv_list.reserve(20); for (int i=0;i<nLabels;++i){ if (hist[i]<50) rmv_list.push_back(i); } for (iy=0; iy<height1; ++iy) { for (ix=0; ix<width1; ++ix) { for (int r=0; r<rmv_list.size(); ++r) if(labelImage.at<int>(iy,ix) == rmv_list[r]) occMap[iy][ix] = 0; } } }
void labelAndWaitForKey(const std::string& window, const std::string& text, const cv::Mat& img, ScaleType type, bool resize, int w, int h) { cv::Mat rescaled = rescaleImageIntensity(img, type); if (resize) { resizeToDisplay(rescaled, rescaled, w, h); } labelImage(rescaled, text); //cv::imshow(window, rescaled); //cv::waitKey(); }
static void on_trackbar(int, void*) { Mat bw = threshval < 128 ? (img < threshval) : (img > threshval); Mat labelImage(img.size(), CV_32S); int nLabels = connectedComponents(bw, labelImage, 8); std::vector<Vec3b> colors(nLabels); colors[0] = Vec3b(0, 0, 0);//background for(int label = 1; label < nLabels; ++label){ colors[label] = Vec3b( (rand()&255), (rand()&255), (rand()&255) ); } Mat dst(img.size(), CV_8UC3); for(int r = 0; r < dst.rows; ++r){ for(int c = 0; c < dst.cols; ++c){ int label = labelImage.at<int>(r, c); Vec3b &pixel = dst.at<Vec3b>(r, c); pixel = colors[label]; } } imshow( "Connected Components", dst ); }
void ITextUtils::labelImage(const IImage* image, const std::string& label, const LabelPosition labelPosition, IImageListener* listener, bool autodelete) { const float fontSize = 20; const Color color = Color::white(); const Color shadowColor = Color::black(); const int separation = 2; labelImage(image, label, labelPosition, separation, fontSize, &color, &shadowColor, listener, autodelete); }
/** handle the results of the BottomUp operator * pid == 0 -> no operator or node have no holistic operator * pid == -1 -> can't start process * return -1 - error * return 0 - delete 'this' * return 1 - analyze is ready * return 2 - processing * remember: a node without SNode is a "Trash-Node" * this node must have children */ int INode::evalBottomUp(int pid) { #ifdef DEBUGMSG qDebug("#* INode::evalBottomUp(%s): pid=%d (%p)\n", (const char *) name(), pid, this); #endif if (pid <= 0) { #ifdef DEBUGMSG qDebug("# (ERROR) evalBottomUp(%p): BottomUp operator have no result \n", this); #endif //! hier noch das Icon im Browser auf rot setzen (Problem) status(TRASH); if (parent()) parent()->decrementCount(); return -1; } execState(BU); if (isRoot()) { //! Debug qDebug("INode::evalBottomUp: is Root"); #ifdef WIN32 QMessageBox::information(0,"INode","evalBottomUp: is Root",QMessageBox::Cancel); #endif } analysis_->nodeChange(this); QList < INode > &nodeList = sNode_->evalBottomUp(this); //new composition analysis_->nodeChange(this); #ifdef DEBUGMSG qDebug("# (INFO) nodeList count: %d\n", nodeList.count()); #endif if (isRoot()) { switch (nodeList.count()) { case 0: #ifdef DEBUGMSG qDebug("# (ERROR) No szene found(%p)\n", this); #endif #ifdef WIN32 QMessageBox::warning(0,"INode","evalBottomUp: No szene found"); #endif return 1; case 1:{ #ifdef DEBUGMSG qDebug("# Root node is unique (%p)\n", this); #endif //alle bis auf die zurückgegebenen aus this entfernen { //! eigentlich können die children direkt gelöscht werden! INode *trashNode = new INode(*this); #ifdef WIN32 if (trashNode == 0){ cout << "Out of Memory..11"; exit(1); } #endif trashNode->status(TRASH); { // normally this code should do nothing because the children are already moved INode *el; while (children().count()) { el = (children()).first(); trashNode->childLink(el); analysis_->nodeChange(el); } } #if 0 delete trashNode; #else analysis_->setTrashRoot(trashNode); #endif } { INode *el; INode *iNode = nodeList.first(); update(iNode->attribList()); labelImage(iNode->labelImage()); el = (iNode->children()).first(); while (el) { childLink(el); analysis_->nodeChange(el); el = (iNode->children()).first(); } delete iNode; } delete & nodeList; return 1; //=> analyze is ready } default: #ifdef DEBUGMSG qDebug("# (ERROR) Root node must be unique (%p)\n", this); #endif #ifdef WIN32 QMessageBox::warning(0,"INode","evalBottomUp: Root node must be unique"); #endif delete & nodeList; return -1; } } if (nodeList.isEmpty()) { #ifdef DEBUGMSG qDebug ("# (ERROR) evalBottomUp: QList<<INode> nodeList is empty - all trash\n"); #endif status(TRASH); parent()->decrementCount(); delete & nodeList; //return 0; // XXX TEST - fuer die Ergebnisdarstellung return 2; //=> } QListIterator < INode > it(nodeList); for (; it.current(); ++it) { INode *iNode = it.current(); analysis_->nodeChange(iNode); parent()->childLink(iNode); iNode->status(CI); INode *el; for (el = iNode->children().first(); el != 0; el = iNode->children().next()) { if (el->status() <= MI) iNode->status(MI); analysis_->nodeChange(el); //info to the rest of the world } } INode *p = parent(); //for later access if ((this->children()).isEmpty()) { #ifdef DEBUGMSG qDebug("# (INFO) evalBottomUp: empty list - no trash\n"); // #endif parent()->childUnlink(this); p->decrementCount(); delete & nodeList; return 0; // delete this; } else { //this->sNode(0); //make a TRASH Node; -> has no SNode this->status(TRASH); p->decrementCount(); delete & nodeList; //return 0; // XXX TEST - fuer die Ergebnisdarstellung return 2; } }
ImageT<int> *relabelOutput (Image &Img, char *labelname, char *descr) { Image hist = Img.calcHistogram(0, 256, 256); int max = 255; while (hist.getInt (max, 0, 0) == 0 && max > 0) max--; if (!max) { cout << "no texture classes in output segmentation!" << endl; exit (-1); } else cout << "identified " << max << " texture classes in segmentation result" << endl; ImageT<int> *result_img = new ImageT<int>(Img.sizeX (), Img.sizeY ()); result_img->typeImage(_PFM_SINT); ImageT<int> tmp_img(Img.sizeX(), Img.sizeY()); tmp_img.typeImage(_PFM_SINT); cout << "neue Bilder angelegt" << endl; int *dest, *source; unsigned char *orig; int sumlabel[max + 1]; memset(sumlabel, 0, sizeof (int) * (max + 1)); cout << "memset Befehl" << endl; // sumlabel stores the number of labels found for each class, // labelling should start with 1 sumlabel[0] = 1; for (int i = 1; i <= max; i++) { dest = tmp_img.data(); orig = (unsigned char *) Img.data(); cout << "Schleife über i" << endl; // extract one class and prepare it for labelling for (int j = 0; j < Img.sizeImage (); ++j, ++dest, ++orig) if (*orig == i) *dest = 0; else *dest = 1; // label the current class and store the number of labels // label 0 and 1 are background and label border --> throw them away (-2)! sumlabel[i] = labelImage(tmp_img) - 2; // sum up the labelnumbers for fusion of the different classes sumlabel[i] += sumlabel[i - 1]; // shift the labels, so that they are unique (fusion) source = tmp_img.data(); dest = result_img->data(); for (int j = 0; j < tmp_img.sizeImage(); ++j, ++dest, ++source) { // if the label is not background or border (-2), add it to the result if (*source > 1) *dest = *source - 2 + sumlabel[i - 1]; } tmp_img.clear(); } cout << "image relabeled" << endl; // generate the label description ImageT<int> list; calcBoundingBoxes(list, *result_img, sumlabel[max]); FILE *fp = fopen (descr, "w"); if (!fp) { cout << "error opening file " << descr << " for writing." << endl; exit (-1); } // list only the labels with value greater than zero (0 is background) for (int i = 1, j = 0; i < sumlabel[max]; ++i) { if (i >= sumlabel[j + 1]) j ++; fprintf(fp, "<region class=\"class%i\" id=\"%d\" p=\"0.7\" file=\"%s\" llx=\"%d\" lly=\"%d\" urx=\"%d\" ury=\"%d\" />\n", j+1, i, labelname, list[0][i], list[1][i], list[2][i], list[3][i]); } cout << "Regionenbeschreibung erzeugt" << endl; fclose(fp); return (result_img); }
void MapGraphicsView::setStatusMessage(QString msg) { if(msg.isEmpty()) { m_statusLabel->setPixmap(QPixmap()); m_statusLabel->hide(); return; } m_statusLabel->show(); if(Qt::mightBeRichText(msg)) { QTextDocument doc; doc.setHtml(msg); msg = doc.toPlainText(); } QImage tmp(1,1,QImage::Format_ARGB32_Premultiplied); QPainter tmpPainter(&tmp); QFont font("");//"", 20); tmpPainter.setFont(font); QRectF maxRect(0, 0, (qreal)width(), (qreal)height() * .25); QRectF boundingRect = tmpPainter.boundingRect(maxRect, Qt::TextWordWrap | Qt::AlignHCenter, msg); boundingRect.adjust(0, 0, tmpPainter.font().pointSizeF() * 3, tmpPainter.font().pointSizeF() * 1.25); QImage labelImage(boundingRect.size().toSize(), QImage::Format_ARGB32_Premultiplied); memset(labelImage.bits(), 0, labelImage.byteCount()); QPainter p(&labelImage); QColor bgColor(0, 127, 254, 180); #ifdef Q_OS_ANDROID bgColor = bgColor.lighter(300); #endif p.setPen(QPen(Qt::white, 2.5)); p.setBrush(bgColor); p.drawRoundedRect(labelImage.rect().adjusted(0,0,-1,-1), 3., 3.); QImage txtImage(boundingRect.size().toSize(), QImage::Format_ARGB32_Premultiplied); memset(txtImage.bits(), 0, txtImage.byteCount()); QPainter tp(&txtImage); tp.setPen(Qt::white); tp.setFont(font); tp.drawText(QRectF(QPointF(0,0), boundingRect.size()), Qt::TextWordWrap | Qt::AlignHCenter | Qt::AlignVCenter, msg); tp.end(); double ss = 8.; p.drawImage((int)-ss,(int)-ss, ImageUtils::addDropShadow(txtImage, ss)); p.end(); #ifdef Q_OS_ANDROID m_statusLabel->setPixmap(QPixmap::fromImage(labelImage)); #else m_statusLabel->setPixmap(QPixmap::fromImage(ImageUtils::addDropShadow(labelImage, ss))); #endif }