Display a Text on the windows with OpenCv and c++
Hi.I use below code for object detection:
static void on_trackbar(int, void*)
{
}
int main(int argc, char* argv[])
{
Mat frame;
Mat frame2 ;
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Cannot open the video cam" << endl;
return 0;
}
namedWindow("Image", 1);
createTrackbar("Threshold", "Image", &threshval, 255, on_trackbar);
while (1)
{
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
cvtColor(frame, frame2, COLOR_BGR2GRAY);
GaussianBlur(frame2, frame2, Size(5, 5), 0);
Canny(frame2, frame2, threshval, threshval * 2, 3);
morphologyEx(frame2, frame2, MORPH_CLOSE, Mat(), Point(-1, -1), 2);
Mat stat, centroid;
Mat bw = threshval < 128 ? (frame2< threshval) : (frame2 > threshval);
Mat labelImage(frame2.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));
}
Mat dst(frame2.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++)
{
if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10)
{
cout << "mitter"<<endl;
}
}
imshow("Image", frame2);
if (waitKey(1) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
} I want to have a text on window that show "Detection off" and when the condition if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) was fulfilled then the Text in the window change to "Detection On".I'm beginner in OpenCv and c++ and I have no idea to do this.I'm so grateful to help me do this
createTrackbar must be outside of while loop and ontrackbar too. I don't understand your program. You use threshold when you move trackbar only. Is it really what you want to do?
@LBerger thanks for your reply.This program works well for monochrome object, but it does not work well for an object with two colors (for example black and white).Do you have an idea to improve this program?
If you want to draw text on color or monochrome image use : putText function
@Ziri can I change text after a condition?
PutText draws text on image so if you modifie your text you'll draw it on next frame. If you want to change text on same frame you'll have to use an overlay image.
@Ziri but how?can you show me?I want to have a text on frame that show "Detection off" and when the condition if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) was fulfilled then the Text in the window change to "Detection On".I can use put text function and Display Detection OFF but Ican't change it after above condition
Did you try ?
if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) {
}
@Ziri yes.in this way I have two Text,that "Detection Off" is Always in the picture and "Detection On" while the condition was fulfilled,displayed on the frame.I want to change "OFF" to "ON"
if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) {
} else
{
}
@Ziri no.I try this but "Detection off" still exist when the condition was fulfilled.