Ask Your Question
0

[Debugging] Provided data element number (1) should be multiple of the Mat channels count (4)

asked 2015-01-14 08:35:08 -0600

jiarongkoh gravatar image

Hi there, I am attempting to threshold a ROI of an image which contains a specific color yellow. So my objective is to threshold that specific ROI and then lay it back to the original image and then display the new image again. This new image will thus be the original image with that little ROI threshold-ed. I am trying out this this set of code which I have no idea if it will work yet. The idea is to set the ROI region in the original image with the threshold-ed colors at its corresponding pixel coordinates:

            Mat source = new Mat();     
    Utils.bitmapToMat(drawnBitmap, source);

    Mat roi = source.submat(100, 700, 100, 700);
    Log.w("roi type", String.valueOf(roi.type()));
    Mat mBGR= new Mat();
    Mat mBGRThres = new Mat();
    Imgproc.cvtColor(roi, mBGR, Imgproc.COLOR_RGBA2BGR,0);
    Core.inRange(mBGR, new Scalar(0, 255, 255), new Scalar(0, 255, 255), mBGRThres); 

    Mat thresholded= source.clone();
    for(int j=0; j<thresholded.rows(); j++)  { 
        for(int i=0;i<thresholded.cols(); i++) {
            if(i>=100 && i<=700 && j>=100 && j<=700){                   
                thresholded.put(j, i,mBGRThres.get(j-100, i-700) );
            }
        }

        }
    double color[]= source.get(300, 300); // rgb at the coordinate of the image 300, 300
    Log.w("RGB", String.valueOf(color[0]) + String.valueOf(color[1]) +String.valueOf(color[2]));

    Highgui.imwrite("Threshold.jpg", thresholded);

    Bitmap bmp = Bitmap.createBitmap(thresholded.rows(),
            thresholded.cols(), Config.ARGB_8888);
    Utils.matToBitmap(thresholded, bmp);

I tried compiling but the logcat throws me the following set of errors:

01-14 22:20:33.340: E/AndroidRuntime(6722): java.lang.UnsupportedOperationException: Provided data element number (1) should be multiple of the Mat channels count (4) 01-14 22:20:33.340: E/AndroidRuntime(6722): at org.opencv.core.Mat.put(Mat.java:2496)

So what I gather from this set of error is that the channels are different, the mBGRThres has (1) channel and thresholded has (4) channels. Is there any way to perform cvtColor or any other methods to align their channels? Correct me if my understanding of the error msg is warped. Thanks!

I develop on the Android platform in Eclipse using OpenCV249

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-01-14 08:50:07 -0600

berak gravatar image

updated 2015-01-14 08:55:44 -0600

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 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 (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);
edit flag offensive delete link more

Comments

Hi @berak, thanks so much for your input. If I can't perform that threshold-ing with ROI, then how can I perform the thresholding of an roi on an image? It has been a problem I'm facing for quite some time and has yet to find a good solution. I thought working with pixels would be the best bet.

jiarongkoh gravatar imagejiarongkoh ( 2015-01-15 21:35:13 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-01-14 08:35:08 -0600

Seen: 8,289 times

Last updated: Jan 14 '15