c# - “有什么方法可以通过特定位置(例如opencvforunity中的(x,y))在图像中访问像素”
问题描述
我正在尝试按位置访问图像像素,我一直使用字节数组进行访问,但它没有像 python image[x][y] 那样给出 x,y 的正确位置。有没有更好的方法来访问像素?
我在unity,visual studio中使用过opencv插件,无法访问它们
public texture2D image;
Mat imageMat = new Mat(image.height, image.width, CvType.CV_8UC4);
Utils.texture2DToMat(image, imageMat); // actually converts texture2d to matrix
byte[] imageData = new byte[(int)(imageMat.total() * imageMat.channels())]; // pixel data of image
imageMat.get(0, 0, imageData);// gets pixel data
pixel=imageData[(y * imageMat.cols() + x) * imageMat.channels() + r]
y 和 x 是代码中的像素值,r 是通道,但我无法使用该代码访问 x 和 y 的特定值
解决方案
没有通常的方法可以做到这一点,因为操作真的很慢。但是一些技巧是你可以从“相机”类中制作屏幕纹理。
制作纹理后,您可以使用 texture.GetPixel(x,y)
public class Example : MonoBehaviour
{
// Take a "screenshot" of a camera's Render Texture.
Texture2D RTImage(Camera camera)
{
// The Render Texture in RenderTexture.active is the one
// that will be read by ReadPixels.
var currentRT = RenderTexture.active;
RenderTexture.active = camera.targetTexture;
// Render the camera's view.
camera.Render();
// Make a new texture and read the active Render Texture into it.
Texture2D image = new Texture2D(camera.targetTexture.width, camera.targetTexture.height);
image.ReadPixels(new Rect(0, 0, camera.targetTexture.width, camera.targetTexture.height), 0, 0);
image.Apply();
// Replace the original active Render Texture.
RenderTexture.active = currentRT;
return image;
}
}
推荐阅读
- linux - 更新 Shiny Server Config 以更改超时错误
- php - Laravel 密码忘记跳过通知
- python - 在 gzip 文件中编写脚本输出的 Pythonic 方式(Windows)
- c# - 从 SQL 服务器获取 DateTime 并将其分配给属性
- django - Django Rest Framework:如何添加多对多关系?
- tensorflow - 线性回归器 - clip_gradients_by_norm - 语法无效
- java - 如何调试 Spring Boot @ConfigurationProperties?
- python - 使用 PANDAS 中的 value_counts() 零出现/频率
- xamarin.forms.listview - 我正在尝试从安卓模拟器发送请求。但是应用程序没有提供正确的输出并在 client.sendasync 上中断
- angular - 如果在angular2中编辑后具有值,如何保持字段启用