J'utilise OpenCV pour une application C++. J'utilise un composant connecté pour la détection d'objet.Je veux dessiner un rectangle autour de l'objet dans le cadre d'origine.Je peux dessiner un rectangle dans la fenêtre Comonent.can Je dessine un rectangle de couleur dans l'image de l'échelle de gris ? Dans ci-dessous j'écris une partie de mon code.thanks pour votre aide.rectangle d'affichage autour des composants dans l'image originale
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);
Vous ne pouvez pas dessiner le rectangle, car vous ne dessinez essentiellement pas un rectangle sur le cadre2 dans le code ci-dessus. –
Peut-être parce que frame2 est une échelle de gris? Avez-vous validé les valeurs dans stat avant de dessiner sur frame2? –
pour (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 rectangle (cadre2, Rect (x, y, w, h), scalaire (0, 255, 255));} '? –
louis89