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); }
void unarySortAdd(vec<Formula>& Xs,vec<Formula>& Ys,vec<Formula>& out_sorter,bool useShortCuts){ vec<Formula> propgationShortCuts; int XsLen = Xs.size(); int YsLen = Ys.size(); if (YsLen==0) { if (XsLen>0) { add(Xs,out_sorter); safeSortNetwork(out_sorter,propgationShortCuts); if (useShortCuts) overwrite(out_sorter,propgationShortCuts); } } else if (XsLen==0) add(Ys,out_sorter); else if (XsLen==1 & YsLen==1) { add(Xs,out_sorter); add(Ys,out_sorter); cmp2(out_sorter, 0); } else { int Ysize; for (Ysize = 2; Ysize < YsLen; Ysize *= 2); Ys.growTo(Ysize,_0_); propgationShortCuts.growTo(Ysize*2,_0_); addShortCuts(Ys,propgationShortCuts); if (XsLen <= YsLen) { Xs.growTo(Ysize,_0_); sortNetwork(Xs,propgationShortCuts); mergeNetwork(Xs,Ys,out_sorter,propgationShortCuts); } else { vec<vec<Formula> > SubXs; SubXs.push(); add(Ys,SubXs.last()); splitToGroups(Xs, Ysize, SubXs); sortGroups(SubXs,propgationShortCuts); mergeGroups(SubXs, Ysize, out_sorter,propgationShortCuts); } out_sorter.shrink(out_sorter.size() - XsLen-YsLen); if(useShortCuts) overwrite(out_sorter,propgationShortCuts); } }