Ask Your Question
0

connected components with opencv

asked 2017-07-21 14:40:52 -0600

louis89 gravatar image

updated 2017-07-23 03:04:08 -0600

I am using OpenCV for a C++ application.I want to use connected components in my code.my code is below.I have 3 question about this code that Thank you very much if you can help me solve them.

1-how can I find the center of mass of commponents and draw a rectangle around them??

2-how can I find the area of components??

3-how can I eliminated the big components and very small components??

thanks a lot..this is my code:`

Mat frame;
 int threshval = 100;
 static void on_trackbar(int, void*){
     Mat bw = threshval < 128 ? (frame < threshval) : (frame > threshval);
     Mat labelImage(frame.size(), CV_32S);
     int nLabels = connectedComponents(bw, labelImage, 8);
     std::vector<Vec3b> colors(nLabels);
          colors[0] = Vec3b(0, 0, 0);//background
       for (int label = 1; label < nLabels; ++label) {
     colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));}
     at dst(frame.size(), CV_8UC3);
 for (int r = 0; r < dst.rows; ++r) {
     for (int c = 0; c < dst.cols; ++c) {
int label = labelImage.at<int>(r, c);
Vec3b &pixel = dst.at<Vec3b>(r, c);

   pixel = colors[label];}}
   imshow("Connected Components", dst);}

static void help()
{

     cout << "\n This program demonstrates connected components and use of the trackbar\n"

         "Usage: \n"
             "   ./connected_components <image(../data/stuff.jpg as default)>\n"
        "The image is converted to grayscale and displayed, another image has a trackbar\n"
        "that controls thresholding and thereby the extracted contours which are drawn in color\n"; 
     };

 const char* keys =
 {
     "{help h||}{@image|../data/stuff.jpg|image for converting to a grayscale}"
 };
 int main(int argc, char* argv[])
 {
     VideoCapture cap("blowcontrast1.avi");
     if (!cap.isOpened())
     {
         cout << "Cannot open the video cam" << endl;
         return  0;

     }

     int totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);

     //namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);
     int frame_width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
     int frame_height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
    // VideoWriter writer;
    // writer.open("test.avi", -1, 20, frame.size(), 0);


     while (1)
     {
         bool bSuccess = cap.read(frame); // read a new frame from video


         if (!bSuccess)
         {
             cout << "Cannot read a frame from video stream" << endl;
             break;
         }


         // bool stop(false);
         // int delay = 10000;
         cv::Mat frame2 = frame.clone();
         cvtColor(frame, frame, COLOR_BGR2GRAY);
         GaussianBlur(frame, frame, Size(5, 5), 0);



         imshow("Image", frame);

         namedWindow("Connected Components", 1);
         createTrackbar("Threshold", "Connected Components", &threshval, 255, on_trackbar);
         on_trackbar(threshval, 0);

         if (waitKey(1) == 27)
         {
             cout << "esc key is pressed by user" << endl;
             break;
         }

     }
     return 0;
 }`
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-07-21 15:11:41 -0600

LBerger gravatar image

updated 2017-07-22 11:47:06 -0600

You should use connectedComponentsWithStat You will have rect, area and center in stats

Mat stat,centroid;
int nLabels = connectedComponents(bw, labelImage, stat,centroid,8);
vector<Rect> rComp;
for (int i=0;i<nLabels;i++)
{
   Rect r(Rect(Point(stat.at<int>(i,CC_STAT_LEFT ),stat.at<int>(i,CC_STAT_TOP)),Size(stat.at<int>(i,CC_STAT_WIDTH ),stat.at<int>(i,CC_STAT_HEIGHT)))
    rComp.push_back(r);
    rectangle(bw,r,Scalar::all(255),1);
    cout<<stat.at<int>(i,CC_STAT_AREA);
}
// you can draw all rect

eliminate what does it means?

edit flag offensive delete link more

Comments

@LBerger thanks a lot for your help.when I useint nLabels = connectedComponentsWithStats(bw, labelImage stat,centroid,8); I have to define stat and centroid.how can I do this?

louis89 gravatar imagelouis89 ( 2017-07-22 03:32:59 -0600 )edit

@LBerger thanks.I would appreciate if you help me to get area of components LBerger.

louis89 gravatar imagelouis89 ( 2017-07-22 04:48:51 -0600 )edit

may be there is no area less than 50 try wit 1000

LBerger gravatar imageLBerger ( 2017-07-22 11:42:50 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2017-07-21 14:40:52 -0600

Seen: 5,749 times

Last updated: Jul 23 '17