灰度图拉伸

<!-- lang: cpp -->

//灰度图拉伸
void GrayStretch(const IplImage img_src,IplImage img_dst)
{

//img_src 和 img_dst 均为单通道
uchar *gray_src, *gray_dst;
double meanPix,maxPix,minPix;


uchar t1 ;
uchar t2 ;
//cvShowImage("img_src",img_src);
//cvWaitKey(0);
meanPix = cvMean(img_src);
cvMinMaxLoc(img_src,&minPix,&maxPix);
t1 = minPix+(meanPix-minPix)/4;
t2 = maxPix-(maxPix-meanPix)/4;
for( int i=0;i<img_src->height;i++)
    for( int j=0;j<img_src->width;j++)
    {
        gray_src = (uchar*)(img_src->imageData + i*img_src->widthStep + 1*j);
        gray_dst = (uchar*)(img_dst->imageData + i*img_dst->widthStep + 1*j);
        if( gray_src[0] <= t1 )
        {
            gray_dst[0] = 0; 
        }
        else if( gray_src[0] >= t2 ) 
        {
            gray_dst[0] = 255;
        }
        else 
        {
            gray_dst[0] = ( gray_src[0] - t1 )*255/(t2-t1);
        }
    }

}

你可能感兴趣的:(灰度图拉伸)