简体   繁体   中英

OpenCV traffic signal recognition error while building

I'm trying to use source code for http://moegelmose.com/p10/ for a project. I'm new to opencv and have minimum knowledge in C++. While building I get the following error,

 could not convert '0' from 'int' to 'Mat {aka cv::Mat}'
 void hueAdd(Mat image, int value, Mat mask = 0, int upperBound = 180);

The code in line 20 is,

void hueAdd(Mat image, int value, Mat mask = 0, int upperBound = 180);

The function call as follows;

void hueAdd(Mat image, int value, Mat mask, int upperBound) {
assert(image.type() == CV_8UC1);
assert(mask.type() == CV_8UC1);

for(int i = 0; i < image.rows; i++) {
    for(int j = 0; j < image.cols; j++) {
        if(mask.at<uchar>(i,j) == 0) {
            continue;
        }

        int result = (int)image.at<uchar>(i, j) + value; // Typecasting to make sure negative results can be saved.

        if(result > upperBound) {
            image.at<uchar>(i, j) = result-upperBound;
        } else if(result < 0) {
            image.at<uchar>(i, j) = upperBound+result;
        } else {
            image.at<uchar>(i, j) = result;
        }

    }
}
}

How can I fix it?

Use nullptr instead of 0 for Mat mask

Edit:

Use Mat::zeros(image.size(), image.type()) to create an empty mat of the same size and type as your image .

Use Mat::ones(image.size(), image.type()) to create a mask that will pass all the values through

I would change the function to work with pointers to the images:

void hueAdd(Mat* image, int value, Mat* mask = NULL, int upperBound = 180);

The code of the function would then look like:

void hueAdd(Mat* image, int value, Mat* mask, int upperBound) 
{
assert(image);
assert(image->type() == CV_8UC1);
assert(mask->type() == CV_8UC1);

for(int i = 0; i < image->rows; i++) {
    for(int j = 0; j < image->cols; j++) {
        if(mask && mask->at<uchar>(i,j) == 0) {
            continue;
        }
[...]

EDIT:

I removed the assert for the mask and instead added a check if the mask is present. This way the function works with and without a given mask. If no mask is given the whole image gets processed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM