Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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

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