none
调整亮度和对比度的算法出问题了,图片变得黑黑的。图片像素的位是怎样的? RRS feed

  • 问题

  • 代码如下:

    //调整亮度

           Bitmap^bitmap = gcnew Bitmap("F:\\girl.jpg");
                int w = bitmap->Width;
               int h = bitmap->Height;
               if (bitmap == nullptr)
               {
                  MessageBox::Show("木有图片!");
               }
               if (degree < -255) degree = -255;
               if (degree > 255) degree = 255;
              int pix = 0;  //初始亮度赋为0          
            //锁定位图对象至内存,存为24位
      Rectangle rt = Rectangle(0,0,w,h);
      System::Drawing::Imaging::BitmapData^ srcData = bitmap->LockBit(rt,ImageLockMode::ReadWrite,bitmap->PixelFormat);
      IntPtr p = srcData->Scan0;       //获取第一行开头的地址
      //用一个数组来存储位图的字节
       int bytes = Math::Abs(srcData->Stride) * h;
       array<Byte>^rgbValues = gcnew array<Byte>(bytes);
         //把RGB的值都复制到该数组中
       System::Runtime::InteropServices::Marshal::Copy( p, rgbValues, 0, bytes );
         //处理指定位置像素的亮度
      for ( int counter = 0; counter < rgbValues->Length; counter++)   //是不是在这里出问题呢?
      {
       //对每一个像素的RGB值同时加上或减去一个特定的值
          rgbValues[counter] = rgbValues[ counter ] + degree;
       //注意亮度的范围在-255和255之间
       if (degree < 0) rgbValues[counter] = Math::Max(0,pix);
                if (degree > 0) rgbValues[counter] = Math::Min(255, pix);
      }
            //把新的RGB值复制回位图中
            System::Runtime::InteropServices::Marshal::Copy( rgbValues, 0, p, bytes );
      //解锁位图
            bitmap->UnlockBits(srcData);

           pictureBox1->Image = bitmap; //调整对比度  

                 if (degree < -100) degree = -100;
                 if (degree > 100) degree = 100;
                 int pixel = 0;
                 double contrast = (100.0 + degree) / 100.0;
                 contrast *= contrast;
                 int w = bitmap->Width;
                 int h = bitmap->Height;
                 //锁定位图对象至内存,存为24位
           Rectangle rt = Rectangle(0,0,w,h);
           System::Drawing::Imaging::BitmapData^ srcData = bitmap->LockBits(rt,ImageLockMode::ReadWrite,bitmap->PixelFormat);
           IntPtr p = srcData->Scan0;       //获取第一行开头的地址
           //用一个数组来存储位图的字节
           int bytes = Math::Abs(srcData->Stride) * h;
                 array<Byte>^rgbValues = gcnew array<Byte>(bytes);
                 //把RGB的值都复制到该数组中
                 System::Runtime::InteropServices::Marshal::Copy( p, rgbValues, 0, bytes );
      
                // 处理指定位置像素的对比度
                 for (int counter = 0; counter<rgbValues->Length; counter++)
                  {
                        pixel = (int)(((rgbValues[counter] / 255- 0.5) * contrast + 0.5) * 255);
                        if (pixel < 0) pixel = 0;
                        if (pixel > 255) pixel = 255;
                        rgbValues[counter] = pixel;
     
                  }
                  //把新的RGB值复制回位图中
               System::Runtime::InteropServices::Marshal::Copy( rgbValues, 0, p, bytes );
         //解锁位图
               bitmap->UnlockBits(srcData);
             pictureBox1->Image = bitmap;


    新手上路,大神指点!

    2012年8月9日 10:11

答案

全部回复