Ask Your Question
0

Same function but different result

asked 2017-11-20 14:08:43 -0600

yode gravatar image

updated 2017-11-20 20:53:06 -0600

Cross post here


This is my private function for show those ROI with red color.

Mat highlight(Mat srcImg, Mat mask) {
    if (srcImg.size != mask.size) {
        cout << "Your two images tried to highlight have different SIZE.\n";
        exit(1);
    }
    if (mask.channels() != 1)
        cvtColor(mask, mask, COLOR_BGR2GRAY);
    if (mask.type() != CV_8UC1)
        mask.convertTo(mask, CV_8UC1);
    threshold(mask, mask, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);
    cvtColor(mask, mask, COLOR_GRAY2BGR);
    if (srcImg.channels() != 3)
        cvtColor(srcImg, srcImg, COLOR_GRAY2BGR);
    if (srcImg.type() != CV_8UC3)
        srcImg.convertTo(srcImg, CV_8UC3);
    dilate(mask - Scalar(0, 0, 255), mask, Mat(), Point(-1, -1), 2);
    return srcImg - mask;
}

This is my code.

#include<opencv.hpp>
using namespace std;
using namespace cv;
Mat highlight(Mat, Mat);
int main() {
    Mat emptyImg = imread("test.jpg", 0);
    Mat test = emptyImg.clone();

    Mat mask(5, 5, CV_8UC1, Scalar(0));
    for (int i = 0; i < mask.rows; i++) {
        uchar* data = mask.ptr<uchar>(i);
        for (int j = 0; j < mask.cols; j++)
            if ((i + j) % 2 == 0)
                data[j] = 255;
    }
    resize(mask, mask, emptyImg.size(), 0, 0, INTER_NEAREST);
    Mat img1, img2,mask1,mask2;
    img1 = highlight(emptyImg, mask);
    imshow("img1", img1);
    img2 = highlight(test, mask);
    imshow("img2", img2);

    waitKey();
    return 0;
}

And this is my test image. I'm very confused. Why I will get different result like following

As my expectation, the img1 and img2 should be same totally. Is there anything I have miss?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-11-20 22:06:28 -0600

Please note that Passing an OpenCV Mat is same as pass by reference, the only difference is that only the Mat headers will be copied separately! So your mask will be modified inside your function at dilate(). You can easily debug this by adding imshow before and after calling your highlight().

So please modify your code accordingly! For more details please refer this answer!

edit flag offensive delete link more

Comments

Thanks very much, I realize that now. :)

yode gravatar imageyode ( 2017-11-20 23:59:28 -0600 )edit

Could you tell me why the img is 8u still here?

yode gravatar imageyode ( 2018-11-23 03:13:21 -0600 )edit
0

answered 2017-11-20 21:07:27 -0600

Ken_du gravatar image

The Mat mask in the main function will be changed after the the function highlight be called.

Add an intermediate variable in the function highlight before mask is changed.

Mat highlight(Mat srcImg, Mat mask) {
    if (srcImg.size != mask.size) {
        cout << "Your two images tried to highlight have different SIZE.\n";
        exit(1);
    }
    Mat src, temp;
    if (mask.channels() != 1)
        cvtColor(mask, temp, COLOR_BGR2GRAY);
    if (mask.type() != CV_8UC1)
        mask.convertTo(temp, CV_8UC1);
    threshold(temp, temp, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);
    cvtColor(temp, temp, COLOR_GRAY2BGR);
    if (srcImg.channels() != 3)
        cvtColor(srcImg, src, COLOR_GRAY2BGR);
    if (srcImg.type() != CV_8UC3)
        srcImg.convertTo(src, CV_8UC3);
    dilate(temp- Scalar(0, 0, 255), temp, Mat(), Point(-1, -1), 2);
    return src- temp;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-11-20 14:08:43 -0600

Seen: 443 times

Last updated: Nov 20 '17