Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Same function but different result

This is my private function

Mat highlight(Mat srcImg, Mat mask, int fixsize) {
    if (fixsize == 1)
        resize(mask, mask, srcImg.size());
    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);
    //inRange(mask, Scalar(0),mean(mask), mask);
    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);
    //cout << srcImg.type();
    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 fixsize = 0);
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

Same function but different result

This is my private function

Mat highlight(Mat srcImg, Mat mask, int fixsize) mask) {
    if (fixsize == 1)
        resize(mask, mask, srcImg.size());
    if (srcImg.size != mask.size) {
        cout << "Your two images tried to highlight have different SIZE.\n";
        exit(1);
    }
    if (mask.channels() !=1)
!= 1)
        cvtColor(mask, mask, COLOR_BGR2GRAY);
    //inRange(mask, Scalar(0),mean(mask), mask);
    if(mask.type()!= 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);
    //cout << srcImg.type();
    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 fixsize = 0);
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

Same function but different result

Cross post here


This is my private functionfunction 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 codecode.

#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 same totally. Is there anything I have miss?

Same function but different result

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?