C++ 在框架上用opencv绘制一个矩形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13840703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drawing a rect with opencv on a frame
提问by Engine
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
知道为什么我会得到这个,我该如何解决它
回答by Niko
You're mixing up the C++ API with the C API. Use the rectangle function in the "cv" namespace instead of "cvRectangle":
您将 C++ API 与 C API 混为一谈。使用“cv”命名空间中的矩形函数而不是“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
// ...
回答by Harsh Bhatia
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();
}