Ask Your Question
0

How to use setTo() in JAVA

asked 2019-02-05 14:19:25 -0600

cz11@hw.ac.uk gravatar image
    Mat gray = new Mat();
    Utils.bitmapToMat(grayBitmap,gray);
    Mat blocks = new  Mat(gray.size(),gray.type());
    Core.randu(blocks,0,1);
    blocks.setTo(0,blocks<0.3);
    blocks.setTo(1,blocks>0.3);

I use android studio to build the code, but it told me that "Operator '>' cannot be applied to 'org.opencv.core.Mat', 'double' " for blocks >/<0.3 parts, is that mean I cant use this method in Java or I need to add something else?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-02-07 07:35:40 -0600

berak gravatar image

updated 2019-02-07 07:38:11 -0600

some misassumptions here:

  • grayscale / binary images are NOT in [0..1] range, but [0..255], thus, e.g. Core.randu(blocks,0,1); will be all 0 !
  • java does not have overloaded > or < operators, you'll have to use: Mat mask=new Mat(); Core.compare(blocks, newScalar(0.3 * 255.0), mask, Core.CMP_LT);
  • your setTo() calls don't make any sense, as long as you are re-using the same blocks Mat for exclusive (contradicting) operations.


in the end, i think, you wanted this:

Mat gray = new Mat();
Utils.bitmapToMat(grayBitmap,gray);
Mat blocks = new  Mat(gray.size(),gray.type());
// right border is exclusive !
Core.randu(blocks,0,256); 
// lower part to 0, upper to 255
Imgproc.threshold(blocks, blocks, 255 * 0.3, 255, Imgproc.THRESH_BINARY);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-02-05 14:19:25 -0600

Seen: 955 times

Last updated: Feb 07 '19