java 如何从OpenCV中的Mat对象m读取每个像素的值作为RGB值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14072761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read value of the each pixel as RGB values from Mat object m in OpenCV
提问by deep
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?
这是我rectangle.jpg
从中读取图像的代码/sdcard
。我想知道像素值(正常,以及 RGB 格式)。我应该使用什么代码来处理它?
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");
}
}
}
回答by Vladp
Mat.get(x, y)
- returns an arrayof 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.get(x, y)
- 返回(x, y) 处所有通道值的数组,每个通道位于不同的位置。如果您的图像是 RGB,那么您将得到一个 [r, g, b] 数组。
Mat.put(x, y, value)
- sets the channel values at (x, y) to value
.
Mat.put(x, y, value)
- 将 (x, y) 处的通道值设置为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
回答by Tleung
The function used in OpenCV is double[] Mat::get(int row, int col)
.
OpenCV 中使用的函数是double[] Mat::get(int row, int col)
.