A.Klim
A.Klim

Reputation: 31

average luminescence value vs distance to the center of an image

I would like to compute the average luminescence value vs distance to the center of an image. The approach I am thinking about is to

  1. compute the distance between pixels in image and image center
  2. group pixels with same distance
  3. compute the average value of pixels for each group
  4. plot graph of distance vs average intensity

To compute first step I use this function:

dist_img = np.zeros(gray.shape, dtype=np.uint8)
for y in range(0, h):
    for x in range(0, w):
        cy = gray.shape[0]/2
        cx = gray.shape[1]/2

        dist = math.sqrt(((x-cx)**2)+((y-cy)**2))
        dist_img[y,x] = dist

Unfortunately id does give different result from the one which I compute from here

distance = math.sqrt(((1 - gray.shape[0]/2)**2 )+((1 - gray.shape[1]/2 )**2))

when I test it for pixel (1,1) I receive 20 from first code and 3605 from second. I would appreciate suggestions on the how to correct the loop and hints on how to start with other points.Or maybe there is other way to achieve what I would like to.

Upvotes: 0

Views: 144

Answers (1)

w-m
w-m

Reputation: 11232

You are setting up dist_img with an np.uint8 dtype. This 8 Bit unsigned integer can fit values between 0 and 255, thus 3605 can not be properly represented. Use a higher bith depth for your distance image dtype, like np.uint32.

distance = math.sqrt(((1 - gray.shape[0]/2)**2 )+((1 - gray.shape[1]/2 )**2))

Careful: gray.shape will give you (height, width) or (y, x). The other code correctly assigns gray.shape[0]/2 to the y center, this one mixes it up and uses the height for the x coordinate.

Your algorithm seems good enough, I would suggest you stick with it. You can achieve something similar to the first two steps by converting the image to polar space (e.g. with OpenCV linearToPolar), but that may be harder to debug.

Upvotes: 0

Related Questions