OpenCV 矩阵访问产生 EXC_BAD_ACCESS
当我使用高于 1.5 的scalefactory 运行以下程序时,该程序会抛出 EXC_BAD_ACCESS。 该程序缩放图像。
#include <iostream>
#include <OpenCV/cv.h>
#include <OpenCV/highgui.h>
#include <cmath>
using namespace std;
using namespace cv;
int scale (int xyvalue, float scalefactor) {
return ceil(xyvalue/scalefactor);
}
int main () {
Mat input;
float scalefactorx = 1.5;
float scalefactory = 1.5;
Mat output;
input = imread("/tmp/plum_grey_scale_MC_.low.jpg", 0);
output = cvCreateMat(input.size().height*scalefactory, input.size().width*scalefactorx, input.type());
for (int y = 0; y<output.size().height; y++)
{
for (int x = 0; x<output.size().width; x++)
{
output.data[y*output.size().width+x] = input.data[scale(y,scalefactory)*input.size().width + scale(x,scalefactorx)];
}
}
namedWindow("pic1", CV_WINDOW_AUTOSIZE);
imshow("pic1", output);
cvWaitKey(0);
cvDestroyWindow("BlomsterBillede");
return 0;
}
例如,如果我设置scalefactorx = 5,scalefactory = 2,它会在x=1356和y=409附近失败。
希望你能帮助我。
When I run the following program with scalefactory higher than 1.5, the program throws an EXC_BAD_ACCESS.
The program scales an image.
#include <iostream>
#include <OpenCV/cv.h>
#include <OpenCV/highgui.h>
#include <cmath>
using namespace std;
using namespace cv;
int scale (int xyvalue, float scalefactor) {
return ceil(xyvalue/scalefactor);
}
int main () {
Mat input;
float scalefactorx = 1.5;
float scalefactory = 1.5;
Mat output;
input = imread("/tmp/plum_grey_scale_MC_.low.jpg", 0);
output = cvCreateMat(input.size().height*scalefactory, input.size().width*scalefactorx, input.type());
for (int y = 0; y<output.size().height; y++)
{
for (int x = 0; x<output.size().width; x++)
{
output.data[y*output.size().width+x] = input.data[scale(y,scalefactory)*input.size().width + scale(x,scalefactorx)];
}
}
namedWindow("pic1", CV_WINDOW_AUTOSIZE);
imshow("pic1", output);
cvWaitKey(0);
cvDestroyWindow("BlomsterBillede");
return 0;
}
If e.g. I set scalefactorx = 5, scalefactory = 2, it fails around x=1356 and y=409.
Hope you can help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 OpenCV 不太熟悉,但在我看来,您在输入矩阵上读取的内容超出了范围。
我会写更像这样的东西:
我还没有测试过这个,但应该清楚它在做什么(而且它比一行索引更容易调试:)
祝你好运。
-汤姆
I'm not too familiar with OpenCV, but it looks to me like you're reading out of bounds on the input matrix.
I would write something more like this:
I've not tested this, but it should be clear what this is doing (Plus it's easier to debug than that one line mass of indexing :)
Good luck.
-Tom