使用openCV sdk 3.1.0将图像从rgb转换为灰度

时间:2019-05-18 15:10:47

标签: java android firebase-realtime-database opencv3.0

我开发了一个应用程序,可以使用openCV 3.1.0 sdk将图像从rgb转换为灰度。我正在跟踪https://www.youtube.com/watch?v=GhQ6CHh4Enk中的示例。

在此示例中,图像是从图库中直接拾取的,但是我的应用程序从firebaseonClickListener获取图像,recyclerView将进行新的活动并解析单击的图像及其字符串,所以我不知道如何将变量位图分配给我的imageview

这是我观看的视频中的示例

{


    ImageView imgviewer;
    Uri imageuri;
    Bitmap grayBitmap, imageBitmap;
    //TextView textView;
    Button btn_calculate;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        OpenCVLoader.initDebug();

        imgviewer = findViewById(R.id.imgViewer);

        btn_calculate = findViewById(R.id.btn_calculate);
        btn_calculate.setVisibility(View.INVISIBLE);

        //textView = findViewById(R.id.textView);
        //textView.setVisibility(View.INVISIBLE);
    }

    public void openGallery(View v)
    {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(galleryIntent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == RESULT_OK && data != null)
        {
            imageuri =  data.getData();
            try
            {
                imageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),imageuri);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            imgviewer.setImageBitmap(imageBitmap);
        }
    }

    public void ConvertGrayscale(View v)
    {
        Mat Rgba = new Mat();
        Mat grayMat = new Mat();

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inDither = false;
        o.inSampleSize = 4;

        int width = imageBitmap.getWidth();
        int height = imageBitmap.getHeight();

        grayBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);

        Utils.bitmapToMat(imageBitmap,Rgba);
        Imgproc.cvtColor(Rgba,grayMat,Imgproc.COLOR_RGB2GRAY);
        Utils.matToBitmap(grayMat, grayBitmap);

        imgviewer.setImageBitmap(grayBitmap);
    }

这是我的修改

        setContentView(R.layout.activity_sample_list_detail);

        OpenCVLoader.initDebug();

        Intent intent = getIntent();
        String sampleID = intent.getStringExtra(EXTRA_sampleID);
        String sampleImageUrl = intent.getStringExtra(EXTRA_sampleImageUrl);

        imageView = findViewById(R.id.imgViewDetail);
        TextView textViewID = findViewById(R.id.idDetail);

        textViewID.setText(sampleID);
        Picasso.get()
                .load(sampleImageUrl)
                .fit()
                .centerInside()
                .into(imageView);
    }

    public void ConvertGrayscale(View v)
    {
        Mat Rgba = new Mat();
        Mat grayMat = new Mat();

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inDither = false;
        o.inSampleSize = 4;

        int width = imageBitmap.getWidth();
        int height = imageBitmap.getHeight();

        grayBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);

        Utils.bitmapToMat(imageBitmap,Rgba);
        Imgproc.cvtColor(Rgba,grayMat,Imgproc.COLOR_RGB2GRAY);
        Utils.matToBitmap(grayMat, grayBitmap);

        imageView.setImageBitmap(grayBitmap);
    }```

*The image should be convert in grayscale*
[This is my recyclerView][1]
[recyclerView intent new activity][2]




  [1]: https://i.stack.imgur.com/XK3VS.png
  [2]: https://i.stack.imgur.com/E0PDf.png

0 个答案:

没有答案