当需要颜色时,OpenCv Core.line 会绘制白色

Dal*_*ale 1 android opencv

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

白色方块代替绿色方块

我看到了一个与灰度相关的问题,但我的图像尚未转换为灰度。

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Zda*_*daR 6

问题出在你初始化的颜色上

public static final Scalar GREEN = new Scalar(0,255,0); 
Run Code Online (Sandbox Code Playgroud)

根据这个声明

Mat matImage = new Mat(bitmap.getHeight(), bitmap.getWidth(), CV_8UC4);`
Run Code Online (Sandbox Code Playgroud)

您正在创建一个 4 通道 Mat,但GREEN仅使用 3 个组件初始化标量,因此是第四个组件,它定义了线条的第四个通道的颜色,在您的情况下它是默认值集0

所以,你所感知的白色实际上是透明的。您可以通过创建matImage标量CV_8UC3或将GREEN标量更改为来解决此问题public static final Scalar GREEN = new Scalar(0,255,0, 255);