Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 亮度脚本使图像完全黑白_C#_Image Processing_Unity3d_Pixels_Brightness - Fatal编程技术网

C# 亮度脚本使图像完全黑白

C# 亮度脚本使图像完全黑白,c#,image-processing,unity3d,pixels,brightness,C#,Image Processing,Unity3d,Pixels,Brightness,我想更改我在unity场景中上传的图像的亮度。以下是我当前的脚本: public void AdjustBrightness(string brightness) { int brightnessInt = Convert.ToInt32(brightness); int mappedBrightness = (51 * brightnessInt) / 10 - 255; //Make an empty Texture the same

我想更改我在unity场景中上传的图像的亮度。以下是我当前的脚本:

public void AdjustBrightness(string brightness)
    {
        int brightnessInt = Convert.ToInt32(brightness);
        int mappedBrightness = (51 * brightnessInt) / 10 - 255;
        //Make an empty Texture the same same as the original 
        Texture2D bitmapImage = new Texture2D(imgTexture.width, imgTexture.height);

        if (mappedBrightness < -255) mappedBrightness = -255;
        if (mappedBrightness > 255) mappedBrightness = 255;
        Color color;
        for (int i = 0; i < bitmapImage.width; i++)
        {
            for (int j = 0; j < bitmapImage.height; j++)
            {
                color = bitmapImage.GetPixel(i, j);
                int cR = (int)color.r + mappedBrightness;
                int cG = (int)color.g + mappedBrightness;
                int cB = (int)color.b + mappedBrightness;

                if (cR < 0) cR = 0;
                if (cR > 255) cR = 255;

                if (cG < 0) cG = 0;
                if (cG > 255) cG = 255;

                if (cB < 0) cB = 0;
                if (cB > 255) cB = 255;

                bitmapImage.SetPixel(i, j,
    new Color((float)cR, (float)cG, (float)cB));
            }
        }
            //Apply all SetPixel changes
            bitmapImage.Apply();

            //Connect texture to material of GameObject this script is attached to 
            Image.renderer.material.mainTexture = bitmapImage as Texture;
    }
public亮度(字符串亮度)
{
int-brightnessInt=Convert.ToInt32(亮度);
int-mappedBrightness=(51*brightnessInt)/10-255;
//使空纹理与原始纹理相同
Texture2D bitmapImage=新的Texture2D(imgTexture.width,imgTexture.height);
如果(mappedBrightness<-255)mappedBrightness=-255;
如果(mappedBrightness>255)mappedBrightness=255;
颜色;
对于(int i=0;i255)cR=255;
如果(cG<0)cG=0;
如果(cG>255)cG=255;
如果(cB<0)cB=0;
如果(cB>255)cB=255;
bitmapImage.SetPixel(i,j,
新颜色((浮动)cR,(浮动)cG,(浮动)cB));
}
}
//应用所有设置像素更改
bitmapImage.Apply();
//将纹理连接到该脚本附加到的游戏对象的材质
Image.renderer.material.mainTexture=作为纹理的位图图像;
}
出于某种原因,此脚本将我的图像完全更改为黑色。然后,如果我将值增加到某一点,图像将完全变为白色

这里的亮度计算正确吗


顺便说一句,开始时我将亮度值从当前输入范围的0%到100%映射到-255到255,以使用此脚本。这样好吗?也就是说,有一个输入字段可以输入0到100的亮度值,要使用这个公式,我必须将50%的值映射到-255到255的比例/范围内50%的含义,对吗

这里我稍微修改了你的脚本,尝试30%到60%它仍然在与脚本相同的比例上只是颜色在0到1之间浮动,你如何计算它是0黑色还是1白色尝试这个,还有你没有得到图像的像素你得到位图图像的像素当你应该得到imgTexture的像素时我也把alpha放进去了如果你想使用alpha的图片,因为它不应该随着亮度的变化而改变

另外,我认为您可能应该保留Texture2D的第三个副本,以便每次都可以恢复它

public void AdjustBrightness(string brightness)
    {
        int brightnessInt = Convert.ToInt32(brightness);
        float mappedBrightness = (51 * brightnessInt) / 10 - 255;
        //Make an empty Texture the same same as the original 
        Texture2D bitmapImage = new Texture2D(imgTexture.width, imgTexture.height);

        if (mappedBrightness < -255) mappedBrightness = -255;
        if (mappedBrightness > 255) mappedBrightness = 255;
        Color color;
        for (int i = 0; i < bitmapImage.width; i++)
        {
            for (int j = 0; j < bitmapImage.height; j++)
            {
                color = imgTexture.GetPixel(i, j);
                float cR;
                float cG;
                float cB;
                float cA;
                if(color.a == 1f){
                cR = color.r + (mappedBrightness/255);
                cG = color.g + (mappedBrightness/255);
                cB = color.b + (mappedBrightness/255);
                    cA = color.a;


                if (cR < 0) cR = 0;
                if (cR > 255) cR = 255;

                if (cG < 0) cG = 0;
                if (cG > 255) cG = 255;

                if (cB < 0) cB = 0;
                if (cB > 255) cB = 255;
                }else{
                    cR = color.r;
                    cG = color.g;
                    cB = color.b;
                    cA = color.a;
                }

                bitmapImage.SetPixel(i, j,
                                     new Color(cR, cG, cB,cA));
            }
        }
        //Apply all SetPixel changes
        bitmapImage.Apply();

        //Connect texture to material of GameObject this script is attached to 
        imgTexture = bitmapImage;
    }
public亮度(字符串亮度)
{
int-brightnessInt=Convert.ToInt32(亮度);
浮点映射亮度=(51*brightnessInt)/10-255;
//使空纹理与原始纹理相同
Texture2D bitmapImage=新的Texture2D(imgTexture.width,imgTexture.height);
如果(mappedBrightness<-255)mappedBrightness=-255;
如果(mappedBrightness>255)mappedBrightness=255;
颜色;
对于(int i=0;i255)cR=255;
如果(cG<0)cG=0;
如果(cG>255)cG=255;
如果(cB<0)cB=0;
如果(cB>255)cB=255;
}否则{
cR=颜色。r;
cG=颜色,g;
cB=颜色。b;
cA=颜色。a;
}
bitmapImage.SetPixel(i,j,
新颜色(cR、cG、cB、cA);
}
}
//应用所有设置像素更改
bitmapImage.Apply();
//将纹理连接到该脚本附加到的游戏对象的材质
imgTexture=位图图像;
}
我想您正在使用。它的成员既不是
float
s也不是
int
s,而是
byte
s,从0到255

我认为当你改变亮度时,你应该按相同的因子缩放
r
g
,和
b
,而不是将相同的项目添加到它们中。请尝试以下代码:

public void AdjustBrightness(string brightnessString)
{
    float brightnessInput = Convert.ToFloat(brightnessString);
    if (brightnessInput < 0) brightnessInput = 0;
    if (brightnessInput > 100) brightnessInput = 100;
    float brightnessFactor = (brightnessInput/50.)*(brightnessInput/50.);
    //Make an empty Texture the same same as the original 
    Texture2D bitmapImage = new Texture2D(imgTexture.width, imgTexture.height);

    Color color;
    for (int i = 0; i < bitmapImage.width; i++)
    {
        for (int j = 0; j < bitmapImage.height; j++)
        {
            color = bitmapImage.GetPixel(i, j);
            int cR = int(color.r * brightnessFactor);
            int cG = int(color.g * brightnessFactor);
            int cB = int(color.b * brightnessFactor);

            if (cR > 255) cR = 255;

            if (cG > 255) cG = 255;

            if (cB > 255) cB = 255;

            bitmapImage.SetPixel(i, j, Color.FromArgb(cR, cG, cB));
        }
    }
        //Apply all SetPixel changes
        bitmapImage.Apply();

        //Connect texture to material of GameObject this script is attached to 
        Image.renderer.material.mainTexture = bitmapImage as Texture;
}
public void调整亮度(string brightnessString)
{
float brightnessInput=Convert.ToFloat(brightnessString);
如果(brightnessInput<0)brightnessInput=0;
如果(brightnessInput>100)brightnessInput=100;
浮点亮度因子=(亮度输入/50.)*(亮度输入/50.);
//使空纹理与原始纹理相同
Texture2D bitmapImage=新的Texture2D(imgTexture.width,imgTexture.height);
颜色;
对于(int i=0;i255)cR=255;
如果(cG>255)cG=255;
如果(cB>255)cB=255;
bitmapImage.SetPixel(i,j,Color.FromArgb(cR,cG,cB));
}
}
//应用所有设置像素更改
bitmapImage.Apply();
//将纹理连接到该脚本附加到的游戏对象的材质
Image.renderer.material.mainTexture=作为纹理的位图图像;
}
美国