vignette滤镜效果

vignette滤镜效果:把图像边缘像素变暗,中心元素变得明亮。

首先看看效果,原图如下:

vignette滤镜效果_第1张图片

滤镜处理后的效果:

实现思路:

变暗——对彩色图像而言,对每个像素的三个通道即RGB分别减去一个相同的值v(保证变化后的像素值在0~255之间)。

v值的确定——p*当前像素点point距离中点的距离的平方/图像四个顶点到中点的距离的平方。(p的值可控制)

关键代码(项目中一部分,只用领会其思路即可)如下:

void qt_test::slotProcessImage(int num)
{
    cvCopy(this->image,this->imageCopy,NULL);
    float value = 0.5;
    cv::Mat imageMat(this->imageCopy,0);
    int width = imageMat.cols;
    int height = imageMat.rows;
    int centerX = width>>1;
    int centerY = height>>1;
    int maxDis = centerX*centerX+centerY*centerY;
    float diff = maxDis*value;
    float ratio = width>height?height/width:width/height;
    for(int i =0;i<height;i++)
    {
        uchar *imageRow = imageMat.ptr<uchar>(i);
        for(int j=0;j<width;j++)
        {
            int b = imageRow[3*j];
            int g = imageRow[3*j+1];
            int r = imageRow[3*j+2];
            float dx = j-centerX;
            float dy = i-centerY;
            if(width>height)
            {
                dx = dx*ratio;
            }
            else
            {
                dy = dy*ratio;
            }
            float dis = dx*dx+dy*dy;
            float v = num*dis/diff;
            b = (int)(b-v);
            g = (int)(g-v);
            r = (int)(r-v);
            b = (b>255?255:(b<0?0:b));
            g = (g>255?255:(g<0?0:g));
            r = (r>255?255:(r<0?0:r));
            this->imageCopy->imageData[this->image->widthStep*i+3*j]=(uchar)b;
            this->imageCopy->imageData[this->image->widthStep*i+3*j+1]=(uchar)g;
            this->imageCopy->imageData[this->image->widthStep*i+3*j+2]=(uchar)r;
        }
    }
    if(this->isImageLoaded)
    {
       
        this->loatPicture(this->width(),this->height(),this->imageCopy);
    }
}




你可能感兴趣的:(opencv,图像处理,vignette滤镜)