开发者

OpenCV matrix access produces EXC_BAD_ACCESS

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);

    cvWa开发者_开发百科itKey(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.


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:

float xt = static_cast<float>(x) / static_cast<float>(output.size().width);
float yt = static_cast<float>(y) / static_cast<float>(output.size().height);
int out_idx = y*output.size().width+x;
int in_idx = (yt*input.size().height)*input.size().width + (xt*input.size().width);
output.data[out_idx] = input.data[in_idx];

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜