Problem with calculations on Mat-Array with Windows 10

asked 2018-06-04 04:08:00 -0600

Pauli gravatar image

updated 2018-06-04 09:36:50 -0600

Hallo, I have a very mysterious problem with calculations on Mat-Arrays under Windows 10. Following code produce a black area on the output image under Windows 10. I don't know where it come from. When I run this code under windows 7 the image is ok, and no black area is to see. The effect has obscure dependency of the parameters MaxIgnore and MinIgnore and the input Image. What is wrong with my code, or is it a problem of the OpenCV Library itself? I use OpenCV 3.4.1, Visual Studio 2017, Windows 10

cv::Mat Image;
Image = cv::imread("Input.png", cv::ImreadModes::IMREAD_ANYCOLOR | cv::ImreadModes::IMREAD_ANYDEPTH);

//scale 16bit gray scale image up to 0-1
int type = Image.type();
Image.convertTo(Image, CV_32F, 1.0 / 65536.0, 0.0);

//Calculations for brightness adjustment
double MinIgnore = 0;//>0 bad!
double MaxIgnore = 0.23;//Bad: 0.2, 0.4, 0.6, 0.7; OK: 0.1, 0.3, 0.5
double Gamma = 1.01;

double minHist, maxHist;
cv::minMaxLoc(Image, &minHist, &maxHist);
double Histdiff = maxHist - minHist;
double min = minHist + MinIgnore * Histdiff;
double max = maxHist - MaxIgnore * Histdiff;

double diff = max - min;
double m = (1 / (max - min));
double n = -m * min;

cv::pow(((m * Image) + n) / diff, Gamma, Image);
Image = diff * (Image);

//Smoothing
int Smoothingfactor = 11;
cv::blur(Image, Image, cv::Size(Smoothingfactor, Smoothingfactor));

//back to 16bit
Image.convertTo(Image, type, 65536.0, 0.0);

cv::imwrite("Output.png", Image);

Following 16Bit gray scale picture i use as InputImage: Input Image

This is the output on Windows 10: Output Image

Following Output will produce with MaxIgnore of 0.3: Output Image 0.3 (no black area)

edit retag flag offensive close merge delete

Comments

hi, can you try to upload your images here (the "Edit" menu has an image upload button for this) ?

(your upload site has an https problem)

berak gravatar imageberak ( 2018-06-04 04:16:17 -0600 )edit
1

No, sorry. I can't upload the image, i dont no why. After i choose my image, no image will be there. Can it be, that the image is to big?(~7MB) Or because it is 16bit gray scale? I can upload a downscaled image, but with this image the effect does not occur.

Pauli gravatar imagePauli ( 2018-06-04 04:32:24 -0600 )edit

ah right, 16bit not supported. and iirc some size limit, too.

but please don't make us download 7mb images , cmon.... !

maybe you can synthesize an image in memory. it's all about debugging some weird over/underflow, right ?

oh, and your actual code works on [0..1] float data.

berak gravatar imageberak ( 2018-06-04 04:37:13 -0600 )edit

what do you mean with synthesize an image in memory? It’s an interesting idea, that the problem comes from data-overflow. but why the effect occurs only on windows 10. And produce black pixels in a rectangle area of the picture.

Pauli gravatar imagePauli ( 2018-06-04 05:01:40 -0600 )edit
1

I have Update the links to the pictures

Pauli gravatar imagePauli ( 2018-06-04 08:56:49 -0600 )edit

try with (maxIgnore=0.23)

    Mat Image2;
    cv::blur(Image, Image2, cv::Size(Smoothingfactor, Smoothingfactor));

    //back to 16bit
    Image2.convertTo(Image, type, 65536.0, 0.0);

it's a better but not good

image description

with setUseoptimize(false)

image description

Oops : bug or not bug in blur (disable blur)

LBerger gravatar imageLBerger ( 2018-06-04 09:50:54 -0600 )edit

Wow. To see a white box is new... It is realy a bug in blur? I wonder that changes in the calculations before the blur, have an impact on the problem. I will not give up the blur filtering, what can i do? Is the problem also in other OpenCV Versions?

Pauli gravatar imagePauli ( 2018-06-04 10:42:56 -0600 )edit

blakc line is a Nan rectangle. You can blur just after imread or convertTo

LBerger gravatar imageLBerger ( 2018-06-04 11:06:03 -0600 )edit