/**
     * @brief ProcessAux
     * @param imgIn
     * @param bBilateral
     * @param nSuperPixels
     * @return
     */
    Image *ProcessAux(Image *imgIn, bool bBilateral, int nSuperPixels = 4096)
    {
        if(bBilateral) {
            float minVal, maxVal;
            imgIn->getMinVal(NULL, &minVal);
            imgIn->getMaxVal(NULL, &maxVal);

            float nLevels = log10f(maxVal) - log10f(minVal) + 1.0f;
            float nLayer = ((maxVal - minVal) / nLevels) / 4.0f;
            float area = imgIn->widthf * imgIn->heightf * perCent;
            int iterations = MAX(int(sqrtf(area)) / 2, 1);

            //create filters
            if(fltIt == NULL) {
                fltBil = new FilterBilateral2DS(1.0f, nLayer);
                fltIt  = new FilterIterative(fltBil, iterations);
            }

    #ifdef PIC_DEBUG
            printf("Layer: %f iterations: %d\n", nLayer, iterations);
    #endif
            //Iterative bilateral filtering
            Image *imgOut = fltIt->Process(Single(imgIn), imgIn_flt);

            return imgOut;
        } else {
            Slic sp;
            sp.execute(imgIn, nSuperPixels);
            Image *imgOut = sp.getMeanImage(NULL);
            return imgOut;
        }
    }
예제 #2
0
void SLIC(const Mat& src, Mat& clusters, int numSuperPixels, int nc, enum method) {
	int width = src.cols;
	int height = src.rows;
	double step = sqrt(width*height / (double)numSuperPixels);

	IplImage iplImg = IplImage(src);

	Slic slic;
    slic.generate_superpixels(&iplImg, step, nc);
    slic.create_connectivity(&iplImg);

    clusters.create(src.size(), CV_32S);
    for(int h = 0; h < height; h++) {
    	for(int w = 0; w < width; w++) {
    		clusters.at<int>(h,w) = slic.clusters[w][h];
    	}
    }
}
예제 #3
0
int main(int argc, char *argv[]) {
    /* Load the image and convert to Lab colour space. */
    Mat image = imread("dog.png", 1);
    Mat lab_image;
    cvtColor(image, lab_image, COLOR_BGR2Lab);
    
    /* Yield the number of superpixels and weight-factors from the user. */
    int w = image.cols, h = image.rows;
    int nr_superpixels = 400;
    int nc = 40;

    double step = sqrt((w * h) / (double) nr_superpixels);

    /* Perform the SLIC superpixel algorithm. */
    Slic slic;
    slic.generate_superpixels(lab_image, int(step), nc);
    slic.create_connectivity(lab_image);
    slic.display_contours(image, Vec3b(0,0,255));
    
    /* Display the contours and show the result. */
    imshow("result", image);
    waitKey(0);
}
예제 #4
0
int main(int argc, char *argv[]) {
    /* Load the image and convert to Lab colour space. */
    IplImage *image = cvLoadImage(argv[1], 1);
    IplImage *lab_image = cvCloneImage(image);
    cvCvtColor(image, lab_image, CV_BGR2Lab);
    
    /* Yield the number of superpixels and weight-factors from the user. */
    int w = image->width, h = image->height;
    int nr_superpixels = atoi(argv[2]);
    int nc = atoi(argv[3]);

    double step = sqrt((w * h) / (double) nr_superpixels);
    
    /* Perform the SLIC superpixel algorithm. */
    Slic slic;
    slic.generate_superpixels(lab_image, step, nc);
    slic.create_connectivity(lab_image);
    
    /* Display the contours and show the result. */
    slic.display_contours(image, CV_RGB(255,0,0));
    cvShowImage("result", image);
    cvWaitKey(0);
    cvSaveImage(argv[4], image);
}
예제 #5
0
int main(int argc, char *argv[]) {
  /* Load the image and convert to Lab colour space. */
  cv::Mat image = cv::imread(argv[1], 1);
  cv::Mat lab_image;
  cv::cvtColor(image, lab_image, CV_BGR2Lab);
  
  /* Yield the number of superpixels and weight-factors from the user. */
  int w = image.cols, h = image.rows;
  int nr_superpixels = atoi(argv[2]);
  int nc = atoi(argv[3]);
  
  double step = sqrt((w * h) / (double) nr_superpixels);
  
  /* Perform the SLIC superpixel algorithm. */
  Slic slic;
  slic.generate_superpixels(lab_image, step, nc);
  slic.create_connectivity(lab_image);
  
  std::vector<std::vector<cv::Point> > pointSets = slic.generatePointSets(image);
  
  
  /* Display the contours and show the result. */
  //  slic.colour_with_cluster_means(image);
  slic.display_contours(image, cv::Vec3b(0,0,255));
 
  std::vector<std::vector<int> > edges = slic.generateGraph(image);
  slic.displayGraph(image, edges, cv::Vec3b(0,255,255));

  slic.display_center_grid(image, cv::Vec3b(0, 0, 0));
  
  //  std::vector<std::vector<cv::Point> > polys = slic.generateBoundingPolys(image);
  //  slic.displayBoundingPolys(image, polys, cv::Vec3b(255,0,0));
  //  std::vector<cv::Rect> boxes = slic.generateBoundingBoxes(image);
  //  slic.displayBoundingBoxes(image, boxes, cv::Vec3b(0, 255, 255));
  //  std::vector<cv::RotatedRect> boxes = slic.generateRotBoundingBoxes(image);
  //  slic.displayRotBoundingBoxes(image, boxes, cv::Vec3b(0, 255, 255));
  //  std::cout << boxes[0].size() << std::endl;

  cv::namedWindow("result", CV_WINDOW_AUTOSIZE);
  cv::imshow("result", image);
  cv::waitKey(0);
  cv::imwrite(argv[4], image);
}
  void SLICSuperPixels::imageCallback(const sensor_msgs::Image::ConstPtr& image)
  {
    boost::mutex::scoped_lock lock(mutex_);
    cv::Mat in_image = cv_bridge::toCvShare(image, image->encoding)->image;
    cv::Mat bgr_image;
    if (in_image.channels() == 1) {
      // gray image
      cv::cvtColor(in_image, bgr_image, CV_GRAY2BGR);
    }
    else if (image->encoding == sensor_msgs::image_encodings::RGB8) {
      // convert to BGR8
      cv::cvtColor(in_image, bgr_image, CV_RGB2BGR);
    }
    else {
      bgr_image = in_image;
    }
    //cv::Mat bgr_image = cv_ptr->image;
    cv::Mat lab_image, out_image, mean_color_image, center_grid_image;
    // slic
    if (debug_image_) {
      bgr_image.copyTo(out_image);
      bgr_image.copyTo(mean_color_image);
      bgr_image.copyTo(center_grid_image);
    }
    cv::cvtColor(bgr_image, lab_image, CV_BGR2Lab);
    int w = image->width, h = image->height;
    double step = sqrt((w * h) / (double) number_of_super_pixels_);
    Slic slic;
    slic.generate_superpixels(lab_image, step, weight_);
    slic.create_connectivity(lab_image);

    if (debug_image_) {
      // creating debug image may occur seg fault.
      // So, publish_debug_images was added, in order to create debug image explicitly
      // See https://github.com/jsk-ros-pkg/jsk_recognition/pull/2181
      slic.colour_with_cluster_means(mean_color_image);
      slic.display_center_grid(center_grid_image, cv::Scalar(0, 0, 255));
      slic.display_contours(out_image, cv::Vec3b(0,0,255));

      pub_debug_.publish(cv_bridge::CvImage(
                                            image->header,
                                            sensor_msgs::image_encodings::BGR8,
                                            out_image).toImageMsg());
      pub_debug_mean_color_.publish(cv_bridge::CvImage(
                                                       image->header,
                                                       sensor_msgs::image_encodings::BGR8,
                                                       mean_color_image).toImageMsg());
      pub_debug_center_grid_.publish(cv_bridge::CvImage(
                                                        image->header,
                                                        sensor_msgs::image_encodings::BGR8,
                                                        center_grid_image).toImageMsg());
    }
    // publish clusters
    cv::Mat clusters;
    cv::transpose(slic.clusters, clusters);
    clusters = clusters + cv::Scalar(1);
    pub_.publish(cv_bridge::CvImage(
                   image->header,
                   sensor_msgs::image_encodings::TYPE_32SC1,
                   clusters).toImageMsg());
  }
static vector<vector<Point> >  make_superpixels(Mat original)
{
    IplImage *image = new IplImage(original);
    IplImage *lab_image = cvCloneImage(image);
    cout<<" yoman "<<endl;
    //cvCvtColor(image, lab_image, CV_BGR2Lab);
    cout<<" yoman2 "<<endl;
    int w = image->width, h = image->height;
    int nr_superpixels = atoi("2000");
    int nc = atoi("60");
    double step = sqrt((w * h) / (double) nr_superpixels);
    Slic slic;
    slic.generate_superpixels(lab_image, step, nc);
    slic.create_connectivity(lab_image);
    Mat ret;
    /*cout<<" size "<<slic.centers.size()<<endl;*/
    vector<vector<Point> > contours(slic.centers.size());
    for(int  i=0; i<slic.centers.size()+5; i++)
    {
        for(int  j=0; j<slic.centers.size()+5; j++)
        {
            len[i][j]=0;
            differ[i][j]=0;
            if(i==j)
            {
                GR[i][j]=0;
                continue;
            }
            GR[i][j]=inf;
        }
    }
    cout<<" Graph Initialized "<<slic.clusters.size()<<endl;
    /*freopen("out.txt","w",stdout);
    for(int i=0; i<slic.clusters.size(); i++)
    {
       for(int j=0; j<slic.clusters[i].size(); j++)
       {
         cout<<slic.clusters[i][j]<<" ";
      }
      cout<<endl;
    }
    cout<<endl;*/
    for(int i=0; i<slic.clusters.size(); i++)
    {
        for(int j=0; j<slic.clusters[i].size(); j++)
        {
            int cluster=slic.clusters[i][j];
            if(cluster<0)
            {
                cluster=0;
            }
            if(contours[cluster].size()==0)
            {
                contours[cluster].push_back(Point(11111,11111));
                contours[cluster].push_back(Point(-1,-1));
            }
            // if(i==637){cout<<cluster<<" cluster leave "<<endl;}
            int xc=i;
            int yc=j;
            //if(i==637)cout<<" YES 1"<<endl;
            contours[cluster].push_back(Point(yc,xc));
            contours[cluster][0]=Point(min(contours[cluster][0].x,yc),min(contours[cluster][0].y,xc));
            contours[cluster][1]=Point(max(contours[cluster][1].x,yc),max(contours[cluster][1].y,xc));
            //if(i==637)cout<<" YES 2"<<endl;
            for(int  k=0; k<4; k++)
            {
                int newxc=xc+dxc[k];
                int newyc=yc+dyc[k];
                if(newxc<0 ||  newyc<0 || newxc>=slic.clusters.size() || newyc>=slic.clusters[i].size())
                {
                    continue;
                }
                int newcluster=slic.clusters[newxc][newyc];
                if(newcluster<0)
                {
                    newcluster=0;
                }
                GR[cluster][newcluster]=1;
                GR[newcluster][cluster]=1;
                len[cluster][newcluster]=len[cluster][newcluster]+1;
                //len[newcluster][cluster]=len[newcluster][cluster]+1;
            }
        }
        // cout<<endl;
    }
    floyd_warshall(slic.centers.size());
    cout<<" LEAVING "<<endl;
    return contours;
}
double functions::global_graph_computeFramesDist(
	IplImage* prev_image, 
	IplImage* cur_image, 
	vec3di prev_graph, 
	vec3di cur_graph, 
	Slic prev_slic, 
	Slic cur_slic, 
	CvPoint p1, /*point by prev. frame*/
	CvPoint p2) /*point by cur. frame*/
{
	double dist = 0;

	int p1_blue = CV_IMAGE_ELEM(prev_image, uchar, p1.y, (p1.x * 3));
	int p1_green = CV_IMAGE_ELEM(prev_image, uchar, p1.y, (p1.x * 3) + 1);
	int p1_red = CV_IMAGE_ELEM(prev_image, uchar, p1.y, (p1.x * 3) + 2);

	int p2_blue = CV_IMAGE_ELEM(cur_image, uchar, p2.y, (p2.x * 3));
	int p2_green = CV_IMAGE_ELEM(cur_image, uchar, p2.y, (p2.x * 3) + 1);
	int p2_red = CV_IMAGE_ELEM(cur_image, uchar, p2.y, (p2.x * 3) + 2);

	int main_part = abs(p1_blue - p2_blue) + abs(p1_green - p2_green) + abs(p1_red - p2_red);
	
	vector<CvPoint> neighbors_cur_points = cur_slic.find_neighbors(cur_image, p1.x, p1.y);
	vector<CvPoint> neighbors_prev_points = cur_slic.find_neighbors(prev_image, p2.x, p2.y);

	vector<vector<int>> neighbors_cur;
	vector<vector<int>> neighbors_prev;

	for (int i = 0; i < neighbors_cur_points.size(); i++)
	{
		vector<int> temp;
		for (int j = 0; j < 3; j++)
		{
			temp.push_back(CV_IMAGE_ELEM(cur_image, uchar, neighbors_cur_points[i].y, neighbors_cur_points[i].x * 3 + j));
		}
		neighbors_cur.push_back(temp);
	}

	for (int i = 0; i < neighbors_prev_points.size(); i++)
	{
		vector<int> temp;
		for (int j = 0; j < 3; j++)
		{
			temp.push_back(CV_IMAGE_ELEM(prev_image, uchar, neighbors_prev_points[i].y, neighbors_prev_points[i].x * 3+ j));
		}
		neighbors_prev.push_back(temp);
	}
	
	int sec_part = 0;

	int num = 0;
	if (neighbors_cur.size() > neighbors_prev.size())
		num = neighbors_prev.size();
	else
		num = neighbors_cur.size();
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			sec_part += abs(neighbors_cur[i][j] - neighbors_prev[i][j]);
		}
	}

	dist = main_part + sec_part;

	return dist;
}
void MultiLabelTree::SplitOnSlic(CBranch* node) {
    vector<vector<float>> pos;
	vector<vector<float>> value;
	for (int i = 0; i < node->linked_nodes.size(); ++i) {
		pos.push_back(node->linked_nodes[i]->mean_pos);
		value.push_back(node->linked_nodes[i]->mean_values);
	}

    vector<vector<vector<float>>> pixel_data;
    vector<vector<int>> node_count;
    int imw = (int)sqrt(node->linked_nodes.size()), imh = 200;
    imh = (node->top - node->bottom) / (node->right - node->left) * imw;
    while (imw * imh < 16 * SLIC_PIXEL_THRESHOLD) {
        imh *= 2;
        imw *= 2;
    }

    pixel_data.resize(imh);
    node_count.resize(imh);
    for (int i = 0; i < imh; ++i) {
        pixel_data[i].resize(imw);
        node_count[i].resize(imw, 0);
        for (int j = 0; j < imw; ++j)
            pixel_data[i][j].resize(point_dataset_->var_num, 0);
    }

    float node_width = node->right - node->left;
    float node_height = node->top - node->bottom;
    for (int i = 0; i < pos.size(); i++) {
        int xi = (int)((pos[i][0] - node->left) / node_width * (imw - 1));
        int yi = (int)((pos[i][1] - node->bottom) / node_height * (imh - 1));
        node_count[yi][xi]++;
        for (int j = 0; j < point_dataset_->var_num; j++)
            pixel_data[yi][xi][j] += value[i][j];
    }

        
    for (int i = 0; i < imh; ++i)
        for (int j = 0; j < imw; ++j)
            if (node_count[i][j] != 0) {
                for (int k = 0; k < point_dataset_->var_num; ++k)
                    pixel_data[i][j][k] /= node_count[i][j];
            }

    VectorFieldData* vfd = new VectorFieldData(pixel_data);

    double step = sqrt(imw * imh / (double)SLIC_PIXEL_THRESHOLD);
    int nc = 1.0;

    Slic slic;
    slic.GenerateSuperPixels(vfd, step, nc);
    vector<vector<int>>& spixel_index = slic.GetClusters();
    vector<vector<float>>& spixel_centers = slic.GetCenters();
    vector<int> spixel_count;
    spixel_count.resize(spixel_centers.size(), 0);
    for (int i = 0; i < imh; ++i)
        for (int j = 0; j < imw; ++j) 
            spixel_count[spixel_index[j][i]] += node_count[i][j];

    vector<vector<float>> new_pos;
    vector<vector<float>> new_value;
    vector<int> spixel_to_node_index;

    float scale_rate = node->right - node->left;
    for (int i = 0; i < spixel_count.size(); ++i)
        if (spixel_count[i] != 0) {
            new_pos.push_back(vector<float>{spixel_centers[i][0] / (imw - 1) * scale_rate, spixel_centers[i][1] / (imh - 1) * scale_rate});
            vector<float> temp_value;
            for (int j = 2; j < spixel_centers[i].size(); ++j)
                temp_value.push_back(spixel_centers[i][j]);
            new_value.push_back(temp_value);
            spixel_to_node_index.push_back(new_pos.size() - 1);
        }
        else {
            spixel_to_node_index.push_back(-1);
        }


    vector<int> clusters;
    //float child_radius = max((node->right - node->left), (node->top - node->bottom)) / 2;
    //float child_radius = ((node->right - node->left) + (node->top - node->bottom)) / 4;
    //float child_radius = node->radius / factor_;
    float child_radius = sqrt((node->right - node->left) * (node->top - node->bottom) / EXPECTED_CLUSTER_NUM);
    SplitPoints(new_pos, new_value, child_radius, clusters);

    vector<CBranch*> label_nodes;
	label_nodes.resize(clusters.size(), NULL);
    for (int i = 0; i < pos.size(); i++) {
        int xi = (int)((pos[i][0] - node->left) / node_width * (imw - 1));
        int yi = (int)((pos[i][1] - node->bottom) / node_height * (imh - 1));
        int label = clusters[spixel_to_node_index[spixel_index[xi][yi]]];

        if (label_nodes[label] == NULL) {
			label_nodes[label] = new CBranch;
			label_nodes[label]->set_level(node->level() + 1);
			label_nodes[label]->radius = child_radius;
			label_nodes[label]->parent = node;
		}

		label_nodes[label]->linked_nodes.push_back(node->linked_nodes[i]);
		node->linked_nodes[i]->set_level(node->level() + 2);
		node->linked_nodes[i]->parent = label_nodes[label];
    }

	vector<CNode*> linked_nodes;
	for (int i = 0; i < clusters.size(); ++i)
		if (label_nodes[i] != NULL) {
			linked_nodes.push_back(label_nodes[i]);
		}
	for (int i = 0; i < linked_nodes.size(); ++i)
		ProgressNodeData(linked_nodes[i]);

	vector<int> tour_list;
	TourPathGenerator::GenerateRoundPath(linked_nodes, tour_list);
	node->linked_nodes.clear();
	for (int i = 0; i < tour_list.size(); ++i)
		node->linked_nodes.push_back(linked_nodes[tour_list[i]]);
}