c++ - display rectangle around components in original image -
i using opencv c++ application.i'm using connected component object detection.i want draw rectangle around object in original frame.i can draw rectangle in comonent window.can draw color rectangle in gray scale image ?in below write part of code.thanks help.
mat frame; mat stat, centroid; 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 = connectedcomponentswithstats(bw, labelimage, stat, centroid, 8); std::vector<vec3b> colors(nlabels); colors[0] = vec3b(0, 0, 0);//background (int label = 1; label < nlabels; ++label) { colors[label] = vec3b((rand() & 255), (rand() & 255), (rand() & 255));} @ dst(frame.size(), cv_8uc3); (int r = 0; r < dst.rows; ++r) { (int c = 0; c < dst.cols; ++c) { int label = labelimage.at<int>(r, c); vec3b &pixel = dst.at<vec3b>(r, c); pixel = colors[label];} (int = 0;i < nlabels;i++) { vector<rect> rcomp; rcomp.push_back(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))))); // rectangle(dst, 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 ))), scalar(0, 255, 255));} } (int = 0;i < nlabels;i++) { int x = stat.at<int>(i, cc_stat_left); int y = stat.at<int>(i, cc_stat_top); int w = stat.at<int>(i, cc_stat_width) ; int h = stat.at<int>(i, cc_stat_height); rectangle(frame, rect(x,y,w,h), scalar(0, 255, 255)); } } imshow("connected components", dst);
as mentioned here , here, can't draw colored rectangles on gray scale images. either use scalar(255, 255, 255) - white / scalar(0, 0, 0) or follow hack in first link.
Comments
Post a Comment