[code]图像亮度调整enhancement

  • //draft     2013.9
    
    //F=X2/u;
    ////远处细节被淹没。  亮的地方增亮明显,暗的地方更暗。  不可取。
    // CvScalar rgb;
    // rgb=cvAvg(src);
    //for(y=0;y<src->height;y++)
    // {
    // unsigned char *srcrow= (unsignedchar*)(src->imageData+y*src->widthStep);
    // for(x=0;x<src->width;x++)
    // {  
    //srcrow[3*x]=srcrow[3*x]*srcrow[3*x]/rgb.val[0]>255 ?255:srcrow[3*x]*srcrow[3*x]/rgb.val[0];
    //srcrow[3*x+1]=srcrow[3*x+1]*srcrow[3*x+1]/rgb.val[1]>255? 255:srcrow[3*x+1]*srcrow[3*x+1]/rgb.val[1] ;
    //srcrow[3*x+2]=srcrow[3*x+2]*srcrow[3*x+2]/rgb.val[2]>255? 255:srcrow[3*x+2]*srcrow[3*x+2]/rgb.val[2];
    // }
    // }
    
    #ifdef Enhencement_Linear
     
    //      255|     ________
    //        |     /
    //        |    /
    //        |   /
    //        |  /
    //        | /
    //        |/_____.______.
    //        0     A   255  
    int b=20;//上移b
    for(y=0;y<src->height;y++)
    {
    unsigned char *srcrow= (unsignedchar*)(src->imageData+y*src->widthStep);
    for (x=0;x<src->width;x++)
    {  
    
    if (srcrow[3*x]>a)srcrow[3*x]=255;
    else
    srcrow[3*x]=(255-b)*srcrow[3*x]/(a)+b>255 ?255:(255-b)*srcrow[3*x]/(a)+b;
    
    if (srcrow[3*x+1]>a) srcrow[3*x+1]=255;
    else 
    srcrow[3*x+1]=(255-b)*srcrow[3*x+1]/(a)+b>255 ?255:(255-b)*srcrow[3*x+1]/(a)+b;
    
    if (srcrow[3*x+2]>a)srcrow[3*x+2]=255;
    else
    srcrow[3*x+2]=(255-b)*srcrow[3*x+2]/(a)+b>255 ?255:(255-b)*srcrow[3*x+2]/(a)+b;
    }
    }
    #endif
    
    
    ////改进log函数
    //#ifdef Enhencement_LOG
    // //CvScalar rgb;
    // //rgb=cvAvg(src);
    // double aa,bb,cc,aaaaa;aaaaa=255.0;
    // k=(a-50)/255.0;
    //for(y=0;y<src->height;y++)
    // {
    // unsigned char *srcrow= (unsignedchar*)(src->imageData+y*src->widthStep);
    // for(x=0;x<src->width;x++)
    // {  
    //aa=srcrow[3*x]+0.0;bb=(double)srcrow[3*x+1]+0.0;cc=(double)srcrow[3*x+2]+0.0;
    // aa=log(aaaaa)-log(aa);
    // bb=log(aaaaa)-log(bb);
    // cc=log(aaaaa)-log(cc);
    // //if (srcrow[3*x]>a);
    // //else
    //srcrow[3*x]=srcrow[3*x]+k*srcrow[3*x]*aa<srcrow[3*x]? srcrow[3*x]:srcrow[3*x]+k*srcrow[3*x]*aa;
    // //srcrow[3*x]=srcrow[3*x]+k*srcrow[3*x]*aa;
    // //if (srcrow[3*x+1]>a);
    // //else
    //srcrow[3*x+1]=srcrow[3*x+1]+k*srcrow[3*x+1]*bb<srcrow[3*x+1]? srcrow[3*x+1]:srcrow[3*x+1]+k*srcrow[3*x+1]*bb;
    // //srcrow[3*x+1]=srcrow[3*x+1]+k*srcrow[3*x+1]*bb;
    // //if (srcrow[3*x+2]>a);
    // //else
    // srcrow[3*x+2]=srcrow[3*x+2]+k*srcrow[3*x+2]*cc<srcrow[3*x+2] ?srcrow[3*x+2]:srcrow[3*x+2]+k*srcrow[3*x+2]*cc;
    // //srcrow[3*x+2]=srcrow[3*x +2]+k*srcrow[3*x+2]*cc;
    // }
    // }
    //#endif
    
    
    
    
    
    //
    // //cvCvtColor(src,src,CV_BGR2YCrCb);
    ////转换颜色空间
    #ifdef SHARPEN_Y   //锐化
    unsigned char x1,x2,x3,x4,x5,x6,x7,x8,x9;
    int n1,n2,n3,n4,n5,n6,n7,n8;
    IplImage* srctem=cvCreateImage(cvGetSize(src),8,3);
    cvCopy(src,srctem);
    //cvSmooth(srctem,srctem);
    for(int y=1;yheight-1;y++)    
    {
    unsigned char *row1= (unsignedchar*)(srctem->imageData+(y-1)*srctem->widthStep);
    unsigned char *row2= (unsignedchar*)(srctem->imageData+y*srctem->widthStep);
    unsigned char *row3= (unsignedchar*)(srctem->imageData+(y+1)*srctem->widthStep);
    
    unsigned char *row= (unsignedchar*)(src->imageData+y*src->widthStep);
    
    for (int x=1;xwidth-1;x++)
    
    {
    x1=row1[3*(x-1)]; x2=row1[3*x]; x3=row1[3*(x+1)]; 
    x4=row2[3*(x-1)]; x5=row2[3*x]; x6=row2[3*(x+1)]; 
    x7=row3[3*(x-1)]; x8=row3[3*x]; x9=row3[3*(x+1)];   
    //Laplace
    //n1=x5<<3;n2=x1+x2+x3+x4+x6+x7+x8+x9;//n1+=x5;   //[8 -1..........-1]
    n1=x5<<2;//n1+=x5;                          //[4 -1 -1-1 -1]
    n2=x2+x4+x6+x8;
    n1=(n1-n2)/3+x5;  //锐¨?化¡¥
    if(n1<0) n1=0; else if(n1>255)n1=255;//else n1=255;
    row[3*x]=n1;
    //row[3*x+1]=128;
    //row[3*x+2]=128;
    
    }
    }
    #endif
    // //cvCvtColor(src,src,CV_YCrCb2BGR);


原文地址:https://www.cnblogs.com/eaglediao/p/7136506.html