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);
// Mat roi(frame, Rect(300, 320, 80, 80));
// Extract the L channel
vector<Mat> lab_planes(3);
split(frame, lab_planes);
// apply the CLAHE algorithm to the L channel
Ptr<CLAHE> clahe = createCLAHE();
clahe->setClipLimit(4);
Mat dst;
clahe->apply(lab_planes[0], dst);
// Merge the the color planes back into an Lab image
dst.copyTo(lab_planes[0]);
merge(lab_planes, frame);
namedWindow("Image", 1);
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;
}`