c++ - 在原始图像中的组件周围显示矩形

标签 c++ opencv connected-components

我正在将 OpenCV 用于 C++ 应用程序。我正在使用连接组件进行对象检测。我想在原始框架中围绕对象绘制一个矩形。我可以在组件窗口中绘制矩形。我可以用灰色绘制彩色矩形吗缩放图像 ?在下面我写了我的部分代码。感谢您的帮助。

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);

最佳答案

如前所述herehere ,您不能在灰度图像上绘制彩色矩形。您可以使用 Scalar(255, 255, 255) - white/Scalar(0, 0, 0) 或遵循第一个链接中的hack

关于c++ - 在原始图像中的组件周围显示矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45300784/

相关文章:

c++ - 一个pthread程序在收到条件信号后无法跳出循环

c++ - C++ 中 vector 和 while 循环的初学者困难

c++ - 在 windows 7 32 位上使用 openssl 1.0.2d 和 visual studio 2010 在 c++ 中实现 TLS1.2 连接

c++ - OpenCV - 从图像分割树

c++ - vector 构造函数/析构函数调用

c# - 视频文件中的对象跟踪

c# - 有没有办法使用与OpenCv的MinMaxLoc类似的方法来获取n个最大值的列表,而不是仅一个最大值?

python-2.7 - python中的连接组件标记

algorithm - 删除k个顶点后的连通分量数

c++ - 连通分量程序产生不正确的输出