Opencv4nodejs: How to make min and max (mat) on nodejs

Created on 8 Nov 2017  路  11Comments  路  Source: justadudewhohacks/opencv4nodejs

I have an example of a Python
I can not understand how to write it on a nodejs

I wrote a part of the example, but I'm not sure if this is correct

Python

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")

Nodejs

let gradX = blackhat.sobel(cv.CV_32F,1,0,-1);
gradX = gradX.abs();
let minVal = **???**
let maxVal = **???**

let v1 = gradX.sub(minVal);
let v2 = maxVal.sub(minVal);
let v3 = v1.hDiv(v2);
gradX = v3.mul(255);

Most helpful comment

The first parameter should be the kernel. You did pass it the grey image twice. Try something like this:

let kernelSize = new cv.Size(13, 5)
let rectKernel = cv.getStructuringElement(cv.MORPH_RECT, kernelSize)
let blackhat = grey.morphologyEx(rectKernel, cv.MORPH_BLACKHAT)

Another note on rewriting the python code to nodejs:
Currently it is not directly possible to do something like Mat - Number, would be a nice TODO to add this functionality for convenience. To achieve this right now you have to do it like this:

let minValMat = new cv.Mat(gradX.rows, gradX.cols, gradX.type, minVal);
let maxValMat = new cv.Mat(gradX.rows, gradX.cols, gradX.type, maxVal);
let v1 = gradX.sub(minValMat);
let v2 = maxValMat.sub(minValMat);
let v3 = v1.hDiv(v2);

All 11 comments

Hey,
you can view the function signature in the docs:

{ minVal: Number, maxVal: Number, minLoc: Point2, maxLoc: Point2 } : mat.minMaxLoc(Mat mask = noArray())

So you could try:

let minMax = gradX.minMaxLoc();
let minVal = minMax.minVal;
let maxVal = minMax.maxVal;

Alternatively with destructuring syntax:

let { minVal, maxVal } = gradX.minMaxLoc();

cool.
now I have stopped on how to take away the Number from the MAT ))

i have a very strange moment

this is blackhut on python

rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))
blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, rectKernel)

image

and this is blackhut on python

let blackhat = grey.morphologyEx(grey, cv.MORPH_BLACKHAT,new cv.Point(13, 5));
image

why did they turn out different??

The first parameter should be the kernel. You did pass it the grey image twice. Try something like this:

let kernelSize = new cv.Size(13, 5)
let rectKernel = cv.getStructuringElement(cv.MORPH_RECT, kernelSize)
let blackhat = grey.morphologyEx(rectKernel, cv.MORPH_BLACKHAT)

Another note on rewriting the python code to nodejs:
Currently it is not directly possible to do something like Mat - Number, would be a nice TODO to add this functionality for convenience. To achieve this right now you have to do it like this:

let minValMat = new cv.Mat(gradX.rows, gradX.cols, gradX.type, minVal);
let maxValMat = new cv.Mat(gradX.rows, gradX.cols, gradX.type, maxVal);
let v1 = gradX.sub(minValMat);
let v2 = maxValMat.sub(minValMat);
let v3 = v1.hDiv(v2);

if I use cv.THRESH_BINARY then everything is fine, but if I use cv.THRESH_OTSU then I get an error

let thresh = gradX.threshold(0,255,cv.THRESH_OTSU);
or
let thresh = gradX.threshold(0,255,cv.THRESH_BINARY | cv.THRESH_OTSU);

OpenCV Error: Assertion failed (src.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3))) in threshold, file /tmp/opencv-20171031-96694-16heojr/opencv-3.3.1/modules/imgproc/src/thresh.cpp, line 1402
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv-20171031-96694-16heojr/opencv-3.3.1/modules/imgproc/src/thresh.cpp:1402: error: (-215) src.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function threshold

./run: line 1: 15978 Abort trap: 6           node index.js

Strange, there is obviously something wrong with the flag. Flags are defined as follows:

enum ThresholdTypes {
    THRESH_BINARY     = 0,
    ...
    THRESH_OTSU       = 8
};

Can you try the following to make sure none of the flags is undefined:

console.log(cv.THRESH_BINARY) // should be 0
console.log(cv.THRESH_OTSU) // should be 8

If this is not working: let thresh = gradX.threshold(0,255, 8);, then I will check it later today.

I have in console

THRESH_BINARY = 0
THRESH_OTSU = 8

let thresh = gradX.threshold(0,255, 8); - not working
https://github.com/Aserus/node-mrz/tree/master

Ok I will check out when at home. Maybe it's also opencv3.3.1 related. Do you use an official release, opencv.3.3.1rc or an own build?

I use opencv: stable 3.3.1 (bottled)
Installed from homebrew

Hey just tested it and threshold with cv.THRESH_OTSU worked for me. However I think the issue is gradX is CV_32F right? I think opencv will let you threshold only CV_8U typed Mats. You could convert the type:
gradX = gradX.convertTo(cv.CV_8U)

Hey, is this still an issue or can I close it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SatoshiKawabata picture SatoshiKawabata  路  5Comments

bgsuello picture bgsuello  路  6Comments

Paulito-7 picture Paulito-7  路  5Comments

s1hofmann picture s1hofmann  路  7Comments

ShadabFaiz picture ShadabFaiz  路  7Comments