简体   繁体   中英

How to read value of the each pixel as RGB values from Mat object m in OpenCV

This is my code where I am reading image rectangle.jpg from /sdcard . I want to know the pixel value (normal, as well as in RGB format). What code should I use to deal with it?

package com.idag.edge;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;

import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;

public class MainActivity extends Activity {  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try
        {
            String path=Environment.getExternalStorageDirectory().getAbsolutePath() +"/rectangle.jpg";
            Mat m=Highgui.imread(path,1); 
            Log.i("Paramenres on matrix", "height "+ m.height()+" width "+ m.width()+" total = "+m.total()+" channels " +m.channels());

            System.out.println("element at 0 0 = "+ m.row(0).col(0).nativeObj+" element at 150 150 = "+ m.row(150).col(150).nativeObj);
        }

        catch(Exception e){
            System.err.print("Error in the code");
            Log.i("Error in imread", "Error in imread");
        } 
    }

}

Mat.get(x, y) - returns an array of all the channels values at (x, y), each channel in a different place. If your image is RGB so you will get an array of [r, g, b].

Mat.put(x, y, value) - sets the channel values at (x, y) to value .

double[] rgb = image.get(0, 0);
Log.i("", "red:"+rgb[0]+"green:"+rgb[1]+"blue:"+rgb[2]);
image.put(0, 0, new double[]{255, 255, 0});//sets the pixel to yellow

OpenCV中使用的函数是double[] Mat::get(int row, int col)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM