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

  • Home
  • SEARCH
  • 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 9178267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:30:43+00:00 2026-06-17T17:30:43+00:00

I have an image, stored in a numpy array of uint8 s, of shape

  • 0

I have an image, stored in a numpy array of uint8s, of shape (planes, rows, cols). I need to compare it to the values stored in a mask, also of uint8s, of shape (mask_rows, mask_cols). While the image may be very large, the mask is usually smallish, normally (256, 256) and is to be tiled over image. To simplify the code, lets pretend that rows = 100 * mask_rows and cols = 100 * mask_cols.

The way I am currently handling this thresholding is something like this:

out = image >= np.tile(mask, (image.shape[0], 100, 100))

The largest array I can process this way before getting slapped in the face with a MemoryError is a little larger than (3, 11100, 11100). The way I figured it, doing things this way I have up to three ginormous arrays coexisting in memory: image, the tiled mask, and my return out. But the tiled mask is the same little array copied over and over 10,000 times. So if I could spare that memory, I would use only 2/3 the memory, and should be able to process images 3/2 larger, so of size around (3, 13600, 13600). This is, by the way, consistent with what I get if I do the thresholding in place with

np.greater_equal(image, (image.shape[0], 100, 100), out=image)

My (failed) attempt at exploiting the periodic nature of mask to process larger arrays has been to index mask with periodic linear arrays:

mask = mask[None, ...]
rows = np.tile(np.arange(mask.shape[1], (100,))).reshape(1, -1, 1)
cols = np.tile(np.arange(mask.shape[2], (100,))).reshape(1, 1, -1)
out = image >= mask[:, rows, cols]

For small arrays it does produce the same result as the other one, although with something of a 20x slowdown(!!!), but it terribly fails to perform for the larger sizes. Instead of a MemoryError it eventually crashes python, even for values that the other method handles with no problems.

What I think is happening is that numpy is actually constructing the (planes, rows, cols) array to index mask, so not only is there no memory saving, but since it is an array of int32s, it is actually taking four times more space to store…

Any ideas on how to go about this? To spare you the trouble, find below some sandbox code to play around with:

import numpy as np

def halftone_1(image, mask) :
    return np.greater_equal(image, np.tile(mask, (image.shape[0], 100, 100)))

def halftone_2(image, mask) :
    mask = mask[None, ...]
    rows = np.tile(np.arange(mask.shape[1]),
                   (100,)).reshape(1, -1, 1)
    cols = np.tile(np.arange(mask.shape[2]),
                   (100,)).reshape(1, 1, -1)
    return np.greater_equal(image, mask[:, rows, cols])

rows, cols, planes = 6000, 6000, 3
image = np.random.randint(-2**31, 2**31 - 1, size=(planes * rows * cols // 4))
image = image.view(dtype='uint8').reshape(planes, rows, cols)
mask = np.random.randint(256,
                         size=(1, rows // 100, cols // 100)).astype('uint8')

#np.all(halftone_1(image, mask) == halftone_2(image, mask))
#halftone_1(image, mask)
#halftone_2(image, mask)

import timeit
print timeit.timeit('halftone_1(image, mask)',
                    'from __main__ import halftone_1, image, mask',
                    number=1)
print timeit.timeit('halftone_2(image, mask)',
                    'from __main__ import halftone_2, image, mask',
                    number=1)
  • 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-17T17:30:44+00:00Added an answer on June 17, 2026 at 5:30 pm

    I would almost have pointed you to a rolling window type of trick, but for this simple non-overlapping thing, normal reshape does it just as well. (the reshapes here are safe, numpy will never make a copy for them)

    def halftone_reshape(image, mask):
        # you can make up a nicer reshape code maybe, it is a bit ugly. The
        # rolling window code can do this too (but much more general then reshape).
        new_shape = np.array(zip(image.shape, mask.shape))
        new_shape[:,0] /= new_shape[:,1]
        reshaped_image = image.reshape(new_shape.ravel())
    
        reshaped_mask = mask[None,:,None,:,None,:]
    
        # and now they just broadcast:
        result_funny_shaped = reshaped_image >= reshaped_mask
    
        # And you can just reshape it back:
        return result_funny_shaped.reshape(image.shape)
    

    And since timings are everything (not really but…):

    In [172]: %timeit halftone_reshape(image, mask)
    1 loops, best of 3: 280 ms per loop
    
    In [173]: %timeit halftone_1(image, mask)
    1 loops, best of 3: 354 ms per loop
    
    In [174]: %timeit halftone_2(image, mask)
    1 loops, best of 3: 3.1 s per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image stored in a numpy array, as yielded by imread() :
In numpy/scipy I have an image stored in an array. I can display it,
I have a project in which I need to display thumbnails of image stored
Hi i have a bitmap image data stored in a variable , i need
I have an 8bit Image (stored in an Array) containing black(0) and white(255) pixels.
I have an image, including image header, stored in a c# byte array (byte
Possible Duplicate: base64 encoded images in email signatures I have a image stored in
I have currently got imagepaths stored within the database and image files stored in
I have a string that contains HTML image elements that is stored in a
I have a service that converts images stored on a website to byte array

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.