gpt4 book ai didi

opencv - OpenCV可以用C++和C一起开发吗

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:25 24 4
gpt4 key购买 nike

OpenCV可以用C++和C一起开发吗?以下是我遇到这个问题的程序。它是用 C 编码的并且运行良好。但是如果我使用

cv::Mat saveImage=cv::imread("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");
cv::imshow("save",saveImage);

替换

IplImage* saveImge =cvLoadImage("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");
cvShowImage("saveimage",saveImge);

我遇到了这个:

TaskDemo.exe 中 0x75709673 处的未处理异常:Microsoft C++ 异常:cv::Exception at memory location 0x0039ea0c..

以下是整个程序。希望有人能帮助我。非常感谢

#include<opencv/cv.h>
#include<opencv/highgui.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/tracking.hpp"


#include <iostream>
#include <ctype.h>

CvPoint pt1 = cvPoint(0,0);
CvPoint pt2 = cvPoint(0,0);
bool is_selecting = false;

void cvMouseCallback(int mouseEvent,int x,int y,int flags,void* param)
{
switch(mouseEvent)
{
case CV_EVENT_LBUTTONDOWN:
pt1 = cvPoint(x,y);
pt2 = cvPoint(x,y);
is_selecting = true;
break;
case CV_EVENT_MOUSEMOVE:
if(is_selecting)
pt2 = cvPoint(x,y);
break;
case CV_EVENT_LBUTTONUP:
pt2 = cvPoint(x,y);
is_selecting = false;
break;
}
return;
}

int main(int argc,char* argv[])
{
char img_path[80] = "D:\\opencvStudy\\pic\\boldt.jpg";
char save_path[80] = "save.jpg";
char* window = "img";

IplImage* img = cvLoadImage(img_path);
IplImage* img_show = cvCloneImage(img);

cvNamedWindow(window,CV_WINDOW_AUTOSIZE);
cvSetMouseCallback(window,cvMouseCallback);

char text[80];
CvFont font;
cvInitFont(&font,CV_FONT_HERSHEY_PLAIN,1.0,1.0);
while(true)
{
cvCopy(img,img_show);
cvRectangle(img_show,pt1,pt2,cvScalar(255,255,255));
sprintf(text,"roi = cvRect(%d,%d,%d,%d)",pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y));
cvPutText(img_show,text,cvPoint(10,20),&font,cvScalar(0,0,255));

cvShowImage(window,img_show);
char key = cvWaitKey(10);
if (key='r')
{
cvSetImageROI(img,cvRect(pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y)));
cvSaveImage(save_path,img);
cvResetImageROI(img);
IplImage* saveImge = cvLoadImage("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");

cvShowImage("saveimage",saveImge);
//cv::Mat saveImage=cv::imread("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");
//cv::imshow("save",saveImage);
}
else if(key==27)
break;
}
cvReleaseImage(&img);
cvReleaseImage(&img_show);
return 0;

}

更新

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include <ctype.h>

using namespace std;
using namespace cv;
cv::Point pt1=Point(0,0);
cv::Point pt2=Point(0,0);
bool is_selecting=false;

static void onMouse(int mouseEvent,int x,int y,int flags,void* param)
{
switch(mouseEvent)
{
case CV_EVENT_LBUTTONDOWN://CV_EVENT_LBUTTONDOWN
pt1 = Point(x,y);
pt2 = Point(x,y);
is_selecting = true;
break;
case CV_EVENT_MOUSEMOVE:
if(is_selecting)
pt2 = Point(x,y);
break;
case CV_EVENT_LBUTTONUP:
pt2 = Point(x,y);
is_selecting = false;
break;
}
}

int main()
{
cv::Mat img=cv::imread("D:\\opencvStudy\\pic\\boldt.jpg");
cv::Mat img_show=img.clone();

cv::namedWindow("Task2",0);
cv::setMouseCallback("Task2",onMouse,0);

char text[80];
while(true)
{
img.copyTo(img_show);
cv::rectangle(img_show,pt1,pt2,cv::Scalar(255,255,255));
sprintf(text,"roi = cvRect(%d,%d,%d,%d)",pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y));
cv::putText(img_show,text,cvPoint(10,20),10,10,Scalar(0,0,255));
cv::imshow("Task2",img_show);
char key=cv::waitKey(10);
if (key='r')
{
cv::Mat save=img(cv::Rect(pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y)));
cv::imwrite("save.jpg",save);
cv::Mat saveImage=cv::imread("D:\\opencvStudy\\opencv_test\\TaskDemo\\TaskDemo\\save.jpg");
cv::imshow("save",saveImage);
}
else if(key==27)
break;
}
return 0;

最佳答案

问题是标题冲突。你不应该像这样同时包括旧的和新的:

#include<opencv/cv.h>
#include<opencv/highgui.h>
#include "opencv2/highgui/highgui.hpp"

相反,仅包含新 header ,如下所示:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

这是因为 C++ 头文件还包含 C 接口(interface)。

更新:

当然,如果您使用 cv::Mat,那么您也需要为此使用新函数。

CvCopy 变成cv::copy,不需要cvReleaseImage,cvRectangle 变成cv::rectangle,cvSetImageROI 变成img(rect),cvPoint 变成cv::Point cvRect 变成cv::RectcvCloneImage 变成 img.clone() 等等...

或者您需要从 cv::Mat 到 IplImage 的一些转换才能使用旧函数。有一些关于 SO 处理来回转换的问题。

更新 2:

最好删除所有 cv::,因为您正在使用 using namespace cv

然后检查带有旧 cv 前缀的函数和变量,并删除这些前缀。 (我仍然找到了一个:cvPoint)。

稍等,您正在使用 char text[80]!

我认为这又是问题的根源。最好换成

#include <sstream>
//...
stringstream ss;
ss << "Rect(" << pt1.x << ", " << ... << ")";
//...
putText(img_show, ss.str(), Point(10,20), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 255) );

请注意,字体(我使用的)不是 10,而是 FONT_HERSHEY_PLAIN(或其他一些 FONT_ 常量)。

关于opencv - OpenCV可以用C++和C一起开发吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13686606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com