简体   繁体   English

获取显示的屏幕或Texture2D的亮度-Unity3D

[英]Getting brightness of displayed screen or of a Texture2D - Unity3D

In my project, I need to get the brightness of the screen that is being displayed. 在我的项目中,我需要获取正在显示的屏幕的亮度。 to do that, I get a snapshot of the screen and make it as a Texture2D 为此,我获取了屏幕快照并将其作为Texture2D

To get the snapshot and convert it I use this: 要获取快照并将其转换,请使用以下命令:

public void GetScreen(ref Texture2D screenShot){
        RenderTexture rt = new RenderTexture(Screen.Width, Screen.Height, 24);
        camera.targetTexture = rt;
        screenShot = new Texture2D(Screen.Width, Screen.Height, TextureFormat.RGB24, false);
        camera.Render();
        RenderTexture.active = rt;
        screenShot.ReadPixels(new Rect(0, 0, Sreen.Width, Screen.Height), 0, 0);
        camera.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
}

but I still need to get the brightness. 但我仍然需要获得亮度。
Any suggestions will be accepted (about the brightness and/or about the conversion). 任何建议都会被接受(关于亮度和/或转换)。
Thanks in advance. 提前致谢。

Once you have the pixels, the next step is to use get pixels on the image to sample some of them and build the brightness from them: http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.GetPixel.html 有了像素后,下一步就是使用图像上的获取像素来采样其中的一些像素并从中获取亮度: http : //docs.unity3d.com/Documentation/ScriptReference/Texture2D.GetPixel.html

Determining brightness is a bit of a complex issue, so it really depends on your application. 确定亮度有点复杂,因此它实际上取决于您的应用程序。 Formula to determine brightness of RGB color has methods to determine the brightness of a single pixel. 确定RGB颜色亮度的公式具有确定单个像素亮度的方法。 You could sample several pixels towards the center of your image, and then take the average of those. 您可以向图像中心采样几个像素,然后取这些像素的平均值。

If you need a more complex solution, you could build a histogram of all the pixels brightness, and then find the peak. 如果需要更复杂的解决方案,则可以构建所有像素亮度的直方图,然后找到峰值。 http://en.wikipedia.org/wiki/Image_histogram . http://en.wikipedia.org/wiki/Image_histogram

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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