在 OpenCv4Android
环境中,当我创建一个 Mat
图像并使用 Core.line()
在图像上绘制时,它总是显示白色而不是我指定的颜色。
我看过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);
}
}
最佳答案
问题出在你初始化的颜色上
public static final Scalar GREEN = new Scalar(0,255,0);
根据这个声明
Mat matImage = new Mat(bitmap.getHeight(), bitmap.getWidth(), CV_8UC4);`
您正在创建一个 4 channel 垫,但仅使用 3 个组件初始化 GREEN
标量,因此是第 4 个组件,它定义了您的线的第四个 channel 的颜色,这是默认设置在你的情况下值 0
。
因此,您所感知的白色实际上是透明的。您可以通过使用 CV_8UC3
创建 matImage
或将 GREEN
标量更改为 public static final Scalar GREEN = new Scalar( 0,255,0, 255);
关于android - OpenCv Core.line 在预期颜色时绘制白色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44349358/