2017-07-25 99 views
0

我使用对象detection.I连接部件使用OpenCV进行一个C++ application.I'm要绘制围绕原始frame.I目标的矩形可以绘制在comonent window.can矩形我在下面的灰阶图像 ?中绘制了一个彩色矩形,我写了部分代码。感谢您的帮助。显示矩形中的原始图像

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

,因为你基本上没有在上面的代码借鉴式2矩形不能绘制矩形。 –

+0

可能是因为frame2是灰度?在绘制frame2之前,您是否验证过统计值? –

+0

'for(int i = 0; i (i,CC_STAT_LEFT); \t \t \t INT Y = stat.at (I,CC_STAT_TOP); \t \t \t INT W = stat.at (I,CC_STAT_WIDTH); \t \t \t INT H = stat.at (I,CC_STAT_HEIGHT); \t \t \t矩形(帧2,矩形(X,Y,W,H),标量(0,255,255));}'? – louis89

回答

0

如前所述herehere,你不能用一个有颜色的灰度图像的矩形。您可以使用标量(255,255,255) - 白色/标量(0,0,0)或在第一个链接中使用黑客