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