OpenCv Core.line在预期颜色时绘制白色

时间:2017-06-03 23:15:25

标签: android opencv opencv4android

OpenCv4Android环境中,当我创建Mat图片并使用Core.line()在图片上绘图时,它始终会显示白色而不是我指定的颜色。

white square instead of green square

我看过a question related to gray scale,但我的图像尚未转换为灰色。

public class DrawingTest extends AppCompatActivity {
    public static final Scalar GREEN = new Scalar(0,255,0);
    private RelativeLayout mLayout;
    private ImageView imageView;

    static {
        System.loadLibrary("opencv_java");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawing_test);

        mLayout = (RelativeLayout) findViewById(R.id.activity_drawing_test);
        mLayout.setDrawingCacheEnabled(true);

        imageView = (ImageView) this.findViewById(imageView_dt);

        //test.jpg is in the drawable-nodpi folder, is an normal color jpg image.
        int drawableResourceId = getResources().getIdentifier("test", "drawable", getPackageName());
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawableResourceId);
        //Mat matImage = new Mat(); // Also white
        Mat matImage = new Mat(bitmap.getHeight(), bitmap.getWidth(), CV_8UC4);
        Utils.bitmapToMat(bitmap, matImage);


        // Attempt to draw a GREEN box, but it comes out white
        Core.line(matImage, new Point(new double[]{100,100}), new Point(new double[]{100, 200}), GREEN,4);
        Core.line(matImage, new Point(new double[]{100,200}), new Point(new double[]{200, 200}), GREEN,4);
        Core.line(matImage, new Point(new double[]{200,200}), new Point(new double[]{200, 100}), GREEN,4);
        Core.line(matImage, new Point(new double[]{200,100}), new Point(new double[]{100, 100}), GREEN,4);

        Bitmap bitmapToDisplay = Bitmap.createBitmap(matImage.cols(), matImage.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(matImage, bitmapToDisplay);
        imageView.setImageBitmap(bitmapToDisplay);
    }
}

1 个答案:

答案 0 :(得分:2)

问题在于您初始化的颜色

public static final Scalar GREEN = new Scalar(0,255,0); 

根据此声明

Mat matImage = new Mat(bitmap.getHeight(), bitmap.getWidth(), CV_8UC4);`

你正在创建一个4通道Mat,但只用3个组件初始化GREEN标量,因此是第4个组件,它定义了你的第四个通道的颜色,它是默认值{{ 1}}在你的情况下。

所以,你认为白色的东西在现实中是透明的。您可以通过使用0创建matImage或将CV_8UC3标量更改为GREEN

来解决此问题