Ask Your Question
0

Truncate and Normalize image

asked 2020-03-16 08:39:15 -0500

Greycloud gravatar image

Is there a way to truncate and normalize an image in one step?

e.g. linearly map values from 50-100 to 0-255 ?

I can do it manually but of course its slow...

 byte range = (byte) (UpperThreshold - LowerThreshold);
            double f = (1.0 / (double) range) * 255;
            var copy = scan.Clone();
            for (int x = 0; x < copy.Cols; x++) {// todo this needs to be in native openCV to be fast enough for use
                for (int y = 0; y < copy.Rows; y++) {
                    byte v = copy.At<byte>(y, x);
                    double d = v;
                    d =  (d - LowerThreshold) * f;
                    if (d < 0) d = 0;
                    if (d > 255) d = 255;
                    v = (byte) d;
                    copy.Set<byte>(y,x,v);
                }
            }

            return copy;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-03-16 09:56:39 -0500

berak gravatar image

updated 2020-03-16 10:20:39 -0500

see here

// make a mask, where "wanted" pixels are 255
Mat mask;
inRange(src, Scalar::all(50), Scalar::all(100), mask); // ::all in case of multiple chans

// to [0,255], restricted to mask "on" (else set to 0)
// Mat dst;
normalize(src, dst, 0, 255, NORM_MINMAX, -1, mask);
edit flag offensive delete link more

Comments

1

as i understand it this will map the Min and Max values of my image to 0-255. Rather i want to map values between 50-100 to 0-255 - e.g. anything less than 50 is mapped to zero and anything higher than 100 is mapped to 255. My image contains values below 50 and above 100

Greycloud gravatar imageGreycloud ( 2020-03-16 10:00:48 -0500 )edit

^^ right, see update

berak gravatar imageberak ( 2020-03-16 10:18:03 -0500 )edit

Epic, runtime reduced from 5000ms to 31ms Thank you @berak :)

Greycloud gravatar imageGreycloud ( 2020-03-17 04:41:06 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-03-16 08:39:15 -0500

Seen: 210 times

Last updated: Mar 16 '20