Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you can't add a 8bit, 1 channel roi (your thresholded image) to an rgba image, you'll have to cvtColor that first:

Mat roi = source.submat(100, 700, 100, 700);
Log.w("roi type", String.valueOf(roi.type()));

Mat mBGR= new Mat();
Imgproc.cvtColor(roi, mBGR, Imgproc.COLOR_RGBA2BGR,0);

Mat threshBin = new Mat();
Core.inRange(mBGR, new Scalar(0, 255, 255), new Scalar(0, 255, 255), threshBin ); 

// ok. now we got an 8bit binary img in threshBin, 
// convert it to rbga:
Mat threshRGBA = new Mat();
Imgproc.cvtColor(threshBin , threshRGBA , Imgproc.COLOR_GRAY2RGBA);

// now we can put it back in place:
threshRGBA.copyTo(roi);

btw, try to avoid any put() get() pixel operations the way you tried above. if you ever have to go at the pixels, do it this way:

int npixels = mat.total() * mat.elemSize();
byte[] pixels = new byte[npixels];
mat.get(0,0,pixels);
//
// work with pixels
//
mat.put(0,0,pixels);

you can't add a 8bit, 1 channel roi (your thresholded image) to an rgba image, you'll have to cvtColor that first:

Mat roi = source.submat(100, 700, 100, 700);
Log.w("roi type", String.valueOf(roi.type()));

Mat mBGR= new Mat();
Imgproc.cvtColor(roi, mBGR, Imgproc.COLOR_RGBA2BGR,0);

Mat threshBin = new Mat();
Core.inRange(mBGR, new Scalar(0, 255, 255), new Scalar(0, 255, 255), threshBin ); 

// ok. now we got an 8bit binary img in threshBin, 
// convert it to rbga:
Mat threshRGBA = new Mat();
Imgproc.cvtColor(threshBin , threshRGBA , Imgproc.COLOR_GRAY2RGBA);

// now we can put it back in into it's old place:
threshRGBA.copyTo(roi);

btw, try to avoid any put() get() pixel operations the way you tried above. if you ever have to go at the pixels, do it this way:way (but careful, this won't work with a roi, only with a 'full' mat):

int npixels = mat.total() * mat.elemSize();
byte[] pixels = new byte[npixels];
mat.get(0,0,pixels);
//
// work with pixels
//
mat.put(0,0,pixels);