2017-06-03 67 views
1

OpenCv4Android環境中,當我創建Mat圖像並使用Core.line()在圖像上繪圖時,它始終顯示一個白色,而不是我指定的顏色。OpenCv 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); 
    } 
} 

回答

2

的問題是你已經初始化

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

按照這種說法

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

要創建一個4通道墊的顏色,但初始化GREEN標量與3只有組件,因此第四個組件,它定義了你的線路的第四個通道的顏色,在你的情況下,默認值爲0

所以,你感覺白色是現實中透明的。您可以通過創建matImageCV_8UC3或將您的GREEN標量更改爲public static final Scalar GREEN = new Scalar(0,255,0, 255);