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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:15:01+00:00 2026-05-28T20:15:01+00:00

I’m trying to implement some image processing (finding regions of similar colour) in Python

  • 0

I’m trying to implement some image processing (finding regions of similar colour) in Python with PIL and Numpy. Can’t figure out how to speed up this code. Could you help?

def findRegions(self, data):
#data is numpy.array
    ret = [[False for _ in range(self.width)] for _ in range(self.heigth)]

    for i in range(self.heigth):
        for j in range(self.width):
            k = 0
            acc = 0
            for x,y in [(-1,0),(0,-1),(0,1),(1,0)]:
                if (self.heigth>i+x>=0 and self.width>j+y>=0):
                    k = k+1
                    acc += math.sqrt(sum((data[i][j][c]-data[i+x][j+y][c])**2 for c in range(3)))
            if (acc/k<self.threshold):
                ret[i][j]= True
    return ret 

PIL and other image libraries have got many filtering and processing functions which are really quick. But what is the best way to implement own image processing functions?

  • 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-28T20:15:02+00:00Added an answer on May 28, 2026 at 8:15 pm

    Rather than looping over each row and column you can shift the array left, right, up, and down for the appropriate number of elements. On each shift you accumulate your values in a base array. After the shifting and accumulating you compute your average and apply your threshold to return a mask. See this post which has a general discussion on the topic. The idea is take advantage of numpy’s broadcasting, which will apply a function or operator to all elements of an array in C rather than Python.

    I’ve adapted the code from the linked post to fit what I believe you are trying to accomplish. In any case the general pattern should speed things up. You have to work out what to do with the edges in the return mask. Here I’ve simply set the return mask to False, but you could also eliminate the edges by expanding the input data by one pixel in each direction and filling with the nearest pixel, zeros, gray, etc.

    def findRegions(self,data):
        #define the shifts for the kernel window
        shifts = [(-1,0),(0,-1),(0,1),(1,0)]
    
        #make the base array of zeros 
        #  array size by 2 in both dimensions
        acc = numpy.zeros(data.shape[:2])
    
        #compute the square root of the sum of squared color 
        # differences between a pixel and it's 
        # four cardinal neighbors
        for dx,dy in shifts:
            xstop = -1+dx or None
            ystop = -1+dy or None
            #per @Bago's comment, use the sum method to add up the color dimension
            #  instead of the list comprehension
            acc += ((data[1:-1,1:-1] - data[1+dx:xstop, 1+dy:ystop])**2).sum(-1)**.5
    
        #compute the average 
        acc /= (len(shifts) + 1)
    
        #build a mask array the same size as the original
        ret = numpy.zeros(data.shape[:2],dtype=numpy.bool)
    
        #apply the threshold
        #  note that the edges will be False
        ret[1:-1,1:-1] acc < self.threshold    
    
        return ret
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string

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.