void ConnectedComponentLayer<Dtype>::Forward_cpu( const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const Dtype* bottom_data = bottom[0]->cpu_data(); Dtype* top_data = top[0]->mutable_cpu_data(); cv::Mat img(bottom[0]->height(), bottom[0]->width(), CV_8SC1); for (int_tp nc = 0; nc < bottom[0]->num() * bottom[0]->channels(); ++nc) { int_tp maxlabel = 0; for (int_tp y = 0; y < bottom[0]->height(); ++y) { for (int_tp x = 0; x < bottom[0]->width(); ++x) { int_tp val = bottom_data[nc * bottom[0]->width() * bottom[0]->height() + bottom[0]->width() * y + x]; if (val > maxlabel) { maxlabel = val; } img.at<unsigned char>(y, x) = val; } } cv::Mat seg = FindBlobs(maxlabel, img); #pragma omp parallel for for (int_tp y = 0; y < seg.rows; ++y) { for (int_tp x = 0; x < seg.cols; ++x) { top_data[nc * bottom[0]->width() * bottom[0]->height() + bottom[0]->width() * y + x] = seg.at<int_tp>(y, x); } } } }
vector<POI> POIFinder::GetPOIsInImage(IplImage *cv_image) { //Pre-processing // Image originalImage(cv_image); // Image grayImage = originalImage.getGrayScale(); /* IplImage *gray_image = cvCreateImage(cvGetSize(cv_image),cv_image->depth, 1); if(cv_image->nChannels == 3) { cvCvtColor(cv_image, gray_image,CV_BGR2GRAY); } else cvCopyImage(cv_image, gray_image); Image grayImage(gray_image);*/ Image grayImage(cv_image); //Core algorithm vector<Blob> detectedBlobs = FindBlobs(grayImage); vector<POI> newCoordinates; for (unsigned int i = 0; i < detectedBlobs.size(); i++) { newCoordinates.push_back(POI(detectedBlobs[i].GetCentroid())); //cout << "Novo POI: " << endl; //Debug::printCvPoint2D32f(newCoordinates[newCoordinates.size()-1].getCoordinates2d()); }; return newCoordinates; }
bool VisionPipeLine::stage_findingBlob() { FindBlobs(_grayFrame, _bloblist, BLOB_MIN_SIZE, BLOB_MAX_SIZE); // test whether a blob is pointed by the user mouse _hoveredBlobID = -1; for (int pos = 0; pos < _bloblist.size(); ++pos) { if ( _bloblist[pos]._box.contains(_lastMousePoint)) { _hoveredBlobID = pos; break; } } return true; }
void ImageViewer::on_actionConnected_Areas_triggered() { cv::Mat cvMat = ASM::QPixmapToCvMat(QPixmap::fromImage(*image)); cv::Mat output = cv::Mat::zeros(cvMat.size(), CV_8UC3); output.setTo(cv::Scalar(255,255,255)); cv::Mat grayscaleMat (cvMat.size(), CV_8U); //Convert BGR to Gray cv::cvtColor( cvMat, grayscaleMat, CV_BGR2GRAY ); //Binary image cv::Mat binary(grayscaleMat.size(), grayscaleMat.type()); cv::threshold(grayscaleMat, binary, 160.0, 255.0, cv::THRESH_BINARY); std::vector < std::vector<cv::Point2i > > blobs; FindBlobs(binary, blobs); for(size_t i=0; i < blobs.size(); i++) { unsigned char r = 255 * (rand()/(1.0 + RAND_MAX)); unsigned char g = 255 * (rand()/(1.0 + RAND_MAX)); unsigned char b = 255 * (rand()/(1.0 + RAND_MAX)); for(size_t j=0; j < blobs[i].size(); j++) { int x = blobs[i][j].x; int y = blobs[i][j].y; output.at<cv::Vec3b>(y,x)[0] = b; output.at<cv::Vec3b>(y,x)[1] = g; output.at<cv::Vec3b>(y,x)[2] = r; } } qDebug() << "#connected areas: " << blobs.size(); imageLabel->setPixmap(ASM::cvMatToQPixmap(output).copy()); }
//! Helper function to label image. void FindBoneTibia::Label() { cv::Mat output = cv::Mat::zeros(img.size(), CV_8UC3); std::vector < std::vector<cv::Point2i> > blobs; cv::adaptiveThreshold(img, binary, 1.0, CV_ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 21, -0.8); //binary = 1-binary; FindBlobs(binary, blobs); if (blobs.size() == 0) { cout << "\nthe blob size was zero \n"; return; } int Max = 0; int Maxcount = 0; int i = 0; int x1 = config->GetSettings("FindBoneTibia", "bounding_box_x1", 230); int x2 = config->GetSettings("FindBoneTibia", "bounding_box_x2", 270); int y1 = config->GetSettings("FindBoneTibia", "bounding_box_y1", 130); int y2 = config->GetSettings("FindBoneTibia", "bounding_box_y2", 150); int blobsize = config->GetSettings("FindBoneTibia", "min_blob_size", 200); for (i = 0; i < blobs.size(); i++) { if (blobs[i].size() > blobsize) { if (inrange (&blobs[i])) { int pointsintherange = 0; for (size_t j = 0; j < blobs[i].size(); j++) { if ((blobs[i][j].x > x1) && (blobs[i][j].x < x2) && (blobs[i][j].y > y1) && (blobs[i][j].y < y2)) pointsintherange++; } if (Maxcount < pointsintherange) { Max = i; Maxcount = pointsintherange; } } } } i = Max; unsigned char r = 255 * (rand() / (1.0 + RAND_MAX)); unsigned char g = 255 * (rand() / (1.0 + RAND_MAX)); unsigned char b = 255 * (rand() / (1.0 + RAND_MAX)); for (size_t j = 0; j < blobs[i].size(); j++) { int x = blobs[i][j].x; int y = blobs[i][j].y; output.at<cv::Vec3b>(y, x)[0] = b; output.at<cv::Vec3b>(y, x)[1] = g; output.at<cv::Vec3b>(y, x)[2] = r; } int size = config->GetSettings("FindBoneTibia", "dilation_size", 1); ; int type = cv::MORPH_ELLIPSE; cv::Mat element = cv::getStructuringElement(type, cv::Size(2 * size, 2 * size), cv::Point(size, size)); if (size > 0) cv::dilate(output, output, element); cv::Canny(output, output, config->GetSettings("FindBoneTibia", "Canny_low_thresh", 5), config->GetSettings("FindBoneTibia", "Canny_high_thresh", 10), config->GetSettings("FindBoneTibia", "Canny_kernel", 3)); int x_mult = config->GetSettings("FindBoneTibia", "X_axis_multiplier", 1); int y_mult = config->GetSettings("FindBoneTibia", "Y_axis_multiplier", 2); int z_mult = config->GetSettings("FindBoneTibia", "Z_axis_multiplier", 1); int plane_low = config->GetSettings("FindBoneTibia", "Image_plane_low", 326); int plane_high = config->GetSettings("FindBoneTibia","Image_plane_high", 445); for (int x = 0; x < img.cols; x++) { for (int y = 0; y < img.rows; y++) { if (output.at<uchar>(y, x) == 255) { pcl::PointXYZ point(x * x_mult, id * z_mult, y * y_mult); if (id > plane_low && id < plane_high) this->LabeledOutput->at(TIBA_TRAN)->cloud->push_back(point); } } } }