当图像保存为位图时,OpenCV 矩形是黑色的
OpenCV Rectangle are black when image is saved as bitmap
以下是保存前在手机屏幕上看到的图像:
将图像保存到外部存储器后:
请指导如何修复opencv绘制的矩形的颜色。
下面是我的代码:
Bitmap new_bitmap = takeScreenShot(findViewById(R.id.img));
Bitmap workingBitmap = Bitmap.createBitmap(new_bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
File outFile = new File("External directory", "My folder name");
FileOutputStream outStream = new FileOutputStream(outFile);
mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
public Bitmap takeScreenShot(View view) {
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
view.buildDrawingCache(true);
Bitmap image = view.getDrawingCache();
if (image != null) {
return Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight());
}
return null;
}
设置图片的代码如下:
Bitmap resultBitmap=BitmapFactory.decodeFile("Path for image");
Mat m_ogr = new Mat(resultBitmap.getWidth(), resultBitmap.getHeight(), CvType.CV_8UC3);
List<Rect> rectList = "Some list with Rect elements";
for (int fi = 0; fi < rectList.size(); fi++) {
Imgproc.rectangle(m_ogr, new Point(rectList.get(fi).x, rectList.get(fi).y),
new Point(rectList.get(fi).x + rectList.get(fi).width, rectList.get(fi).y + rectList.get(fi).height),
new Scalar(50, 205, 50), 2);
}
Utils.matToBitmap(m_ogr, resultBitmap);
img.setImageBitmap(resultBitmap);
保存前如何显示图片? OpenCV 不使用 alpha 通道,可能它在矩形位置为 0(例如,如果您仅使用 3 个颜色值而不是 4 个颜色值绘制矩形)?
@Micka 指出 alpha 通道后,
我通过执行以下操作解决了这个问题:
//First Change
Mat m_ogr = new Mat(resultBitmap.getWidth(), resultBitmap.getHeight(), CvType.CV_8UC4);
//Second Change
Imgproc.rectangle(m_ogr, new Point(rectList.get(fi).x, rectList.get(fi).y),
new Point(rectList.get(fi).x + rectList.get(fi).width, rectList.get(fi).y + rectList.get(fi).height),
new Scalar(50, 205, 50, 255), 2);
现在颜色正确了。谢谢
以下是保存前在手机屏幕上看到的图像:
将图像保存到外部存储器后:
请指导如何修复opencv绘制的矩形的颜色。
下面是我的代码:
Bitmap new_bitmap = takeScreenShot(findViewById(R.id.img));
Bitmap workingBitmap = Bitmap.createBitmap(new_bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
File outFile = new File("External directory", "My folder name");
FileOutputStream outStream = new FileOutputStream(outFile);
mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
public Bitmap takeScreenShot(View view) {
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
view.buildDrawingCache(true);
Bitmap image = view.getDrawingCache();
if (image != null) {
return Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight());
}
return null;
}
设置图片的代码如下:
Bitmap resultBitmap=BitmapFactory.decodeFile("Path for image");
Mat m_ogr = new Mat(resultBitmap.getWidth(), resultBitmap.getHeight(), CvType.CV_8UC3);
List<Rect> rectList = "Some list with Rect elements";
for (int fi = 0; fi < rectList.size(); fi++) {
Imgproc.rectangle(m_ogr, new Point(rectList.get(fi).x, rectList.get(fi).y),
new Point(rectList.get(fi).x + rectList.get(fi).width, rectList.get(fi).y + rectList.get(fi).height),
new Scalar(50, 205, 50), 2);
}
Utils.matToBitmap(m_ogr, resultBitmap);
img.setImageBitmap(resultBitmap);
保存前如何显示图片? OpenCV 不使用 alpha 通道,可能它在矩形位置为 0(例如,如果您仅使用 3 个颜色值而不是 4 个颜色值绘制矩形)?
@Micka 指出 alpha 通道后,
我通过执行以下操作解决了这个问题:
//First Change
Mat m_ogr = new Mat(resultBitmap.getWidth(), resultBitmap.getHeight(), CvType.CV_8UC4);
//Second Change
Imgproc.rectangle(m_ogr, new Point(rectList.get(fi).x, rectList.get(fi).y),
new Point(rectList.get(fi).x + rectList.get(fi).width, rectList.get(fi).y + rectList.get(fi).height),
new Scalar(50, 205, 50, 255), 2);
现在颜色正确了。谢谢