0
votes

I'm new to android studio developer, I'm looking to convert an image from RGB color space to YIQ. My app consist of taking an image as input and then transferring the image to YIQ, so I used Core.split to take the Red, Green and Blue channels from the image and then applying the equations but I couldn't apply the multiplication of the matrix with float so I used the scalar type and still stuck.

    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_4_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
                             Log.i(TAG, "onManagerConnected: "+ alpha_Y.toString());
                            Scalar alpha1_Y = new Scalar(0.587); // the factor
                            Scalar alpha2_Y = new Scalar(0.114); // the factor


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

                             /*working on I channel*/
                             /*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

                            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.set(0, Y);
                            YIQ.set(1, I);
                            YIQ.set(2, Q);


                    Log.i(TAG, "onManagerConnected: "+YIQ.toString());

                            Core.merge(YIQ,X);
                    Log.i(TAG, "onManagerConnected: "+X.toString());
                            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
OpenCV uses BGR not BRG. Also, what is the multiplication you want to do? bgr_pixel * mat3x3 ? or you want to multiply per element values of a mat with a single scalar value? - api55
as mentioned in this link en.wikipedia.org/wiki/YIQ i have to multiply every channel of the RGB image with the equation for example to get the Q channel Q = 0.596 * Red - 0.274 * Green - 0.322 * Blue, so i used Core.split to take out the RED , Green and Blue channel but i can't multiply a Mat type with float ( example : 0.596 * Red ( Mat of the RED channel) - ben mbarek Manef

1 Answers

1
votes

change this part of code

 YIQ.set(0, Y);
 YIQ.set(1, I);
 YIQ.set(2, Q);

like this

 YIQ.add(0, Y);
 YIQ.add(1, I);
 YIQ.add(2, Q);

also it may be related to a memory issue try to optimize your code