Ask Your Question
-1

find connected component and create sub images for each component...

asked 2014-02-22 02:54:22 -0600

sujay gravatar image

updated 2014-02-23 03:28:19 -0600

trouble in getting subimage....!

problem in Mat subImage(output, cv::Rect(x1, y1, x2-x1, y2-y1)); imshow("sub", subImage);

not taking the correct parameters; unable to create subimages

 void FindBlobs(const Mat &binary,vector<vector<Point2i>>&blobs);

int main()
{
// load the image

Mat img = imread("C:\\Users\\xyz\\Desktop\\text.jpg");
if(!img.data) {
    cout << "File not found" << endl;
    return -1;
}
//Image scaling
Mat img_scale= imread("C:\\Users\\sujay\\Desktop\\texture.jpeg");
resize(img,img, img_scale.size());

// show it in a window
namedWindow( "Image", WINDOW_AUTOSIZE );
imshow("Image", img);

// image window will immediately disappear if the program ends, so
// we'll wait for a keypress, indefinitely
waitKey();

// first copy the image
Mat img_gray = img.clone();
cvtColor(img, img_gray, CV_RGB2GRAY);
imshow("Image", img_gray);
waitKey();

// do a simple transformation: convert to binary

// first copy the image
Mat img_binary = img.clone();

threshold(img_gray, img_binary, 128.0, 255.0, THRESH_BINARY_INV);
resize(img_binary,img_binary, img_scale.size());
imshow("Image", img_binary);
waitKey();

threshold(img_binary, img_binary, 0.0, 1.0, THRESH_BINARY);
resize(img_binary,img_binary, img_scale.size());

Mat output = Mat::zeros(img.size(), CV_8UC3);

vector < vector<Point2i > > blobs;
FindBlobs(img_binary, blobs);
int x1=0,x2=0,y1=0,y2=0;
// Randomy color the blobs
for(size_t i=0; i < blobs.size(); i++) {
    unsigned char r = 255 * (rand()/(1.0 + RAND_MAX));
    unsigned char g = 255 * (rand()/(1.0 + RAND_MAX));
    unsigned char b = 255 * (rand()/(1.0 + RAND_MAX));

    for(size_t j=0; j < blobs[i].size(); j++) {
        int x = blobs[i][j].x; 
        int y = blobs[i][j].y;
        if
        if(j == 0)
        {
            x1=x;
            x2=x;
            y1=y;
            y2=y;
        }
        else
        {
                if(x<x1)
                    x1=x;
                if(x>x2)
                    x2=x;
                if(y<y1)
                    y1=y;
                if(y>y2)
                    y2=y;
        }

        output.at<Vec3b>(y,x)[0] = b;
        output.at<Vec3b>(y,x)[1] = g;
        output.at<Vec3b>(y,x)[2] = r;
    }
    Mat subImage(output, cv::Rect(x1, y1, x2-x1, y2-y1));
    imshow("sub", subImage);
}
imshow("labelled", output);
waitKey(0);
return 0;
 }
 void FindBlobs(const cv::Mat &binary, std::vector < std::vector<cv::Point2i> > &blobs)
{
 blobs.clear();
 cv::Mat label_image;
 binary.convertTo(label_image, CV_32SC1);
 int label_count = 2; // starts at 2 because 0,1 are used already
 for(int y=0; y < label_image.rows; y++) {
    int *row = (int*)label_image.ptr(y);
    for(int x=0; x < label_image.cols; x++) {
        if(row[x] != 1) {
            continue;
        }
        cv::Rect rect;
        cv::floodFill(label_image, cv::Point(x,y), label_count, &rect, 0, 0, 4);
        std::vector <cv::Point2i> blob;

        for(int i=rect.y; i < (rect.y+rect.height); i++) {
            int *row2 = (int*)label_image.ptr(i);
            for(int j=rect.x; j < (rect.x+rect.width); j++) {
                if(row2[j] != label_count) {
                    continue;
                }
                blob.push_back(cv::Point2i(j,i));
            }
        }

        blobs.push_back(blob);

        label_count++;
    }
  }
}
edit retag flag offensive close merge delete

Comments

wall of code, but you forgot to ask an actual question ....

berak gravatar imageberak ( 2014-02-22 03:06:59 -0600 )edit

program getting break bcoz of exceptions not able to create subimages

sujay gravatar imagesujay ( 2014-02-23 03:30:11 -0600 )edit
  • what is that for-loop supposed to do ?
  • imshow("sub", subImage); inside a loop will get overwritten any time, so you only see the last result
  • why don't you just get the boundingRect() of the blob ?
berak gravatar imageberak ( 2014-02-23 04:20:31 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-02-24 02:39:54 -0600

Nghia gravatar image

cv::Rect(x1, y1, x2-x1, y2-y1) should be cv::Rect(x1, y1, x2-x1+1, y2-y1+1). Might not be the problem you are facing though ...

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-02-22 02:54:22 -0600

Seen: 1,111 times

Last updated: Feb 23 '14