Home Scaling Mat elements to [0..255] range with OpenCV on Android
Reply: 1

Scaling Mat elements to [0..255] range with OpenCV on Android

sposnjak
1#
sposnjak Published in 2018-02-14 08:35:46Z

I am trying to convert some OpenCV python code to Java and I ran into a problem of finding the correct functions in Java wrapper. The python code in question is calculating the Scharr gradient along the x-axis as:

# compute the Scharr gradient of the blackhat image and scale the
# result into the range [0, 255]
gradX = cv2.Sobel(blackhat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
gradX = np.absolute(gradX)
(minVal, maxVal) = (np.min(gradX), np.max(gradX))
gradX = (255 * ((gradX - minVal) / (maxVal - minVal))).astype("uint8")

[Code is from pyimagesearch website - Detecting machine-readable zones in passport images - blog post]

What I have in Java is:

Imgproc.Sobel(blackhat, gradX, CvType.CV_32F, 1, 0, -1, 1.0, 0, Core.BORDER_DEFAULT);
Core.convertScaleAbs(gradX, gradX);
Core.MinMaxLocResult minmax = Core.minMaxLoc(gradX);

This should output the same result as the first three lines of the python code. But I got stuck on the last one. How does one perform the scaling of the gradX back into the [0..255] range with Java API?

Also does the convertScaleAbs function perform the same action as NumPys absolute?

Andrey Smorodov
2#
Andrey Smorodov Reply to 2018-02-14 09:01:51Z

Use normalize with normType=NORM_MINMAX.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30836 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO