Mat PreGraph::GeneSp(const Mat &img) { int width = img.cols; int height = img.rows; int sz = width*height; UINT *reimg = new UINT[sz * 3]; for (int c = 0; c<3; c++) { for (int i = 0; i<width; i++) { for (int j = 0; j<height; j++) reimg[c*(width*height) + i*height + j] = saturate_cast<unsigned int>(img.at<Vec3b>(j, i)[2 - c]); } } int* label = nullptr; SLIC slic; slic.DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(reimg, height, width, label, spNum, spNumMax, compactness); Mat superpixels(img.size(), CV_16U); for (int i = 0; i<superpixels.rows; i++) { for (int j = 0; j<superpixels.cols; j++) { superpixels.at<ushort>(i, j) = label[i + j*superpixels.rows]; } } delete [] reimg; delete [] label; return superpixels; }
void BasicImage::SLICSuperPixel(int pnum, double compact) { int m = OriginalImage.channels(); int c = OriginalImage.cols; int r = OriginalImage.rows; pbuff = new unsigned int[r*c]; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { pbuff[i*c+j] = (unsigned int)OriginalImage.at<Vec3b>(i,j)[2]*pow(2,16) + (unsigned int)OriginalImage.at<Vec3b>(i,j)[1]*pow(2,8) + (unsigned int)OriginalImage.at<Vec3b>(i,j)[0]; } } SLIC segment; int* klabels = NULL; int numlabels(0); segment.DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(pbuff, c, r, klabels, numlabels, pnum, compact); std::vector<std::vector<Point>> unsorted_kernal_groups; unsorted_kernal_groups.resize(numlabels); cutted = OriginalImage.clone(); for (int i = 1; i < r-1; i++) { for (int j = 1; j < c-1; j++) { int n0 = klabels[i*c+j]; int n1 = klabels[i*c+j-1]; int n2 = klabels[i*c+j+1]; int n3 = klabels[(i+1)*c+j]; int n4 = klabels[(i-1)*c+j]; int n5 = klabels[(i+1)*c+j+1]; int n6 = klabels[(i+1)*c+j-1]; int n7 = klabels[(i-1)*c+j+1]; int n8 = klabels[(i-1)*c+j-1]; if (n0 != n1 || n0 != n2 || n0 != n3 || n0 != n4 || n0 != n5 || n0 != n6 || n0 != n7 || n0 != n8 ) { cutted.at<Vec3b>(i,j)[0] = 0; cutted.at<Vec3b>(i,j)[1] = 0; cutted.at<Vec3b>(i,j)[2] = 188; int index = klabels[i*c+j]; unsorted_kernal_groups[index].push_back(Point(i,j)); } } } sortGroups(unsorted_kernal_groups); imwrite("cutted.png",cutted); }
Mat GMRsaliency::GetSup(const Mat &image) { int width=image.cols; int height=image.rows; int sz = width*height; UINT *img=new UINT[sz*3]; for(int c=0;c<3;c++) { for(int i=0;i<width;i++) { for(int j=0;j<height;j++) img[c*(width*height)+i*height+j]=saturate_cast<unsigned int>(image.at<Vec3b>(j,i)[2-c]); } } int* labels = new int[sz]; SLIC slic; slic.DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(img, height, width, labels, spcounta, spcount, compactness); Mat supLab(image.size(),CV_16U); for(int i=0;i<supLab.rows;i++) { for(int j=0;j<supLab.cols;j++) { supLab.at<ushort>(i,j)=labels[i+j*supLab.rows]; } } if(labels) delete [] labels; if(img) delete []img; return supLab; }