未处理的异常...访问冲突读取位置
Unhandled exception at ... Access violation reading location
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv2\core\core.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
int i, M;
Mat ycrcb, rgb, vect, Data;
vector < String > files;
/*Names of the pictures*/
glob("C:\Users\lenovo\Desktop\cubicle\trainning\*.jpg", files); // M=number of training images
M = files.size();
// calculatong of the matrix Data
for (i = 0; i < M; i++)
{
// Lecture of RGB image
rgb = imread(files[i]);
namedWindow("RGB image", WINDOW_AUTOSIZE);
imshow("RGB image", rgb);
waitKey(10);
if (i == 0)
{ //for the first iteration
Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
}
rgb.convertTo(rgb, CV_32FC3, 1.0 / 255.0);
// Convert to float // Convert the RGB color space to the color space Ycrcbb*/
cvtColor(rgb, ycrcb, CV_BGR2YCrCb);
//making each image a vector line
rgb = rgb.reshape(1, rgb.total() * 3);
ycrcb = ycrcb.reshape(1, ycrcb.total() * 3);
/*Concatenate rgb and ycrcb*/
hconcat(rgb, ycrcb, vect);
fprintf(stdout,
"rgb=[%d,%d] , ycrcb=[%d,%d], vect=[%d,%d\n",
rgb.rows,
rgb.cols,
ycrcb.rows,
ycrcb.cols,
vect.rows,
vect.cols);
vect.copyTo(Data.row(i));
}
int nclusters = 35;
Mat labels, centers(nclusters, Data.cols, CV_32FC1);
/* clustering Data by kmeans*/
kmeans(Data,
nclusters,
labels,
TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 0.65, 200),
3,
KMEANS_PP_CENTERS,
centers);
}
这是完整的代码,我遇到了错误:
Unhandled exception at 0x00b85c10 in PB.exe: 0xC0000005: Access violation reading location 0xc35de59f.
我对 OpenCV 一无所知,但这看起来有问题:
int main(int argc, char** argv)
{
int i, M;
Mat ycrcb, rgb, vect, Data;
刚刚定义了 Mat
类型的变量 Data
。不幸的是,这是完全合理的做法
if (i == 0)
{ //for the first iteration
Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
}
制作了第二个 Mat Data
,它隐藏了较早的那个并且仅存在于 if
的主体范围内。当内部 Data
超出范围时,设置此 Data
所做的所有工作都将在右大括号处丢弃。在 if
主体之外,原始 Data
仍处于活动状态且可访问,但从未正确初始化。它的使用有些问题,所以当
vect.copyTo(Data.row(i));
已达到 ,Data
可能没有可将 vect
复制到其中的行。 Data
的后续使用同样值得怀疑,任何一个都可能导致段错误。
我的建议是在所有数据可用之前推迟 Data
的创建。
既然你有一个非常简单的功能,改变
Mat ycrcb, rgb, vect, Data;
至
Mat ycrcb, rgb, vect;
并替换
if (i == 0)
{ //for the first iteration
Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
}
与
static Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1);
可能就是你需要的。
Read up on Static local variables here.
编辑
static
局部变量有点奇怪。它们是“有点”全球性的。它们的生命周期从第一次使用到程序终止,但只在定义它们的范围内可见。
如果定义在一个将被多次调用的函数中,static
变量将被分配并初始化一次。不是每次调用一次。随后进入变量范围的入口将从最后一个入口剩下的任何地方开始,这正是本例中所需要的。但是对于一个较大的程序多次调用一个函数,这个方案需要慎用。
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv2\core\core.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
int i, M;
Mat ycrcb, rgb, vect, Data;
vector < String > files;
/*Names of the pictures*/
glob("C:\Users\lenovo\Desktop\cubicle\trainning\*.jpg", files); // M=number of training images
M = files.size();
// calculatong of the matrix Data
for (i = 0; i < M; i++)
{
// Lecture of RGB image
rgb = imread(files[i]);
namedWindow("RGB image", WINDOW_AUTOSIZE);
imshow("RGB image", rgb);
waitKey(10);
if (i == 0)
{ //for the first iteration
Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
}
rgb.convertTo(rgb, CV_32FC3, 1.0 / 255.0);
// Convert to float // Convert the RGB color space to the color space Ycrcbb*/
cvtColor(rgb, ycrcb, CV_BGR2YCrCb);
//making each image a vector line
rgb = rgb.reshape(1, rgb.total() * 3);
ycrcb = ycrcb.reshape(1, ycrcb.total() * 3);
/*Concatenate rgb and ycrcb*/
hconcat(rgb, ycrcb, vect);
fprintf(stdout,
"rgb=[%d,%d] , ycrcb=[%d,%d], vect=[%d,%d\n",
rgb.rows,
rgb.cols,
ycrcb.rows,
ycrcb.cols,
vect.rows,
vect.cols);
vect.copyTo(Data.row(i));
}
int nclusters = 35;
Mat labels, centers(nclusters, Data.cols, CV_32FC1);
/* clustering Data by kmeans*/
kmeans(Data,
nclusters,
labels,
TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 0.65, 200),
3,
KMEANS_PP_CENTERS,
centers);
}
这是完整的代码,我遇到了错误:
Unhandled exception at 0x00b85c10 in PB.exe: 0xC0000005: Access violation reading location 0xc35de59f.
我对 OpenCV 一无所知,但这看起来有问题:
int main(int argc, char** argv)
{
int i, M;
Mat ycrcb, rgb, vect, Data;
刚刚定义了 Mat
类型的变量 Data
。不幸的是,这是完全合理的做法
if (i == 0)
{ //for the first iteration
Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
}
制作了第二个 Mat Data
,它隐藏了较早的那个并且仅存在于 if
的主体范围内。当内部 Data
超出范围时,设置此 Data
所做的所有工作都将在右大括号处丢弃。在 if
主体之外,原始 Data
仍处于活动状态且可访问,但从未正确初始化。它的使用有些问题,所以当
vect.copyTo(Data.row(i));
已达到 ,Data
可能没有可将 vect
复制到其中的行。 Data
的后续使用同样值得怀疑,任何一个都可能导致段错误。
我的建议是在所有数据可用之前推迟 Data
的创建。
既然你有一个非常简单的功能,改变
Mat ycrcb, rgb, vect, Data;
至
Mat ycrcb, rgb, vect;
并替换
if (i == 0)
{ //for the first iteration
Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
}
与
static Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1);
可能就是你需要的。
Read up on Static local variables here.
编辑
static
局部变量有点奇怪。它们是“有点”全球性的。它们的生命周期从第一次使用到程序终止,但只在定义它们的范围内可见。
如果定义在一个将被多次调用的函数中,static
变量将被分配并初始化一次。不是每次调用一次。随后进入变量范围的入口将从最后一个入口剩下的任何地方开始,这正是本例中所需要的。但是对于一个较大的程序多次调用一个函数,这个方案需要慎用。