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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:20:49+00:00 2026-05-20T09:20:49+00:00

I’m looking for a good approach for efficiently dividing an image into small regions,

  • 0

I’m looking for a good approach for efficiently dividing an image into small regions, processing each region separately, and then re-assembling the results from each process into a single processed image. Matlab had a tool for this called blkproc (replaced by blockproc in newer versions of Matlab).

In an ideal world, the function or class would support overlap between the divisions in the input matrix too. In the Matlab help, blkproc is defined as:

B = blkproc(A,[m n],[mborder nborder],fun,…)

  • A is your input matrix,
  • [m n] is the block size
  • [mborder, nborder] is the size of your border region (optional)
  • fun is a function to apply to each block

I have kluged together an approach, but it strikes me as clumsy and I bet there’s a much better way. At the risk of my own embarrassment, here’s my code:


import numpy as np

def segmented_process(M, blk_size=(16,16), overlap=(0,0), fun=None):
    rows = []
    for i in range(0, M.shape[0], blk_size[0]):
        cols = []
        for j in range(0, M.shape[1], blk_size[1]):
            cols.append(fun(M[i:i+blk_size[0], j:j+blk_size[1]]))
        rows.append(np.concatenate(cols, axis=1))
    return np.concatenate(rows, axis=0)

R = np.random.rand(128,128)
passthrough = lambda(x):x
Rprime = segmented_process(R, blk_size=(16,16), 
                           overlap=(0,0), 
                           fun=passthrough)

np.all(R==Rprime)
  • 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-05-20T09:20:50+00:00Added an answer on May 20, 2026 at 9:20 am

    Here are some examples of a different (loop free) way to work with blocks:

    import numpy as np
    from numpy.lib.stride_tricks import as_strided as ast
    
    A= np.arange(36).reshape(6, 6)
    print A
    #[[ 0  1  2  3  4  5]
    # [ 6  7  8  9 10 11]
    # ...
    # [30 31 32 33 34 35]]
    
    # 2x2 block view
    B= ast(A, shape= (3, 3, 2, 2), strides= (48, 8, 24, 4))
    print B[1, 1]
    #[[14 15]
    # [20 21]]
    
    # for preserving original shape
    B[:, :]= np.dot(B[:, :], np.array([[0, 1], [1, 0]]))
    print A
    #[[ 1  0  3  2  5  4]
    # [ 7  6  9  8 11 10]
    # ...
    # [31 30 33 32 35 34]]
    print B[1, 1]
    #[[15 14]
    # [21 20]]
    
    # for reducing shape, processing in 3D is enough
    C= B.reshape(3, 3, -1)
    print C.sum(-1)
    #[[ 14  22  30]
    # [ 62  70  78]
    # [110 118 126]]
    

    So just trying to simply copy the matlab functionality to numpy is not all ways the best way to proceed. Sometimes a ‘off the hat’ thinking is needed.

    Caveat:
    In general, implementations based on stride tricks may (but does not necessary need to) suffer some performance penalties. So be prepared to all ways measure your performance. In any case it’s wise to first check if the needed functionality (or similar enough, in order to easily adapt for) has all ready been implemented in numpy or scipy.

    Update:
    Please note that there is no real magic involved here with the strides, so I’ll provide a simple function to get a block_view of any suitable 2D numpy-array. So here we go:

    from numpy.lib.stride_tricks import as_strided as ast
    
    def block_view(A, block= (3, 3)):
        """Provide a 2D block view to 2D array. No error checking made.
        Therefore meaningful (as implemented) only for blocks strictly
        compatible with the shape of A."""
        # simple shape and strides computations may seem at first strange
        # unless one is able to recognize the 'tuple additions' involved ;-)
        shape= (A.shape[0]/ block[0], A.shape[1]/ block[1])+ block
        strides= (block[0]* A.strides[0], block[1]* A.strides[1])+ A.strides
        return ast(A, shape= shape, strides= strides)
    
    if __name__ == '__main__':
        from numpy import arange
        A= arange(144).reshape(12, 12)
        print block_view(A)[0, 0]
        #[[ 0  1  2]
        # [12 13 14]
        # [24 25 26]]
        print block_view(A, (2, 6))[0, 0]
        #[[ 0  1  2  3  4  5]
        # [12 13 14 15 16 17]]
        print block_view(A, (3, 12))[0, 0]
        #[[ 0  1  2  3  4  5  6  7  8  9 10 11]
        # [12 13 14 15 16 17 18 19 20 21 22 23]
        # [24 25 26 27 28 29 30 31 32 33 34 35]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.