如何在OpenCV中从Mat对象m读取每个像素的值作为RGB值(How to read value of the each pixel as RGB values from Mat object m

系统教程 行业动态 更新时间:2024-06-14 16:57:40
如何在OpenCV中从Mat对象m读取每个像素的值作为RGB值(How to read value of the each pixel as RGB values from Mat object m in OpenCV)

这是我从/sdcard阅读image rectangle.jpg代码。 我想知道像素值(正常,以及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"); } } }

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) - 返回Mat.get(x, y)所有通道值的数组 ,每个通道在不同的地方。 如果你的图像是RGB,所以你会得到一个[r,g,b]的数组。

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

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

更多推荐

本文发布于:2023-04-13 12:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/ed46e425d8b9a8556eca44fd91230f85.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:像素   对象   如何在   Mat   OpenCV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!