mercredi 29 juin 2016

Null Data field in cv::Mat object


I am quite new at OpenCV. I am running into quite a puzzle ( from my naive perspective...)

I am trying to set a region of a zero matrix to ones. In essence do the following:

Mat a = Mat::zeros(10, 10, CV_8UC1);
Mat b = Mat::ones(3, 3, CV_8UC1);

Range h = Range(2, 5);
Range w = Range(2, 5);

b.copyTo(a(h, w));

I've checked the output of this exact code, and it works fine. The problem comes in when i try to do this in my actual code:

int key, top,left,bottom,right;
Mat blackImg = Mat::zeros(imgHeight, imgWidth, CV_8UC1);
Mat whiteBox = Mat::ones(patternHeight, patternWidth, CV_8UC1);
while (true) {
    blackImg = Mat::zeros(patternHeight, patternWidth, CV_8UC1);

    top = patternPosY;
    bottom = patternPosY + patternHeight;
    left = patternPosX;
    right = patternPosX + patternWidth;

    Range h = Range(top, bottom);
    Range w = Range(left, right);

    whiteBox.copyTo(blackImg(h, w));

    imshow("Pattern", blackImg);
    // key inputs
    key = waitKey(30);

    if (key == 27) {
        break;
    }
}

However, the data field of the blackImg Mat object is NULL and remains so. This in turns leads to a memory error obviously. I have checked the values of top, bottom, right, and left and they are within bounds.

I am sure I'm missing something basic, and it would be infinitely helpful if someone could point it out.


Aucun commentaire:

Enregistrer un commentaire