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]; } } }
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); }
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); }
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); }
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; }