OpenCv Core.line 在预期颜色时绘制白色
OpenCv Core.line draws white when color is expected
在 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 通道垫,但仅使用 3 个组件初始化 GREEN
标量,因此是第 4 个组件,它定义了您的线的第四个通道的颜色,它设置为默认值0
你的情况。
所以,你认为的白色实际上是透明的。您可以通过使用 CV_8UC3
创建 matImage
或将 GREEN
标量更改为 public static final Scalar GREEN = new Scalar(0,255,0, 255);
来解决此问题
在 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 通道垫,但仅使用 3 个组件初始化 GREEN
标量,因此是第 4 个组件,它定义了您的线的第四个通道的颜色,它设置为默认值0
你的情况。
所以,你认为的白色实际上是透明的。您可以通过使用 CV_8UC3
创建 matImage
或将 GREEN
标量更改为 public static final Scalar GREEN = new Scalar(0,255,0, 255);