调整图像亮度 [英] Adjusting image brightness

查看:134
本文介绍了调整图像亮度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Windows Phone应用程序,当我通过滑块调整亮度时,它工作正常,当I
向右移动。但是当我回到以前的位置,而不是图像变暗,它变得更明亮和更明亮。这是我基于像素操作的代码。

  private void slider1_ValueChanged(object sender,RoutedPropertyChangedEventArgs< double> ; e)
{

wrBmp = new WriteableBitmap(Image1,null);


for(int i = 0; i {
int pixel = wrBmp.Pixels [i] ;
int B =(int)(pixel& 0xFF);像素>> = 8;
int G =(int)(pixel& 0xFF);像素>> = 8;
int R =(int)(pixel& 0xFF);像素>> = 8;
int A =(int)(pixel);

B + =(int)slider1.Value; R + =(int)slider1.Value; G + =(int)slider1.Value;
if(R> 255)R = 255;如果(G> 255)G = 255; if(B> 255)B = 255;
if(R< 0)R = 0;如果(G <0)G = 0;如果(B <0)B = 0;
wrBmp.Pixels [i] = B | (G <8)| (R <16)| (A << 24);
}
wrBmp.Invalidate();
Image1.Source = wrBmp;

}

我缺少什么,中。我正在与手机中的小图像一样工作。我已经尝试复制原始图像复制一个。我认为代码是完美的,经过大量的研究,我发现问题是由于滑块value.Possible解决方案是分配初始值到滑块。我想要一些代码帮助。



private double lastSlider3Vlaue;
private void slider3_ValueChanged(object sender,`RoutedPropertyChangedEventArgs e)
{

  if )return; 
double [] contrastArray = {1,1.2,1.3,1.6,1.7,1.9,2.1,2.4,2.6,2.9};
double CFactor = 0;
int nIndex = 0;
nIndex =(int)slider3Value - (int)lastSlider3Vlaue;
if(nIndex <0)
{
nIndex =(int)lastSlider3Vlaue - (int)slider3.Value;
this.lastSlider3Vlaue = slider3.Value;
CFactor = contrastArray [nIndex];
}
else
{
nIndex =(int)slider3.Value - (int)lastSlider3Vlaue;
this.lastSlider3Vlaue = slider3.Value;
CFactor = contrastArray [nIndex];
}

WriteableBitmap wbOriginal;
wbOriginal = new WriteableBitmap(Image1,null);
wrBmp = new WriteableBitmap(wbOriginal.PixelWidth,wbOriginal.PixelHeight);
wbOriginal.Pixels.CopyTo(wrBmp.Pixels,0);
int h = wrBmp.PixelHeight;
int w = wrBmp.PixelWidth;
for(int i = 0; i {
int pixel = wrBmp.Pixels [i];
int B =(int)(pixel& 0xFF);像素>> = 8;
int G =(int)(pixel& 0xFF);像素>> = 8;
int R =(int)(pixel& 0xFF);像素>> = 8;
int A =(int)(pixel);

R =(int)(((R-128)* CFactor)+ 128);
G =(int)(((G-128)* CFactor)+ 128);
B =(int)(((B-128)* CFactor)+ 128);

if(R> 255)R = 255;如果(G> 255)G = 255; if(B> 255)B = 255;
if(R< 0)R = 0;如果(G <0)G = 0;如果(B <0)B = 0;
wrBmp.Pixels [i] = B | (G <8)| (R <16)| (A << 24);
}

wrBmp.Invalidate();
Image1.Source = wrBmp;



}



当向前滑动时,rgb值是连续减小的,但当向后滑动时,rgb值也随着增加而减小。
请帮助iam从过去三个月以来工作。除此之外,你还可以告诉我如何完成整个图像处理。

解决方案

您的演算法有误。每次滑块的值更改时,您都要将该值添加到图片的亮度。您的逻辑缺陷是,滑块返回的值总是为正,您总是将亮度添加到相同图片中。



因此,如果滑块以10的值开始,则会为图片的亮度添加10。



然后,我滑动到5。将上一张图片的亮度(已添加10的亮度的亮度)添加到5。



解决问题的两种方法: p>


  1. 保留原始图片的副本,并在每次调用方法时复制。然后将亮度添加到副本(而不是原始)。这是最安全的方法。


  2. 不是添加滑块的新绝对值,而是计算相对值(自上次方法调用:

      private double lastSliderValue; 

    private void slider1_ValueChanged(object sender,RoutedPropertyChangedEventArgs< double> e )
    {
    var offset = slider1.Value - this.lastSliderValue;
    this.lastSliderValue = slider1.Value;

    //在这里插入旧的算法,但是将slider1.Value的出现次数替换为offset
    }


第二种方法可能导致一些头痛,你的算法将RGB值上限为255.在这些情况下,你丢失信息,不能恢复到旧状态,例如,滑块值为255的极端示例。算法将所有像素设置为255,从而生成白色图片,然后将滑块减小到0,这在理论上应该恢复原始图片。在这种情况下,你将为每个像素减去255,但是因为每个像素的值为255,所以最终会得到一个黑色的图片。



因此,除非找到一个聪明的方法来解决第二个解决方案中提到的问题,我建议使用第一个解决方案。


For windows phone app, when I am adjusting brightness by slider it works fine when I move it to right. But when I go back to previous position, instead of image darkening, it goes brighter and brighter. Here is my code based on pixel manipulation.

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {

        wrBmp = new WriteableBitmap(Image1, null);


        for (int i = 0; i < wrBmp.Pixels.Count(); i++)
        {
            int pixel = wrBmp.Pixels[i];
            int B = (int)(pixel & 0xFF); pixel >>= 8;
            int G = (int)(pixel & 0xFF); pixel >>= 8;
            int R = (int)(pixel & 0xFF); pixel >>= 8;
            int A = (int)(pixel);

            B += (int)slider1.Value; R += (int)slider1.Value; G += (int)slider1.Value;
            if (R > 255) R = 255; if (G > 255) G = 255; if (B > 255) B = 255;
            if (R < 0) R = 0; if (G < 0) G = 0; if (B < 0) B = 0;
            wrBmp.Pixels[i] = B | (G << 8) | (R << 16) | (A << 24);
        }
        wrBmp.Invalidate();
        Image1.Source = wrBmp;

    }

What am I missing and is there any problem with slider value. I am working with small images as usual in mobiles. I have already tried copying original image to duplicate one. I think code is perfect, after a lot of research I found that the problem is due to slider value.Possible solution is assigning initial value to slider. I want some code help.

private double lastSlider3Vlaue; private void slider3_ValueChanged(object sender,`RoutedPropertyChangedEventArgs e) {

        if (slider3 == null) return;
        double[] contrastArray = { 1, 1.2, 1.3, 1.6, 1.7, 1.9, 2.1, 2.4, 2.6, 2.9 };
        double CFactor = 0;
        int nIndex = 0;
        nIndex = (int)slider3.Value - (int)lastSlider3Vlaue;
        if (nIndex < 0)
        {
            nIndex = (int)lastSlider3Vlaue - (int)slider3.Value;
            this.lastSlider3Vlaue = slider3.Value;
            CFactor = contrastArray[nIndex];
        }
        else
        {
            nIndex = (int)slider3.Value - (int)lastSlider3Vlaue;
            this.lastSlider3Vlaue = slider3.Value;
            CFactor = contrastArray[nIndex];
        }

        WriteableBitmap wbOriginal;
        wbOriginal = new WriteableBitmap(Image1, null);
        wrBmp = new WriteableBitmap(wbOriginal.PixelWidth, wbOriginal.PixelHeight);
        wbOriginal.Pixels.CopyTo(wrBmp.Pixels, 0);
        int h = wrBmp.PixelHeight;
        int w = wrBmp.PixelWidth;
        for (int i = 0; i < wrBmp.Pixels.Count(); i++)
        {
            int pixel = wrBmp.Pixels[i];
            int B = (int)(pixel & 0xFF); pixel >>= 8;
            int G = (int)(pixel & 0xFF); pixel >>= 8;
            int R = (int)(pixel & 0xFF); pixel >>= 8;
            int A = (int)(pixel);

            R = (int)(((R - 128) * CFactor) + 128);
            G = (int)(((G - 128) * CFactor) + 128);
            B = (int)(((B - 128) * CFactor) + 128);

            if (R > 255) R = 255; if (G > 255) G = 255; if (B > 255) B = 255;
            if (R < 0) R = 0; if (G < 0) G = 0; if (B < 0) B = 0;
            wrBmp.Pixels[i] = B | (G << 8) | (R << 16) | (A << 24);
        }

        wrBmp.Invalidate();
        Image1.Source = wrBmp;

}

After debugging I found that the r g b values are decreasing continuosly when sliding forward,but when sliding backwards it is also decreasing where as it shoul increase. please help iam working on this since past last three months.Besides this u also give me advice about how i can complete this whole image processing

解决方案

Your algorithm is wrong. Each time the slider's value changes, you're adding that value to the picture's brightness. What makes your logic flawed is that the value returned by the slider will always be positive, and you're always adding the brightness to the same picture.

So, if the slider starts with a value of 10, I'll add 10 to the picture's brightness.

Then, I slide to 5. I'll add 5 to the previous picture's brightness (the one you already added 10 of brightness to).

Two ways to solve the issue:

  1. Keep a copy of the original picture, and duplicate it every time your method is called. Then add the brightness to the copy (and not the original). That's the safest way.

  2. Instead of adding the new absolute value of the slider, calculate the relative value (how much it changed since the last time the method was called:

    private double lastSliderValue;
    
    private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        var offset = slider1.Value - this.lastSliderValue;
        this.lastSliderValue = slider1.Value;
    
        // Insert your old algorithm here, but replace occurences of "slider1.Value" by "offset"
    }
    

This second way can cause a few headaches though. Your algorithm is capping the RGB values to 255. In those cases, you are losing information and cannot revert back to the old state. For instance, take the extreme example of a slider value of 255. The algorithm sets all the pixels to 255, thus generating a white picture. Then you reduce the slider to 0, which should in theory restore the original picture. In this case, you'll subtract 255 to each pixel, but since every pixel's value is 255 you'll end up with a black picture.

Therefore, except if you find a clever way to solve the issue mentionned in the second solution, I'd recommand going with the first one.

这篇关于调整图像亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆