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);
,因爲你基本上沒有在上面的代碼借鑑式2矩形不能繪製矩形。 –
可能是因爲frame2是灰度?在繪製frame2之前,您是否驗證過統計值? –
'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