如何在matlab/octave中实现图像的韦伯对比度?

发布于 2024-11-04 09:01:38 字数 174 浏览 3 评论 0原文

我想获得图像的每像素局部对比度,我猜韦伯对比度 是一个合理的公式。问题是,假设我们只使用相邻像素(例如,5x5)作为 Ib,如何在 matlab 中得到它?

I would like to get per-pixel local contrast of an image, and I guess Weber contrast is a plausible formula for that. Question is, how to get it in matlab assuming we just use neighboring pixels (ex, 5x5) for Ib?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2024-11-11 09:01:38

为了计算局部对比度,我们需要亮度图像和局部背景亮度。

假设我们已经有一个亮度图像,可以通过取局部区域中所有像素的平均值来计算特定像素的局部背景亮度。

我们可以通过在水平和垂直方向上重复将图像移动 0:(n-1) 个像素来收集一组此类局部背景区域(原始图像中的每个像素一个)。

以下演示函数说明了基本思想:

function weberContrastDemo

    imgWidthPixels            = 1024;
    imgHeightPixels           = 1024;
    localBackgroundSizePixels = 5; % square patch

    luminance     = randn( imgHeightPixels, imgWidthPixels );
    luminance( 496:528, 496:528 ) = 20;
    background    = localMeanFilter( luminance, localBackgroundSizePixels );
    weberContrast = ( luminance - background );% ./ background;

    imagesc( weberContrast );
    title( 'Weber Contrast' );
    colormap(gray);

end

function filteredImg = localMeanFilter( img, regionSizePixels )

    offsetImages  = getOffsetImages( img, regionSizePixels );
    filteredImg   = mean( offsetImages, 3 );

end

function buffer = getOffsetImages( img, regionSizePixels )
% GETOFFSETIMAGES

    imgSize      = size( img );
    imgHeight    = imgSize( 1 );
    imgWidth     = imgSize( 2 );

    minDelta     = 0;
    maxDelta     = (regionSizePixels-1);

    bufferWidth  = imgWidth  + maxDelta;
    bufferHeight = imgHeight + maxDelta;
    bufferDepth  = regionSizePixels .^ 2;
    bufferSize   = [ bufferHeight bufferWidth bufferDepth ];
    buffer       = zeros( bufferSize );

    iSample = 0;

    for deltaX = minDelta:maxDelta

        iStartX = 1        + deltaX;
        iEndX   = imgWidth + deltaX;
        idxX    = iStartX:iEndX;

        for deltaY = minDelta:maxDelta

            iSample = iSample + 1;

            iStartY = 1         + deltaY;
            iEndY   = imgHeight + deltaY;
            idxY    = iStartY:iEndY;

            buffer( idxY, idxX, iSample ) = img;

        end
    end

    iMin   = ceil(regionSizePixels/2);
    iMax   = iMin + (imgWidth-1);
    buffer = buffer( iMin:iMax, iMin:iMax, : );

end % GETOFFSETIMAGES

对于真正的心理物理学实验,我们希望将参数从弧度或球面度转换为像素,并且需要校准显示器或图像捕获设备,以便亮度测量准确。

To calculate local contrast we need a luminance image, and local background luminance.

Assuming we already have a luminance image, the local background luminance for a particular pixel can be calculated by taking the average over all pixels in the local region.

We can collect a set of such local background regions (one per pixel in the original image) by repeatedly shifting the image by 0:(n-1) pixelsin both horizontal and vertical directions.

The following demo function illustrates the basic idea:

function weberContrastDemo

    imgWidthPixels            = 1024;
    imgHeightPixels           = 1024;
    localBackgroundSizePixels = 5; % square patch

    luminance     = randn( imgHeightPixels, imgWidthPixels );
    luminance( 496:528, 496:528 ) = 20;
    background    = localMeanFilter( luminance, localBackgroundSizePixels );
    weberContrast = ( luminance - background );% ./ background;

    imagesc( weberContrast );
    title( 'Weber Contrast' );
    colormap(gray);

end

function filteredImg = localMeanFilter( img, regionSizePixels )

    offsetImages  = getOffsetImages( img, regionSizePixels );
    filteredImg   = mean( offsetImages, 3 );

end

function buffer = getOffsetImages( img, regionSizePixels )
% GETOFFSETIMAGES

    imgSize      = size( img );
    imgHeight    = imgSize( 1 );
    imgWidth     = imgSize( 2 );

    minDelta     = 0;
    maxDelta     = (regionSizePixels-1);

    bufferWidth  = imgWidth  + maxDelta;
    bufferHeight = imgHeight + maxDelta;
    bufferDepth  = regionSizePixels .^ 2;
    bufferSize   = [ bufferHeight bufferWidth bufferDepth ];
    buffer       = zeros( bufferSize );

    iSample = 0;

    for deltaX = minDelta:maxDelta

        iStartX = 1        + deltaX;
        iEndX   = imgWidth + deltaX;
        idxX    = iStartX:iEndX;

        for deltaY = minDelta:maxDelta

            iSample = iSample + 1;

            iStartY = 1         + deltaY;
            iEndY   = imgHeight + deltaY;
            idxY    = iStartY:iEndY;

            buffer( idxY, idxX, iSample ) = img;

        end
    end

    iMin   = ceil(regionSizePixels/2);
    iMax   = iMin + (imgWidth-1);
    buffer = buffer( iMin:iMax, iMin:iMax, : );

end % GETOFFSETIMAGES

For a real psychophysical experiment we would want to convert parameters from radians or steradians to pixels, and would need to calibrate the display or image capture device so the luminance measure is accurate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文