gpt4 book ai didi

opencv - opencv r6010 abort()在Visual Studio 2013中被称为错误

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

我有一些代码可以在鼠标选择的图像上的两个点之间绘制一条线,然后显示直方图。
但是,当我按代码要求按q时,出现一条错误消息,提示已调用R6010 abort(),并显示VC++运行时错误。

请告诉我如何找到此错误。

#include <vector>
#include "opencv2/highgui/highgui.hpp"
#include <opencv\cv.h>
#include <iostream>
#include<conio.h>

using namespace cv;
using namespace std;

struct Data_point
{
int x;
unsigned short int y;
};

int PlotMeNow(unsigned short int *values, unsigned int nSamples)
{
std::vector<Data_point> graph(nSamples);

for (unsigned int i = 0; i < nSamples; i++)
{
graph[i].x = i;
graph[i].y = values[i];
}

cv::Size imageSize(5000, 500); // your window size
cv::Mat image(imageSize, CV_8UC1);

if (image.empty()) //check whether the image is valid or not
{
std::cout << "Error : Image cannot be created..!!" << std::endl;
system("pause"); //wait for a key press
return 0;
}
else
{
std::cout << "Good job : Image created successfully..!!" << std::endl;
}

// tru to do some ofesseting so the graph do not hide on x or y axis
Data_point dataOffset;

dataOffset.x = 20;
// we have to mirror the y axis!
dataOffset.y = 5000;

for (unsigned int i = 0; i<nSamples; ++i)
{
graph[i].x = (graph[i].x + dataOffset.x) * 3;
graph[i].y = (graph[i].y + dataOffset.y) / 200;
}

// draw the samples
for (unsigned int i = 0; i<nSamples - 1; ++i)
{
cv::Point2f p1;
p1.x = graph[i].x;
p1.y = graph[i].y;
cv::Point2f p2;
p2.x = graph[i + 1].x;
p2.y = graph[i + 1].y;
cv::line(image, p1, p2, 'r', 1, 4, 0);
}

cv::namedWindow("MyWindow1", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
cv::imshow("MyWindow1", image); //display the image which is stored in the 'img' in the "MyWindow" window
while (true)
{
char c = cv::waitKey(10);
if (c == 'q')
break;
}
destroyWindow("MyWindow1");
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}



void IterateLine(const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int* count1)
{
LineIterator it(image, p2, p1, 8);

for (int i = 0; i < it.count; i++, it++)
{
linePixels.push_back(image.at<ushort>(it.pos())); //doubt
}

*count1 = it.count;
}


//working line with mouse
void onMouse(int evt, int x, int y, int flags, void* param)
{
if (evt == CV_EVENT_LBUTTONDOWN)
{
std::vector<cv::Point>* ptPtr = (std::vector<cv::Point>*)param;
ptPtr->push_back(cv::Point(x, y));
}
}


void drawline(Mat image, std::vector<Point>& points)
{
cv::namedWindow("Output Window");
cv::setMouseCallback("Output Window", onMouse, (void*)&points);

int X1 = 0, Y1 = 0, X2 = 0, Y2 = 0;

while (1)
{
cv::imshow("Output Window", image);

if (points.size() > 1) //we have 2 points
{
for (auto it = points.begin(); it != points.end(); ++it)
{
}
break;
}
waitKey(10);
}


//just for testing that we are getting pixel values
X1 = points[0].x;
X2 = points[1].x;

Y1 = points[0].y;
Y2 = points[1].y;

// Draw a line
line(image, Point(X1, Y1), Point(X2, Y2), 'r', 2, 8);
cv::imshow("Output Window", image);

//exit image window
while (true)
{
char c = cv::waitKey(10);
if (c == 'q')
break;
}
destroyWindow("Output Window");
}


void show_histogram_image(Mat img1)
{
int sbins = 65536;
int histSize[] = { sbins };
float sranges[] = { 0, 65536 };
const float* ranges[] = { sranges };
cv::MatND hist;
int channels[] = { 0 };
cv::calcHist(&img1, 1, channels, cv::Mat(), // do not use mask
hist, 1, histSize, ranges,
true, // the histogram is uniform
false);

double maxVal = 0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
int xscale = 10;
int yscale = 10;

cv::Mat hist_image;
hist_image = cv::Mat::zeros(65536, sbins*xscale, CV_16UC1);

for int s = 0; s < sbins; s++)
{
float binVal = hist.at<float>(s, 0);
int intensity = cvRound(binVal * 65535 / maxVal);

rectangle(hist_image, cv::Point(s*xscale, hist_image.rows),
cv::Point((s + 1)*xscale - 1, hist_image.rows - intensity),
cv::Scalar::all(65535), 1);

}


imshow("Histogram", hist_image);
waitKey(0);
}


int main()
{
vector<Point> points1;
vector<ushort>linePixels;

Mat img = cvLoadImage("desert.jpg");

if (img.empty()) //check whether the image is valid or not
{
cout << "Error : Image cannot be read..!!" << endl;
system("pause"); //wait for a key press
return -1;
}
//Draw the line
drawline(img, points1);

//now check the collected points
Mat img1 = cvLoadImage("desert.jpg");

if (img1.empty()) //check whether the image is valid or not
{
cout << "Error : Image cannot be read..!!" << endl;
system("pause"); //wait for a key press
return -1;
}

int *t = new int;
IterateLine( img1, linePixels, points1[1], points1[0], t );
PlotMeNow(&linePixels[0], t[0]);

show_histogram_image(img);
delete t;
_getch();
return 0;
}

最佳答案

这是代码中的不良气味之一:

void IterateLine(const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int* count1)
{
...
linePixels.push_back(image.at<ushort>(it.pos())); //doubt

现在 image是一个CV_8UC3图像(来自 Mat img1 = cvLoadImage("desert.jpg");,但是您像CV_16UC1一样在这里访问,因此 linePixels中放入的内容是垃圾。这几乎肯定会导致 PlotMeNow()绘制其图像之外并破坏某些内容,这可能就是为什么您的代码崩溃了

确实,您的代码正在尝试执行的操作尚不清楚,我不能建议您在这里使用什么。

关于opencv - opencv r6010 abort()在Visual Studio 2013中被称为错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24851305/

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