Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:40:27+00:00 2026-06-16T02:40:27+00:00

I wrote a Matlab function to compute the per-pixel contrast (i.e., the difference between

  • 0

I wrote a Matlab function to compute the per-pixel contrast (i.e., the difference between the center pixel of a window (3×3) and the mean of all the pixels in the window, and the difference over the standard deviation of all the pixels in the window).

The code runs very slow for an 1024×1024 gray scale image. Is there any method to speed up the code? Thanks!

function [ imContrast ] = LocalContrast( im )
% [ imContrast ] = LocalContrast( im )
%   Compute the contrast between a center contrast and its neighbors.
%
%   Equation:   window_size = 3x3
%               x_contrast = (x_center - mu) / std(pixels_in_window)
%               mu: mean of the pixels' gray values in the window
%
%   Input:
%       im - original image in gray scale
%
%   Output:
%       imContrast - feature matrix of contrast, same size of im

[rows, cols] = size(im);
imContrast = double(zeros(size(im)));

% Boundary - keep the gray values of those in im
imContrast(1,:) = im(1,:) / 255;
imContrast(rows,:) = im(rows,:) / 255;
imContrast(:,1) = im(:,1) / 255;
imContrast(:,cols) = im(:,cols) / 255;

% Compute contrast for each pixel
for x = 2:(rows-1)
    for y = 2:(cols-1)

        winPixels = [ im(x-1,y-1), im(x-1,y), im(x-1,y+1),...
                      im(x,y-1), im(x,y), im(x,y+1),...
                      im(x+1,y-1), im(x+1,y), im(x+1,y+1)];

        winPixels = double(winPixels);

        mu = mean(winPixels);
        stdWin = std(winPixels);
        imContrast(x,y) = (double(im(x,y)) - mu) / stdWin;
    end
end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:40:28+00:00Added an answer on June 16, 2026 at 2:40 am

    Here’s a way around it that uses the image processing toolbox. Given your image is called im ,

    the mean difference (MD):

     MD = im - imfilter(im,fspecial('average',[3 3]),'same');
    

    the standard deviation difference (SDD):

     SDD = im - stdfilt(im, ones(3));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have 10 data files and I wrote a MATLAB function to process
What is the difference between hist and imhist functions in Matlab? I have a
here's my matlab problem. I need to write a matlab function that copies all
I have a couple of MatLab function, and almost all of them have a
So I have a matlab function I wrote that takes in a number and
I wrote a code in matlab to compute an integral using Gauss-Chebyshev quadrature ,
I'm all new to Matlab and I'm supposed to use this function to find
I wanted to declare a nonlinear complicated function in Matlab, so I wrote this
How can I convert this script into a MATLAB function? clear all; set={AB02XY_1,ZT99ER_1,UI87GP_1}; fileData1
Earlier I wrote a code in Matlab for this sort of lottery function, just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.