在原始图像中的组件周围显示矩形
display rectangle around components in original image
我正在为 C++ 使用 OpenCV application.I正在为对象使用连接组件 detection.I 想在原始对象周围绘制一个矩形 frame.I 可以在组件中绘制矩形 window.can 我在灰度图像中画了一个彩色矩形
?在下面,我写了一部分 code.thanks 以供您帮助。
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
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];}
for (int i = 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));}
}
for (int i = 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);
如前所述here and ,您不能在灰度图像上绘制彩色矩形。您可以使用 Scalar(255, 255, 255) - white / Scalar(0, 0, 0) 或遵循第一个 link.[=11= 中的 hack ]
我正在为 C++ 使用 OpenCV application.I正在为对象使用连接组件 detection.I 想在原始对象周围绘制一个矩形 frame.I 可以在组件中绘制矩形 window.can 我在灰度图像中画了一个彩色矩形 ?在下面,我写了一部分 code.thanks 以供您帮助。
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
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];}
for (int i = 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));}
}
for (int i = 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);
如前所述here and