[英]Visual Studio Release and Debug difference
I'm writing a code in which I read and image and process it and get a Mat of double/float
. 我正在编写一个代码,在其中读取和成像并处理它,并获得
Mat of double/float
的Mat of double/float
。 I'm saving it to a file and later on, I'm reading it from that file. 我将其保存到文件中,然后再从该文件中读取它。
When I use double, the space it requires is 8MB for 1Kx1K image, when I use float it is 4MB. 当我使用double时,用于1Kx1K图像所需的空间为8MB,而当使用float时则为4MB。 So I want to use
float
. 所以我想用
float
。
Here is my code and output: 这是我的代码和输出:
Mat data = readFloatFile("file_location");
cout << data.at<float>(0,0) << " " << data.at<double>(0,0);
When I run this code in the DEBUG mode
, the print out for float is -0
and double gives exception namely assertion failed
. 当我在
DEBUG mode
运行此代码时,float的打印输出为-0
并且double给出了异常,即assertion failed
。 But when I use RELEASE mode
the print out for float is -0
and 0.832 for double which is true value . 但是当我使用
RELEASE mode
时,float的打印输出为-0
, 而double的打印输出为0.832,这是真实值 。
My question is why I cant get output when I use data.at<float>(0,0)
and why I don't get exception when I use data.at<double>(0,0) in RELEASE mode
which is supposed to be the case? 我的问题是,为什么我在使用
data.at<float>(0,0)
时无法获得输出,为什么data.at<double>(0,0) in RELEASE mode
使用data.at<double>(0,0) in RELEASE mode
没有得到异常?是这样吗?
EDIT : Here is my code which writes and reads 编辑 :这是我写和读的代码
void writeNoiseFloat(string imageName,string fingerprintname) throw(){
Mat noise = getNoise(imageName);
FILE* fp = fopen(fingerprintname.c_str(),"wb");
if (!fp){
cout << "not found ";
perror("fopen");
}
float *buffer = new float[noise.cols];
for(int i=0;i<noise.rows;++i){
for(int j=0;j<noise.cols;++j)
buffer[j]=noise.at<float>(i,j);
fwrite(buffer,sizeof(float),noise.cols,fp);
}
fclose(fp);
free(buffer);
}
void readNoiseFloat(string fpath,Mat& data){
clock_t start = clock();
cout << fpath << endl;
FILE* fp = fopen(fpath.c_str(),"rb");
if (!fp)perror("fopen");
int size = 1024;
data.create(size,size,CV_32F);
float* buffer= new float[size];
for(int i=0;i<size;++i) {
fread(buffer,sizeof(float),size,fp);
for(int j=0;j<size;++j){
data.at<float>(i,j)=buffer[j];
cout << data.at<float>(i,j) << " " ;
cout << data.at<double>(i,j);
}
}
fclose(fp);
}
Thanks in advance, 提前致谢,
The first of all, you can not use the float
and double
in one cv::Mat
as storage itself is only array of bytes. 首先,您不能在一个
cv::Mat
使用float
和double
,因为存储本身只是字节数组。 Size of this array will be different for matrix of float
and matrix of double
. 对于
float
矩阵和double
矩阵,此数组的大小将有所不同。
So, you have to decide what you are using. 因此,您必须决定要使用什么。
Essentially, data.at<type>(x,y)
is equivalent to (type*)data_ptr[x][y]
(note this is not exact code, its purpose is to show what is happening) 本质上,
data.at<type>(x,y)
等效于(type*)data_ptr[x][y]
(请注意,这不是确切的代码,其目的是显示正在发生的事情)
EDIT: On the basis of code you added you are creating matrix of CV_32F
this means that you must use float
to write and read and element. 编辑:在您添加的代码的基础上,您正在创建
CV_32F
矩阵,这意味着您必须使用float
来进行读写和元素。 Using of double causes reinterpretation of value and will definitely give you an incorrect result. 使用double会导致重新解释价值,并且肯定会给您错误的结果。
Regarding to assertion, I am sure that inside the cv::MAT::at<class T>
there is a kind of following code: 关于断言,我确定在
cv::MAT::at<class T>
有一种以下代码:
assert(sizeof<T>==this.getDepth());
Usually asserts are compiled only in DEBUG
mode, so that's why you do not give this error in RELEASE
. 通常,仅在
DEBUG
模式下编译断言,因此这就是为什么您不在RELEASE
给出此错误的原因。
EDIT2: Not regarding to issue, but never use free()
with new
or delete
with malloc()
. EDIT2:不考虑发布,但切勿将
free()
与new
一起使用,或delete
与malloc()
。 The result can be a hardly debugging issue. 结果可能是很难调试的问题。
So please use delete[]
for buffer. 因此,请使用
delete[]
作为缓冲区。
Difference between debug & release: 调试和发布之间的区别:
There's a bug in your code. 您的代码中有一个错误。 It just doesn't show up in Release mode.
它只是没有显示在发布模式下。 That is what the debugger is for.
这就是调试器的用途。 Debugger tells you if there's any bug/issue with the code, Release just runs through it... Also the compiler optimizes your code to run faster and is therefore smaller, the debugger uses more size on your HD because you can actually DEBUG it.
调试器会告诉您代码中是否有任何错误/问题,Release会一直通过它运行...此外,编译器还会优化您的代码以使其运行更快,因此更小,调试器在HD上使用更大的大小,因为您实际上可以对其进行调试。
Release initializes your un-initialized variables to 0. This may vary on different compilers. Release将未初始化的变量初始化为0。这可能在不同的编译器上有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.