gpt4 book ai didi

c++ - OpenCV中的段错误

转载 作者:行者123 更新时间:2023-12-02 16:58:48 26 4
gpt4 key购买 nike

我最近开始使用C++做一些图像处理工作,并且遇到了分割错误的问题。希望有人可以帮助我弄清楚发生了什么事?谢谢!!问题是下面附加的代码可以正常工作,但是如果声明了“vector roi_corners(4);”,更改为“ vector roi_corners;”然后使用“push_back()”和“clear()”来更新 vector ,将会得到分割错误。有人可以帮我弄清楚这个问题的原因吗?谢谢!!
OpenCV版本:4.4.0
MacOS版本:10.14.5
可行代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
#include <vector>

#define PI 3.14159265
#define WINDOW "Image Correction"

using namespace std;
using namespace cv;

vector< Point2f> roi_corners(4);
vector< Point2f> dst_corners(4);
Mat img1;
Mat cache;
int roi_id;

void On_mouse(int event, int x, int y, int flags, void*);


int main(int argc, char const *argv[])
{

//import image
roi_id = 0;
img1 = imread("Board.jpg");
if ( img1.empty() )
{
cerr << "Please Import an Image!!" << endl;
}
int factor = 60;//for pixel adjustment
cache = img1.clone();//copy for retake points
imshow(WINDOW, img1);


/*Run the point taking procedure*/
while(true){
setMouseCallback(WINDOW, On_mouse, 0);
char c = (char)waitKey( 10 );
if(c=='n') break;//press 'n' when determine the four point you want
if(c=='e') {roi_id=0; img1 = cache.clone();} //press 'e' to retake the foru point

}


/*For adjustment point estimation*/
dst_corners[0].x = roi_corners[0].x;
dst_corners[0].y = roi_corners[0].y;
dst_corners[1].x = roi_corners[0].x+factor*1;
dst_corners[1].y = roi_corners[0].y;
dst_corners[2].x = roi_corners[0].x+factor*1;
dst_corners[2].y = roi_corners[0].y+factor*1;
dst_corners[3].x = roi_corners[0].x;
dst_corners[3].y = roi_corners[0].y+factor*1;

Mat M = getPerspectiveTransform(roi_corners, dst_corners);
Mat warped_image;


/*Print the corrected picture*/
Size sz = cache.size();
warpPerspective(cache, warped_image, M, Size(sz.width, sz.height)); // do perspective transformation
imshow("Corrected Image", warped_image);
waitKey(0);
cout<<"complete"<<endl;
return 0;
}


void On_mouse(int event, int x, int y, int flags, void*)
{ imshow(WINDOW, img1);
if(roi_id<4){
if (event == EVENT_LBUTTONDOWN){
roi_corners[roi_id].x=x;
roi_corners[roi_id].y=y;
cout<<"The Point You Take is: "<<x<<' '<<y<<endl;
roi_id++;
circle(img1, Point(x,y), 2, Scalar(0, 0, 255), LINE_8 ,0);
imshow(WINDOW, img1);
}

}

}
如果我这样修改代码,它将显示段错误
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
#include <vector>

#define PI 3.14159265
#define WINDOW "Image Correction"

using namespace std;
using namespace cv;

vector< Point2f> roi_corners;
vector< Point2f> dst_corners;
Mat img1;
Mat cache;


void On_mouse(int event, int x, int y, int flags, void*);


int main(int argc, char const *argv[])
{

//import image
roi_id = 0;
img1 = imread("Board.jpg");
if ( img1.empty() )
{
cerr << "Please Import an Image!!" << endl;
}
int factor = 60;//for pixel adjustment
cache = img1.clone();//copy for retake points
imshow(WINDOW, img1);


/*Run the point taking procedure*/
while(true){
setMouseCallback(WINDOW, On_mouse, 0);
char c = (char)waitKey( 10 );
if(c=='n') break;//press 'n' when determine the four point you want
if(c=='e') {roi_corner.clear(); img1 = cache.clone();} //press 'e' to retake the foru point

}


/*For adjustment point estimation*/
dst_corners[0].x = roi_corners[0].x;
dst_corners[0].y = roi_corners[0].y;
dst_corners[1].x = roi_corners[0].x+factor*1;
dst_corners[1].y = roi_corners[0].y;
dst_corners[2].x = roi_corners[0].x+factor*1;
dst_corners[2].y = roi_corners[0].y+factor*1;
dst_corners[3].x = roi_corners[0].x;
dst_corners[3].y = roi_corners[0].y+factor*1;

Mat M = getPerspectiveTransform(roi_corners, dst_corners);
Mat warped_image;


/*Print the corrected picture*/
Size sz = cache.size();
warpPerspective(cache, warped_image, M, Size(sz.width, sz.height)); // do perspective transformation
imshow("Corrected Image", warped_image);
waitKey(0);
cout<<"complete"<<endl;
return 0;
}


void On_mouse(int event, int x, int y, int flags, void*)
{ imshow(WINDOW, img1);
if(roi_corners.size()<4){
if (event == EVENT_LBUTTONDOWN){
roi_corners.push_back(Point2f(x,y));
cout<<"The Point You Take is: "<<x<<' '<<y<<endl;
circle(img1, Point(x,y), 2, Scalar(0, 0, 255), LINE_8 ,0);
imshow(WINDOW, img1);
}

}

}

最佳答案

您的代码有几件事。您声明一个包含4个元素的 vector ,但不对其进行初始化。根据您的平台和数据类型,您可能会看到不希望的行为。尝试在同一行上声明和初始化 vector :

std::vector<cv::Point2f> roi_corners( 4, cv::Point2f(0.0, 0.0) );
当然,如果您使用初始容量( size)和初始值声明 vector ,则可以使用 std::vector<>::operator[]索引每个元素:
roi_corners[0] = cv::Point2f( 1.0, 2.0 );
roi_corners[1] = cv::Point2f( 3.0, 4.0 );
roi_corners[2] = cv::Point2f( 5.0, 6.0 );
roi_corners[3] = cv::Point2f( 7.0, 8.0 );
通过声明具有初始大小的 vector ,您已分配了用于 storeload vector 的各个元素的内存。现在,假设您不使用初始大小声明 vector ,而是使用 push_back在添加时添加元素:
//vector declaration with no initial size:
std::vector<cv::Point2f> roi_corners;

//store a new element into the vector:
roi_corners.push_back( cv::Point2f(1.0, 2.0) );
太酷了,您的 vector 已存储了一个新元素,并且仅显示一项的容量。但是,您仍然可以这样做:
roi_corners[0] = cv::Point2f( 1.0, 2.0 ); // data overwrite in position 0
roi_corners[1] = cv::Point2f( 3.0, 4.0 ); // you haven't allocated memory for this yet!

Result: seg fault
推论:如果您具有大小为 N的预定义 vector ,则可以通过 0将元素从 N索引到 std::vector<>::operator[],因为已分配了内存来容纳所有 N元素。如果您尝试寻址超出此范围的元素,则会看到 seg fault

关于c++ - OpenCV中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63405706/

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