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 7976135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:50:33+00:00 2026-06-04T08:50:33+00:00

I have an image with values ranging from 0 to 1. What I like

  • 0

I have an image with values ranging from 0 to 1. What I like to do is simple averaging.
But, more specifically, for a cell at the border of the image I’d like to compute the average of the pixels for that part of the neighbourhood/kernel that lies within the extent of the image. In fact this boils down to adapt the denominator of the ‘mean formula’, the number of pixels you divide the sum by.

I managed to do this as shown below with scipy.ndimage.generic_filter, but this is far from time-efficient.

def fnc(buffer, count):
    n = float(sum(buffer < 2.0))
    sum = sum(buffer) - ((count - b) * 2.0)
    return (sum / n)

avg = scipy.ndimage.generic_filter(image, fnc, footprint = kernel, \
                                   mode = 'constant', cval = 2.0,   \
                                   extra_keywords = {'count': countkernel})

Details

  • kernel = square array (circle represented by ones)
  • Padding with 2’s and not by zeroes since then I could not properly separate zeroes of the padded area and zeroes of the actual raster
  • countkernel = number of ones in the kernel
  • n = number of cells that lie within image by excluding the cells of the padded area identified by values of 2
  • Correct the sum by subtracting (number of padded cells * 2.0) from the original neighbourhood total sum

Update(s)

1) Padding with NaNs increases the calculation with about 30%:

    def fnc(buffer):
        return (numpy.nansum(buffer) / numpy.sum([~numpy.isnan(buffer)]))

    avg = scipy.ndimage.generic_filter(image, fnc, footprint = kernel, \
                                       mode = 'constant', cval = float(numpy.nan)

2) Applying the solution proposed by Yves Daoust (accepted answer), definitely reduces the processing time to a minimum:

    def fnc(buffer):
        return numpy.sum(buffer)

    sumbigimage = scipy.ndimage.generic_filter(image, fnc, \
                                               footprint = kernel, \
                                               mode = 'constant', \
                                               cval = 0.0)
    summask     = scipy.ndimage.generic_filter(mask, fnc, \
                                               footprint = kernel, \
                                               mode = 'constant', \
                                               cval = 0.0)
    avg = sumbigimage / summask

3) Building on Yves’ tip to use an additional binary image, which in fact is applying a mask, I stumbled upon the principle of masked arrays. As such only one array has to be processed because a masked array ‘blends’ the image and mask arrays together.
A small detail about the mask array: instead of filling the inner part (extent of original image) with 1’s and filling the outer part (border) with 0’s as done in the previous update, you must do vice versa. A 1 in a masked array means ‘invalid’, a 0 means ‘valid’.
This code is even 50% faster then the code supplied in update 2):

    maskedimg = numpy.ma.masked_array(imgarray, mask = maskarray)

    def fnc(buffer):
        return numpy.mean(buffer)

    avg = scipy.ndimage.generic_filter(maskedimg, fnc, footprint = kernel, \
                                       mode = 'constant', cval = 0.0)

–> I must correct myself here!
I must be mistaken during the validation, since after some calculation runs it seemed that scipy.ndimage.<filters> cannot handle masked_arrays in that sense that during the filter operation the mask is not taken into account.
Some other people mentioned this too, like here and here.


The power of an image…

  • grey: extent of image to be processed
  • white: padded area (in my case filled with 2.0’s)
  • red shades: extent of kernel
    • dark red: effective neighbourhoud
    • light red: part of neighbourhood to be ignored

enter image description here


How can this rather pragmatical piece of code be changed to improve performance of the calculation?

Many thanks in advance!

  • 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-04T08:50:35+00:00Added an answer on June 4, 2026 at 8:50 am

    Unsure if this will help, as I am not proficient in scipy: use an auxiliary image of 1’s in the gray area and 0’s in the white area (0’s too in the source image). Then apply the filter to both images with a simple sum.

    There is some hope of a speedup if scipy provides a specialized version of the filter with a built-in function for summing.

    This done, you will need to divide both images pixel by pixel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a greyscale image which has pixel values ranging from 1.000 to 1.003.
i have one text field and setting its name & values from php, like:
I have a simple image that I'm showing with imshow in matplotlib. I'd like
I have a list populated from a sqlite database with data values ranging from
i have integer pixel array of an image with values like -1100791, -16777056 and
I have an image and want to read the pixel values in a particular
I have this: Create Proc CrearNuevoImagen @imagen image AS INSERT INTO Imagenes(imagen) VALUES( @imagen
So I have Image like this (source: de-viz.ru ) I want to get something
So I have Image like this (source: de-viz.ru ) I want to get something
I have an OpenCV matrix of double (CV_32F) values. I'd like to save it

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.