drawing a rect with opencv on a frame

c++, opencv

Solution

You're mixing up the C++ API with the C API. Use the rectangle function in the "cv" namespace instead of "cvRectangle":

cv::rectangle(
    frame,
    cv::Point(5, 10),
    cv::Point(20, 30),
    cv::Scalar(255, 255, 255)
);

Furthermore, you're trying to display the image in a window that you didn't open:

int main() {
    cv::namedWindow("test ");

    // ...

If the image did not load properly, this might also cause an error because you're then trying to draw onto an empty image.

if (frame.data != NULL) {
    // Image successfully loaded
    // ...

Problem

I have a frame and want to draw a rectangle in specefic position a rectangle with: ``` #include "opencv2/opencv.hpp" #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include<conio.h> int main () { cv::Mat frame = cv::imread("cmd.png"); cvRectangle( &frame, cvPoint(5,10), cvPoint(20,30), cvScalar(255,255,255) ); cv::imshow("test " , frame); while (cv::waitKey() != 23) ; return 1; } ``` wenn I run the code I get a memory error. ``` Unhandled exception at 0x000007fefd42caed in OpenCV_capture.exe: Microsoft C++ exception: cv::Exception at memory location 0x0018ead0.. ``` Any idea why do I get this, and how can I solve it

Original source