Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Scalar in inRange and setTo missing? #101

Closed
gurglet opened this issue Mar 11, 2015 · 7 comments
Closed

Support for Scalar in inRange and setTo missing? #101

gurglet opened this issue Mar 11, 2015 · 7 comments
Labels

Comments

@gurglet
Copy link

gurglet commented Mar 11, 2015

I'm trying to use inRange() and Mat.setTo() to color in some areas in an image but I'm encountering problems when using Scalar in the functions:

inRange(intensity, zeroScalar, black_level, mask);
image.setTo(blackColor, mask);

Errors:

Error:(83, 9) Gradle: error: method inRange in class opencv_core cannot be applied to given types;
required: Mat,Mat,Mat,Mat
found: Mat,Scalar,Scalar,Mat
reason: actual argument Scalar cannot be converted to Mat by method invocation conversion

Error:(84, 14) Gradle: error: no suitable method found for setTo(Scalar,Mat)
method Mat.setTo(Mat) is not applicable
(actual and formal argument lists differ in length)
method Mat.setTo(Mat,Mat) is not applicable
(actual argument Scalar cannot be converted to Mat by method invocation conversion)

I've used OpenCV4Android before and it works in that. From what I can see in the C++ documentation it should work as well

Mat& Mat::setTo(InputArray value, InputArray mask=noArray() )
Parameters:
value – Assigned scalar converted to the actual array type.

Is it missing from JavaCV or am I using the functions wrong?

@saudet
Copy link
Member

saudet commented Mar 15, 2015

Not really missing no, but the InputArray design pattern isn't efficient when expressed in Java. Besides, Mat works just as well as a generic type, so no need of Scalar and others. Just use Mat, for example, in this case:

inRange(intensity, new Mat(new Size(4, 1), CV_64FC1, zeroScalar), new Mat(new Size(4, 1), CV_64FC1, black_level), mask);
image.setTo(new Mat(new Size(4, 1), CV_64FC1, blackColor), mask);

Or create Mat objects directly from scalars if you don't need to use the Scalar class to start with.

@saudet saudet closed this as completed Mar 15, 2015
@govi2010
Copy link

@saudet this code is not working


let main_image1 = this.ImageToMat(res);  // Convert bitmat to MAT object
    this.cvtColor(main_image1, main_image1, ColorConversionCodes.COLOR_RGB2RGBA); /// my custom method for cvtColor
    let main_image = this.ImageToMat(res);
    this.cvtColor(main_image, main_image, ColorConversionCodes.COLOR_RGB2GRAY); /// my custom method for cvtColor
    //

    this.bitwise_not(main_image, main_image);  /// my custom method for cvtColor
    this.threshold(main_image, main_image, 0, 255, ThresholdTypes.THRESH_BINARY);  /// my custom method for cvtColor

    let dst = this.CreateMat();   /// my custom method new mat
    let low = new org.bytedeco.opencv.opencv_core.Mat(main_image.size(), main_image.type(), new org.bytedeco.opencv.opencv_core.Scalar(0, 0, 200, 0) );
    let high = new org.bytedeco.opencv.opencv_core.Mat(main_image.size(), main_image.type(), new org.bytedeco.opencv.opencv_core.Scalar(180, 20, 255, 0));

    org.bytedeco.opencv.global.opencv_core.inRange(main_image, low, high, dst);
    this.bitwise_not(dst, dst);

    console.log(main_image1.type());
    main_image1.setTo(new org.bytedeco.opencv.opencv_core.Mat( new org.bytedeco.opencv.opencv_core.Scalar(255, 0, 0, 255)), dst);

my dst means mask is something like this. ...Check lower Image
image

@saudet
Copy link
Member

saudet commented Oct 25, 2019

What is not working exactly?

@govi2010
Copy link

govi2010 commented Oct 25, 2019

I want to turn that blue line to red.

Now consider the upper image which is a blue line at main_image1

let main_image1 = this.ImageToMat(res);  // Convert bitmat to MAT object
    this.cvtColor(main_image1, main_image1, ColorConversionCodes.COLOR_RGB2RGBA);

and Mask as a white line image which is at the bottom. DST is mask image

let main_image = this.ImageToMat(res);
    this.cvtColor(main_image, main_image, ColorConversionCodes.COLOR_RGB2GRAY); /// my custom method for cvtColor
    //

    this.bitwise_not(main_image, main_image);  /// my custom method for cvtColor
    this.threshold(main_image, main_image, 0, 255, ThresholdTypes.THRESH_BINARY);  /// my custom method for cvtColor

    let dst = this.CreateMat();   /// my custom method new mat
    let low = new org.bytedeco.opencv.opencv_core.Mat(main_image.size(), main_image.type(), new org.bytedeco.opencv.opencv_core.Scalar(0, 0, 200, 0) );
    let high = new org.bytedeco.opencv.opencv_core.Mat(main_image.size(), main_image.type(), new org.bytedeco.opencv.opencv_core.Scalar(180, 20, 255, 0));

    org.bytedeco.opencv.global.opencv_core.inRange(main_image, low, high, dst);
    this.bitwise_not(dst, dst);

Now I call

    main_image1.setTo(new org.bytedeco.opencv.opencv_core.Mat( new org.bytedeco.opencv.opencv_core.Scalar(255, 0, 0, 255)), dst);

But main_image1 is blank white image.

@saudet
Copy link
Member

saudet commented Oct 25, 2019

Let's see, it looks like setTo() wants a column 4-vector, not a scalar, so try something like this:

main_image1.setTo(new Mat(4, 1, CV_64FC1, new DoublePointer(255, 0, 0, 255)), dst);

@govi2010
Copy link

java.lang.Exception: Failed resolving constructor for class 'org.bytedeco.javacpp.DoublePointer' with 4 parameters. Check the number and type of arguments.
Primitive types need to be manually wrapped in their respective Object wrappers.
If you are creating an instance of an inner class, make sure to always provide reference to the outer `this` as the first argument.
    com.tns.Runtime.resolveConstructorSignature(Runtime.java:1182)
    com.tns.Runtime.callJSMethodNative(Native Method)
    com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1242)
    com.tns.Runtime.callJSMethodImpl(Runtime.java:1122)
    com.tns.Runtime.callJSMethod(Runtime.java:1109)
    com.tns.Runtime.callJSMethod(Runtime.java:1089)
    com.tns.Runtime.callJSMethod(Runtime.java:1081)
    com.tns.gen.java.lang.Object_vendor_19262_32_ClickListenerImpl.onClick(Object_vendor_19262_32_ClickListenerImpl.java:18)
    android.view.View.performClick(View.java:6294)
    android.view.View$PerformClick.run(View.java:24770)
    android.os.Handler.handleCallback(Handler.java:790)
    android.os.Handler.dispatchMessage(Handler.java:99)
    android.os.Looper.loop(Looper.java:164)
    android.app.ActivityThread.main(ActivityThread.java:6494)
    java.lang.reflect.Method.invoke(Native Method)
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
    at OpenCV.webpackHotUpdate.../../src/open-cv.ts.OpenCV.ApplyResultOnImage (open-cv.ts:319)

@saudet
Copy link
Member

saudet commented Oct 25, 2019

You're going to need to obey the rules of whatever programming language you're using!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants