使用OpenCV

时间:2019-02-08 07:50:56

标签: java android opencv mat

我是Android编程的初学者。我正在开发一个用于图像隐写术的Android应用程序。我已经使用Java和OpenCV开发了适用于桌面的此应用程序,效果非常好。我正在尝试重用代码,我有。

我创建了一个Mat对象并将其传递给Java类(已经有)。我能够读取Mat对象中的像素强度,但是,当我尝试修改这些值(使用put()),我的应用程序崩溃了。

引发了异常

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hari.imagesteganography, PID: 8850
    java.lang.UnsupportedOperationException: Provided data element number (3) should be multiple of the Mat channels count (4)
        at org.opencv.core.Mat.put(Mat.java:954)
        at com.example.hari.imagesteganography.LSBImageStego.encodeOriginalMessageBinaryLength(LSBImageStego.java:101)
        at com.example.hari.imagesteganography.LSBImageStego.encodeImage(LSBImageStego.java:239)
        at com.example.hari.imagesteganography.EncodeActivity$1.onClick(EncodeActivity.java:98)
        at android.view.View.performClick(View.java:6302)
        at android.view.View$PerformClick.run(View.java:24782)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6518)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

相同的方法(put())在桌面上运行完全正常。

以下是我在Android应用程序中的代码。

private void encodeOriginalMessageBinaryLength(){

// LOGGING THIS,SHOWS THAT THE FUNCTION EXECUTION STARTED
        Log.i("URI" , "encodeOriginalMessageBinaryLength()");

        String binary_of_originalMessageBinaryLength = Integer.toBinaryString(this.originalMessageBinaryLength);
        if(binary_of_originalMessageBinaryLength.length()%2 !=0){
            binary_of_originalMessageBinaryLength = "0" + binary_of_originalMessageBinaryLength;
        }
        int remaining = binary_of_originalMessageBinaryLength.length();
        String newLsbBits;

        for(int col = this.coverImage_cols -1 ; col >=0 ; col--){
            if(remaining > 0){
                newLsbBits = binary_of_originalMessageBinaryLength.substring( remaining - NUMBER_OF_BITS_REPLACING , remaining );
                remaining -= NUMBER_OF_BITS_REPLACING;

            }else{
                newLsbBits = String.join("", Collections.nCopies(NUMBER_OF_BITS_REPLACING, "0"));
            }

            Log.i("URI","newLsbBits"+newLsbBits);
            String modifiedBinaryString = this.getBeautifiedBinaryString(this.coverImage.get( this.coverImage_rows -1,col )[0]).substring(0 , this.getBeautifiedBinaryString(this.coverImage.get( this.coverImage_rows -1,col )[0]).length() - NUMBER_OF_BITS_REPLACING) + newLsbBits;

            Log.i("URI","modifiedBianryString"+modifiedBinaryString);
            double[] data = new double[3];
            data[0] = Integer.parseInt(modifiedBinaryString , 2);
            data[1] = this.coverImage.get( this.coverImage_rows -1,col )[1];
            data[2] = this.coverImage.get( this.coverImage_rows -1,col )[2];
this.coverImage_rows -1,col )[0]);

//   LOGGING THE DIMENSIONS OF THE IMAGE CORRECTLY,SHOWS THAT THE Mat Object 
//   IS INITIALIZED CORRECTLY
            Log.i("URI" , new Integer(this.coverImage_rows).toString());
            Log.i("URI" , new Integer(this.coverImage_cols).toString());

//  EXECUTING BELOW LINE CAUSES THE APP TO CRASH
            this.coverImage.put( this.coverImage_rows -1,col  , data);


// BELOW MESSAGE IS NOT BEING LOGGED
           Log.i("URI" , "encodeOriginalMessageBinaryLength() END");

        }
}

对应的Logcat日志如下。

2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: encodeOriginalMessageBinaryLength()
2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: newLsbBits10
2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: modifiedBianryString10010110
2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: 1440
2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: 2560

Mat对象和其他属性的设置如下

public LSBImageStego(Mat coverImage){
        this.coverImage = coverImage;
        this.coverImage_rows = (int)coverImage.size().height;
        this.coverImage_cols = (int)coverImage.size().width;
}

我的代码是否错误?如何在Android中使用OpenCV和Java更改Mat对象的像素强度值?

我相信,这个问题不是Using get() and put() to access pixel values in OpenCV for Java的重复 要么 opencv java modify pixel values ,因为它们不适用于Android,并且这些问题中介绍的put()方法对我来说在台式机上也能正常工作。

1 个答案:

答案 0 :(得分:0)

方法put()在Mat类中已重载。 对于每种Mat类型,都需要特定的数据类型。 例如,如果Mat类型为CV_32S,则所需数据为int []。 您可以检查所有Mat类型的Mat类的实现。