Reputation: 407
I'm trying to make a simple program that draws a rectangle over a video stream coming from my webcam. The following code compiles and runs, but the rectangle isn't visible. I've tried various line thicknesses, colors, and positions; as well as tried to put rectangles simply on images rather than a video stream.
After looking through examples and tutorials as well as the OpenCV docs, I still can't seem to figure it out. If anyone could assist me in making the rectangle visible, it would be greatly appreciated.
#include <opencv2/video.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
VideoCapture vid(0);
Mat frame;
int main()
{
while(true)
{
vid.read(frame);
imshow("Webcam", frame);
rectangle(frame, Point(100, 100), Point(300, 300), Scalar(255), 10, 8, 0);
if (waitKey(30) == 27)
break;
}
}
Upvotes: 2
Views: 1719
Reputation: 41765
Simply draw the rectangle before you show the image:
#include <opencv2\opencv.hpp> // It's just easier to #include only this
using namespace cv;
int main() {
// Don't use global variables if they are not needed!
VideoCapture vid(0);
Mat frame;
while(true)
{
// Read frame
vid.read(frame);
// Draw rectangle
rectangle(frame, Point(100, 100), Point(300, 300), Scalar(255, 0, 0) /*blue*/, 10, 8, 0);
// Show image
imshow("Webcam", frame);
if ((waitKey(30) & 0xFF) == 27) { // for portability
break;
}
}
}
Upvotes: 2