Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing a rect with opencv on a frame

Tags:

c++

opencv

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

like image 848
Engine Avatar asked Dec 12 '12 13:12

Engine


2 Answers

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
    // ...
like image 95
Niko Avatar answered Sep 18 '22 23:09

Niko


This Code works :

#include <opencv\cv.h>
#include <opencv\highgui.h>
int main()
{
//Window 
cvNamedWindow("Drawing",CV_WINDOW_AUTOSIZE);
//Image loading
IplImage* original=cvLoadImage("i.jpg");
 if(Original==NULL ) 
{
    puts("ERROR: Can't upload frame");
    exit(0);
}

cvRectangle(original,cvPoint(100,50),cvPoint(200,200),CV_RGB(255,0,0),5,8);

 //Showing the image
 cvShowImage("Drawing",original);

 cvWaitKey(0);
 //CleanUp
 cvReleaseImage(&original);
 cvDestroyAllWindows();

}
like image 41
Harsh Bhatia Avatar answered Sep 21 '22 23:09

Harsh Bhatia