コード例 #1
1
ファイル: labelingmodel.cpp プロジェクト: ribalda/gerbil
void LabelingModel::alterLabel(short index, cv::Mat1b mask,
							   bool negative)
{
	if (mask.empty()) {  // clear label
		mask = (labels == index);
		labels.setTo(0, mask);
	} else if (negative) { // remove pixels from label
		mask = mask.mul(labels == index);
		labels.setTo(0, mask);
	} else { // add pixels to label
		labels.setTo(index, mask);
	}

	// signal change
	emit partialLabelUpdate(labels, mask);
	invalidateMaskIcons();
}
コード例 #2
0
void ObjectDetector :: detect(const cv::Mat1f& distance_image,
                              cv::Mat1b& mask_image,
                              std::list<cv::Rect>& rects)
{
  if (mask_image.size() != distance_image.size())
    mask_image = cv::Mat1b(distance_image.size());

  for (int r = 0; r < distance_image.rows; ++r)
    for (int c = 0; c < distance_image.cols; ++c)
    {
      if (distance_image(r,c) >= m_min_threshold && distance_image(r,c) <= m_max_threshold)
        mask_image(r,c) = 255;
      else
        mask_image(r,c) = 0;
    }

  cv::morphologyEx(mask_image, mask_image,
                   cv::MORPH_OPEN,
                   getStructuringElement(cv::MORPH_RECT,
                                         cv::Size(3,3)));
  cv::morphologyEx(mask_image, mask_image,
                   cv::MORPH_CLOSE,
                   getStructuringElement(cv::MORPH_RECT,
                                         cv::Size(3,3)));

  std::vector< std::vector<cv::Point> > contours;
  cv::Mat1b contour_image = mask_image.clone();
  cv::findContours(contour_image, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  for (int i = 0; i < contours.size(); ++i)
  {
    cv::Rect rect = cv::boundingRect(cv::Mat(contours[i]));
    if (rect.area() > 300)
      rects.push_back(rect);
  }
}
コード例 #3
0
ファイル: opencv_utils.cpp プロジェクト: gpodevijn/nestk
 void apply_mask(cv::Mat1b& im, const cv::Mat1b& mask)
 {
   if (!mask.data)
     return;
   ntk_assert(im.size() == mask.size(), "Wrong mask size");
   for_all_rc(im)
     if (mask(r,c) == 0)
       im(r,c) = 0;
 }
コード例 #4
0
ファイル: main.cpp プロジェクト: shikumidesign/perc_sample
void Binalize(const cv::Mat& depth_image, const cv::Mat& uv_map, cv::Mat1b& binary_image, short depth_threshold_mm) {

	assert(depth_image.size() == uv_map.size());
	assert(depth_image.type() == CV_16SC1);
	assert(uv_map.type() == CV_32FC2);

	cv::Size binary_size = binary_image.size();
	//Clear all pixels of binary_image
	binary_image = 0;

	cv::Size loop_size = depth_image.size();
	if(depth_image.isContinuous() && uv_map.isContinuous()) {
		loop_size.width *= loop_size.height;
		loop_size.height = 1;
	}
	for(int i = 0; i < loop_size.height; ++i) {
		const short* depth = depth_image.ptr<short>(i);
		const cv::Vec2f* uv = uv_map.ptr<cv::Vec2f>(i);
		for(int j = 0; j < loop_size.width; ++j) {
			if(depth[j] < depth_threshold_mm) {
				int x = cvRound(uv[j][0] * binary_size.width);
				int y = cvRound(uv[j][1] * binary_size.height);
				if(0 <= x && x < binary_size.width && 0 <= y && y < binary_size.height) {
					binary_image.at<unsigned char>(cv::Point(x, y)) = 255;
				}
			}
		}
	}
}
コード例 #5
0
ファイル: test_voronoi.cpp プロジェクト: mkgessen/voronoi
 void init(const cv::Mat1b & query,
           std::string implementation_name_,
           bool crop_img_before_) {
   _implementation_name = implementation_name_;
   _crop_img_before = crop_img_before_;
   _first_img = query.clone();
   VoronoiThinner::copy_bounding_box_plusone(query, _first_img, true);
   _curr_skel = _first_img.clone();
   // printf("first_img:%s\n", image_utils::infosImage(_first_img).c_str());
   _curr_iter = 1;
   _nframes = 0;
 }
コード例 #6
0
ファイル: main.cpp プロジェクト: shikumidesign/perc_sample
void BlendScreen(cv::Mat3b& target_image, const cv::Mat1b& binary_image, cv::Vec3b color) {

	assert(target_image.size() == binary_image.size());

	cv::Size loop_size = target_image.size();
	if(target_image.isContinuous() && binary_image.isContinuous()) {
		loop_size.width *= loop_size.height;
		loop_size.height = 1;
	}
	for(int i = 0; i < loop_size.height; ++i) {
		cv::Vec3b* target = target_image.ptr<cv::Vec3b>(i);
		const unsigned char* binary = binary_image.ptr<unsigned char>(i);
		for(int j = 0; j < loop_size.width; ++j) {
			if(!binary[j]) {
				target[j][0] = target[j][0] + color[0] - (target[j][0] * color[0]) / 255;
				target[j][1] = target[j][1] + color[1] - (target[j][1] * color[1]) / 255;
				target[j][2] = target[j][2] + color[2] - (target[j][2] * color[2]) / 255;
			}
		}
	}
}
コード例 #7
0
ファイル: graphseg.cpp プロジェクト: ribalda/gerbil
cv::Mat1b GraphSeg::execute(const multi_img& input,
                                  const cv::Mat1b& seeds,
                                  cv::Mat1b *proba_map) {
	Stopwatch running_time("Total Running Time");
	cv::Mat1b output;
	int i;

	if ((seeds.cols != input.width)||(seeds.rows != input.height)) {
		std::cerr << "ERROR: Seed file dimensions do not match image dimensions!"
		          << std::endl;
		return output; // which is empty so far
	}

	Graph graph(seeds.cols, seeds.rows);

	/* extract seeds */
	cv::Mat1b::const_iterator it;
	if (config.multi_seed == true) {		// multilabel seed image
		graph.max_label = 0;
		for (i = 0, it = seeds.begin(); it < seeds.end(); ++i, ++it) {
			if (*it > 0) {
				graph.seeds.push_back(std::make_pair(i, *it));
				if (*it > graph.max_label)
					graph.max_label = *it;
			}
		}
	} else {
		graph.max_label = 2;
		for (i = 0, it = seeds.begin(); it < seeds.end(); ++i, ++it) {
			if (*it > 192) {
				graph.seeds.push_back(std::make_pair(i, 1));
			} else if (*it < 64) {
				graph.seeds.push_back(std::make_pair(i, 2));
			}
		}
	}

	// edge weights
	similarity_measures::SimilarityMeasure<multi_img::Value> *distfun;
#ifdef WITH_SOM
	boost::shared_ptr<som::GenSOM> som; // create in this scope for survival
	if (!config.som_similarity) {
		distfun = similarity_measures::SMFactory<multi_img::Value>
				::spawn(config.similarity);
	} else {
		input.rebuildPixels();
		som = boost::shared_ptr<som::GenSOM>(
					som::GenSOM::create(config.som, input));
		distfun = new som::SOMDistance<multi_img::Value>(*som, input);
	}
#else
	distfun = SMFactory<multi_img::Value>::spawn(config.similarity);
#endif

	assert(distfun);

	Stopwatch watch;
	if (config.algo == WATERSHED2) {
		/* Kruskal & RW on plateaus multiseeds linear time */

		graph.color_standard_weights(input, distfun, true);
		watch.print_reset("Graph coloring");
		// for PW, color_standard_weights is always called with geodesic = true
		output = graph.PowerWatershed_q2(config.geodesic, proba_map);
		watch.print("Segmentation");
	} else {
		graph.color_standard_weights(input, distfun, config.geodesic);
		watch.print_reset("Graph coloring");
		if (config.algo == KRUSKAL) { // Kruskal
			output = graph.MSF_Kruskal();
		} else if (config.algo == PRIM) { // Prim RB tree
			output = graph.MSF_Prim();
		}
		watch.print("Segmentation");
	}

	delete distfun;

	return output;
}
コード例 #8
0
void DisjointSets2::process_image(cv::Mat1b & img) {
    maggieDebug3("process_image()");

    if (img.isContinuous() == false)
        maggieError("process_image() only works with continuous images");

    // resizem but with no initial filling
    resize(img.cols * img.rows);

#ifdef TIMER_ON
    timer.reset();
#endif // TIMER_ON

    // make the disjoint sets loop
    nb_comp = 0;
    //bool is_left_non_null, is_up_non_null;
    CompIndex idx_current = 0, idx_left = -1, idx_up = -img.cols;
    CompIndex* roots_ptr = roots;

    for (CompIndex row = 0; row < img.rows; ++row) {
        // get the address of row
        uchar* data = img.ptr<uchar>(row);
        for (CompIndex col = 0; col < img.cols; ++col) {
            if (*data++ != 0) { // pixel non black
                // init roots
                //*roots_ptr = img.cols * y + x;
                *roots_ptr = idx_current;
                ++nb_comp;

                // check neighbours
                //                bool is_left_non_null = (x > 0 && roots[idx_left] != NO_NODE);
                //                bool is_up_non_null = (y > 0 && roots[idx_up] != NO_NODE);

                if (col > 0 && roots[idx_left] != NO_NODE) { // left non null
                    if (row > 0 && roots[idx_up] != NO_NODE) { // up non-null
                        maggieDebug3("%i: case 1", idx_current);
                        //idx_current_father =
                        Union(FindSet(idx_left),
                              //idx_current_father
                              Union(FindSet(idx_up), idx_current));
                    } // end up non-null

                    else { // up null
                        maggieDebug3("%i: case 2", idx_current);
                        //idx_current_father =
                        Union(FindSet(idx_left), idx_current);
                    } // end up null
                }

                else { // left null
                    if (row > 0 && roots[idx_up] != NO_NODE) { // up non-null
                        maggieDebug3("%i: case 3", idx_current);
                        //idx_current_father =
                        Union(FindSet(idx_up), idx_current);
                    } // end up non-null

                    else { // up null
                        maggieDebug3("%i: case 4", idx_current);
                        //idx_current_father = idx_current;
                    } // end up null
                } // end left null

                //display(img.cols);
            } // end pixel non black

            else { // pixel black
                *roots_ptr = NO_NODE;
            } // end pixel non black

            //++img_it;
            ++idx_left;
            ++idx_up;
            ++idx_current;
            ++roots_ptr;
        } // end loop col
    } // end loop row

#ifdef TIMER_ON
    timer.printTime("process_image()");
#endif // TIMER_ON


    //display(img.cols);
}