//================================================================================= /// DrawContoursAroundSegments /// /// Internal contour drawing option exists. One only needs to comment the if /// statement inside the loop that looks at neighbourhood. //================================================================================= void SLIC::DrawContoursAroundSegments( unsigned int** ubuff_p, int* labels, int& width, int& height, unsigned int& color ) { const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1}; unsigned int * ubuff = *ubuff_p; /* int sz = width*height; vector<bool> istaken(sz, false); int mainindex(0); for( int j = 0; j < height; j++ ) { for( int k = 0; k < width; k++ ) { int np(0); for( int i = 0; i < 8; i++ ) { int x = k + dx8[i]; int y = j + dy8[i]; if( (x >= 0 && x < width) && (y >= 0 && y < height) ) { int index = y*width + x; if( false == istaken[index] )//comment this to obtain internal contours { if( labels[mainindex] != labels[index] ) np++; } } } if( np > 1 )//change to 2 or 3 for thinner lines { ubuff[mainindex] = color; istaken[mainindex] = true; } mainindex++; } }*/ int sz = width*height; vector<bool> istaken(sz, false); vector<int> contourx(sz); vector<int> contoury(sz); int mainindex(0); int cind(0); for( int j = 0; j < height; j++ ) { for( int k = 0; k < width; k++ ) { int np(0); for( int i = 0; i < 8; i++ ) { int x = k + dx8[i]; int y = j + dy8[i]; if( (x >= 0 && x < width) && (y >= 0 && y < height) ) { int index = y*width + x; //if( false == istaken[index] )//comment this to obtain internal contours { if( labels[mainindex] != labels[index] ) np++; } } } if( np > 1 ) { contourx[cind] = k; contoury[cind] = j; istaken[mainindex] = true; //img[mainindex] = color; cind++; } mainindex++; } } int numboundpix = cind;//int(contourx.size()); for( int j = 0; j < numboundpix; j++ ) { int ii = contoury[j]*width + contourx[j]; ubuff[ii] = 0xffffff; for( int n = 0; n < 8; n++ ) { int x = contourx[j] + dx8[n]; int y = contoury[j] + dy8[n]; if( (x >= 0 && x < width) && (y >= 0 && y < height) ) { int ind = y*width + x; if(!istaken[ind]) ubuff[ind] = 0; } } } }
//================================================================================= /// DrawContoursAroundSegments /// /// Internal contour drawing option exists. One only needs to comment the if /// statement inside the loop that looks at neighbourhood. //================================================================================= void SLIC::DrawContoursAroundSegments( Mat& img, int*& labels, const int& width, const int& height, const unsigned int& color, int numlabel) { const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1}; int sz = width*height; dataset.resize(sz); segment.resize(numlabel); vector<bool> istaken(sz, false); vector<int> contourx(sz);vector<int> contoury(sz); int mainindex(0);int cind(0); for( int j = 0; j < height; j++ ) { for( int k = 0; k < width; k++ ) { int np(0); for( int i = 0; i < 8; i++ ) { int x = k + dx8[i]; int y = j + dy8[i]; if( (x >= 0 && x < width) && (y >= 0 && y < height) ) { int index = y*width + x; //if( false == istaken[index] )//comment this to obtain internal contours { if( labels[mainindex] != labels[index] ) np++; } } } if( np > 1 ) { int index1=j*width+k; dataset[index1].setMark(-1); dataset[index1].setX(k); dataset[index1].setY(j); dataset[index1].setIs_contour(1); int z=img.at<uchar>(j,k); dataset[index1].setZ(z); // contours.push_back(dataset[index1]); contourx[cind] = k; contoury[cind] = j; istaken[mainindex] = true; //img[mainindex] = color; cind++; segment[labels[index1]].push_back(dataset[index1]); } else{ int index1=j*width+k; dataset[index1].setMark(labels[index1]); dataset[index1].setX(k); dataset[index1].setY(j); dataset[index1].setIs_contour(0); int z=img.at<uchar>(j,k); dataset[index1].setZ(z); segment[labels[index1]].push_back(dataset[index1]); } mainindex++; } } char name[50]; int num=0; vector<vector<Node>>::iterator itv; vector<Node>::iterator itv2; cout<<segment.size()<<endl; for(itv=segment.begin();itv!=segment.end();itv++){ sprintf(name,"%.3d.txt",num); ofstream seg(name); for(itv2=itv->begin();itv2!=itv->end();itv2++){ seg<<itv2->getX()<<" "<<itv2->getY()<<" "<<itv2->getZ()<<" "<<itv2->getIs_contour()<<endl; } seg.close(); num++; } int numboundpix = cind;//int(contourx.size()); for( int j = 0; j < numboundpix; j++ ) { img.at<uchar>(contoury[j],contourx[j])=255; } }
void drawLabelContours(cv::Mat& image, cv::Mat& labels_cv) { int* labels = labels_cv.ptr<int>(0); const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1}; int width = image.cols; int height = image.rows; int sz = width*height; std::vector<bool> istaken(sz, false); std::vector<int> contourx(sz); std::vector<int> contoury(sz); int mainindex(0);int cind(0); for( int j = 0; j < height; j++ ) { for( int k = 0; k < width; k++ ) { int np(0); for( int i = 0; i < 8; i++ ) { int x = k + dx8[i]; int y = j + dy8[i]; if( (x >= 0 && x < width) && (y >= 0 && y < height) ) { int index = y*width + x; //if( false == istaken[index] )//comment this to obtain internal contours { if( labels[mainindex] != labels[index] ) np++; } } } if( np > 1 ) { contourx[cind] = k; contoury[cind] = j; istaken[mainindex] = true; //img[mainindex] = color; cind++; } mainindex++; } } int numboundpix = cind;//int(contourx.size()); for( int j = 0; j < numboundpix; j++ ) { image.at<cv::Vec3b>(contoury[j], contourx[j]) = cv::Vec3b(255,255,255); for( int n = 0; n < 8; n++ ) { int x = contourx[j] + dx8[n]; int y = contoury[j] + dy8[n]; if( (x >= 0 && x < width) && (y >= 0 && y < height) ) { if(!istaken[y*width + x]) image.at<cv::Vec3b>(y, x) = cv::Vec3b(0,0,0); } } } }
//================================================================================= /// DrawContoursAroundSegmentsTwoColors /// /// Internal contour drawing option exists. One only needs to comment the if /// statement inside the loop that looks at neighbourhood. //================================================================================= void SLIC::DrawContoursAroundSegmentsTwoColors( unsigned int* img, const int* labels, const int& width, const int& height) { const int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1}; int sz = width*height; vector<bool> istaken(sz, false); vector<int> contourx(sz); vector<int> contoury(sz); int mainindex(0); int cind(0); for( int j = 0; j < height; j++ ) { for( int k = 0; k < width; k++ ) { int np(0); for( int i = 0; i < 8; i++ ) { int x = k + dx[i]; int y = j + dy[i]; if( (x >= 0 && x < width) && (y >= 0 && y < height) ) { int index = y*width + x; //if( false == istaken[index] )//comment this to obtain internal contours { if( labels[mainindex] != labels[index] ) np++; } } } if( np > 1 ) { contourx[cind] = k; contoury[cind] = j; istaken[mainindex] = true; //img[mainindex] = color; cind++; } mainindex++; } } int numboundpix = cind;//int(contourx.size()); for( int j = 0; j < numboundpix; j++ ) { int ii = contoury[j]*width + contourx[j]; img[ii] = 0xffffff; //---------------------------------- // Uncomment this for thicker lines //---------------------------------- for( int n = 0; n < 8; n++ ) { int x = contourx[j] + dx[n]; int y = contoury[j] + dy[n]; if( (x >= 0 && x < width) && (y >= 0 && y < height) ) { int ind = y*width + x; if(!istaken[ind]) img[ind] = 0; } } } }