c++ - Connected Component Algorithm Not Differentiating Between Objects -
i trying write program takes binary image , uses sequential labeling algorithm label each object , color them shade gray different each other.
i believe program differentiating alright, not color them different colors. using union/find algorithm combine labels.
any great:
#include "image.h" #include "image.cpp" #include "pgm.cpp" #include "disjsets.h" #include "disjsets.cpp" #include <stdlib.h> #include <stdio.h> #include <iostream> using namespace std; int main(int argc, char **argv){ if(argc != 3){ std::cout << "use p2 <input image> <output image name>" << std::endl; return 0; } image *im; im = new image; if(readimage(im, argv[1]) != 0){ cout << "file " << argv[1] << " not opened." << endl; return 0; } int labels[im->getnrows()][im->getncols()]; int linked[im->getnrows()]; for(int x = 0; x < im->getnrows(); x++){ for(int y = 0; y < im->getncols(); y++){ labels[x][y] = 0; //fill labels array linked[x] = 0; } } int current_label = 1; disjsets *disj; disj = new disjsets(im->getnrows()); int rows = im->getnrows(); int cols = im->getncols(); for(int r = 0; r < rows; r++) for(int c = 0; c < cols; c++){ if(r-1<0 || c-1<0){ r++; c++; continue; } if(im->getpixel(r,c) == 1){ if(im->getpixel(r-1, c) == 0 && im->getpixel(r, c-1) == 0) { if(labels[r][c] == 0 && labels[r-1][c] == 0 && labels[r][c-1] == 0){ linked[current_label] = current_label; labels[r][c] = current_label; current_label += 1; } }else{ if((im->getpixel(r-1, c) == 1) && im->getpixel(r, c-1) == 0){ labels[r][c] = labels[r-1][c]; } else if((im->getpixel(r-1, c) == 0) && im->getpixel(r, c-1) == 1){ labels[r][c] = labels[r][c-1]; } else if((im->getpixel(r-1, c) == 1) && im->getpixel(r, c-1) == 1){ if(labels[r-1][c] == labels[r][c-1] && labels[r-1][c] != 0 && labels[r][c-1] != 0){ labels[r][c] = labels[r-1][c]; } else { if(labels[r][c-1] < labels[r-1][c] && labels[r-1][c] != 0 && labels[r][c-1] != 0){ labels[r][c] = labels[r][c-1]; linked[labels[r][c]] = disj->unionsets(linked[labels[r][c]], labels[r-1][c]); } else if(labels[r][c-1] > labels[r-1][c] && labels[r-1][c] != 0 && labels[r][c-1] != 0){ labels[r][c] = labels[r-1][c]; linked[labels[r][c]] = disj->unionsets(linked[labels[r][c]], labels[r][c-1]); } } } } } } for(int r = 0; r < im->getnrows(); r++){ for(int c = 0; c < im->getncols(); c++){ if(labels[r][c] != labels[r+1][c+1] && labels[r][c] != labels[r+1][c] && labels[r][c+1]) cout << labels[r][c] << " "; if(im->getpixel(r,c) == 1){ labels[r][c] = disj->find(linked[labels[r][c]]); if(labels[r][c] < 30){ im->setpixel(r,c,1); } else if(labels[r][c] > 30) im->setpixel(r,c,2); } } } if(writeimage(im, argv[2]) != 0){ cout << "cannot write file: " << argv[2] << endl; return 0; } return 0; } edit: ok turns out, if print labels array, of labels 0. can explain why happening?
edit2: fixed fill labels array, not sure how go doing pixel color changing. updated code
Comments
Post a Comment