0
votes

I'm using opencv 3.0.0 on android-studio, i need to convert a RGB image to YIQ so i have to do some adds and subtracts equations. i used the Core.Split of openCV to take the Red , Green, and blue channel from the RGB image. Then i used this channels to calculte the YIQ image using this equations : equation. after the test of my appp i got this Error : A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x19b in tid 27631 (orstackoverflow), pid 27631 (orstackoverflow) And this is my code

public class MainActivity extends AppCompatActivity {

private static final String TAG = "3:qinQctivity";
Button button;
ImageView imageView;

ArrayList<Mat> RGB = new ArrayList<Mat>(3);
ArrayList<Mat> YIQ = new ArrayList<Mat>(3);

Mat newImage;

Mat Blue,Green,Red,I,Y,Q,B,X,D,W;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = findViewById(R.id.button);
    imageView = findViewById(R.id.image);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (!OpenCVLoader.initDebug()) {
                OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, getApplicationContext(), baseLoaderCallback);
            } else {
                baseLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);

            }

        }
    });

}

BaseLoaderCallback baseLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        super.onManagerConnected(status);
        if (status == LoaderCallbackInterface.SUCCESS) {
            try {
                Y = new Mat();
                Q = new Mat();
                I = new Mat();
                X = new Mat();
                newImage = Utils.loadResource(getApplicationContext(), R.drawable.retinalimage, CvType.CV_32FC3);

                Core.split(newImage, RGB);
                Blue = RGB.get(0);
                Red = RGB.get(1);
                Green = RGB.get(2);

                B = new Mat(); // result
                D = new Mat(); // result
                W = new Mat(); // result

                /*working on Y channel*/
                Scalar alpha_Y = new Scalar(0.299); // the factor
                Scalar alpha1_Y = new Scalar(0.587); // the factor
                Scalar alpha2_Y = new Scalar(0.114); // the factor



                Core.multiply(Red,alpha_Y, B);
                Core.multiply(Green,alpha1_Y,D);
                Core.multiply(Blue,alpha2_Y,W);
                Core.add(B,D,Y);
                Log.i(TAG, "onManagerConnected: "+ Y.toString());
                Core.add(Y,W,Y);

                /*I = 0.211 * Red - 0.523 * Green + 0.312 * Blue;*/
                Mat Z = new Mat(); // result
                Mat P = new Mat(); // result
                Mat O = new Mat(); // result

                /*working on I channel*/
                Scalar alpha_I = new Scalar(0.211); // the factor
                Scalar alpha1_I = new Scalar(0.523); // the factor
                Scalar alpha2_I = new Scalar(0.312); // the factor

                Core.multiply(Red,alpha_I,Z);
                Core.multiply(Green,alpha1_I,P);
                Core.multiply(Blue,alpha2_I,O);
                Core.add(Z,P,I);
                Core.add(I,O,I);


                /*working on Q channel*/
                /*Q = 0.596 * Red - 0.274 * Green - 0.322 * Blue;*/

                Mat V = new Mat();
                Mat W = new Mat();
                Mat N = new Mat();

                Scalar alpha_Q = new Scalar(0.596); // the factor
                Scalar alpha1_Q = new Scalar(0.274); // the factor
                Scalar alpha2_Q = new Scalar(0.322); // the factor


                Core.multiply(Red,alpha_Q,V);
                Core.multiply(Green,alpha1_Q,W);
                Core.multiply(Blue,alpha2_Q,N);
                Core.subtract(V,W,Q);
                Core.subtract(Q,N,Y);

                YIQ.add(Y);
                YIQ.add(I);
                YIQ.add(Q);

                Core.merge(YIQ,X);
                showImage(X);

            } catch(IOException e){
                e.printStackTrace();
            }

        }


    }


} ;

void showImage (Mat y){
    Bitmap bm = Bitmap.createBitmap(y.width(), y.height(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(y, bm);
    imageView.setImageBitmap(bm);
}
1

1 Answers

0
votes

You are not allocating memory for the mats B,D,W,Z,P,O,.... You need to get the size of the original RGB Matrices and pass the size to the new matrix constructors. According to OpenCV documentation for the multiply function:

Parameters:

src1 - First source array.

src2 - Second source array of the same size and the same type as src1.

dst - Destination array of the same size and type as src1.

(highlights mine)