Home > Enterprise >  find the location of the whitish points on an image
find the location of the whitish points on an image

Time:07-21

import matplotlib.image as img
import numpy as np
import matplotlib as plt
import matplotlib.pyplot as plt

image1 = img.imread("82.png",0)
image2 = img.imread("83.png",0)

diff = image2 - image1

matchingInd = np.where(diff > 0.6)


diff[matchingInd] = 1
plt.imshow(diff)

image1 and image2 has black background with some bright(whitish) points. image2 is the same photo as image1 but just added some bright(whitish) points or deleted some of the bright(whitish) points. How can I find the location of the added bright points on the image2?

CodePudding user response:

Since you have tagged OpenCV I have replicated your problem the following way.

Consider 2 image arrays a and b, where 255 refers to the bright spots:

>>> a
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0, 255,   0, 255,   0,   0, 255,   0,   0,   0,   0,   0],
       [  0, 255,   0,   0,   0,   0,   0, 255,   0, 255, 255,   0,   0],
       [  0,   0,   0,   0,   0, 255,   0,   0,   0,   0,   0,   0,   0],
       [  0, 255,   0,   0,   0, 255, 255,   0, 255,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]],
  dtype=uint8)

In b, some spots have been removed compared to a and new spots have been added:

>>> b
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255, 255],
       [255,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255],
       [255,   0, 255,   0, 255,   0,   0, 255,   0,   0,   0,   0,   0],
       [  0, 255,   0,   0,   0,   0,   0, 255,   0, 255,   0,   0,   0],
       [  0,   0,   0,   0,   0, 255,   0,   0,   0,   0, 255,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0, 255,   0, 255,   0,   0],
       [  0,   0,   0,   0,   0, 255,   0,   0,   0,   0,   0,   0,   0]],
  dtype=uint8)

Finding only the newly added spots in b using cv2.subtract. This should be read as elements in a are subtracted from elements in a:

>>>diff = cv2.subtract(b, a)

diff contains only the newly added spots:

>>> diff
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255, 255],
       [255,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255],
       [255,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255,   0,   0],
       [  0,   0,   0,   0,   0, 255,   0,   0,   0,   0,   0,   0,   0]],
  dtype=uint8)

Now find the coordinates as mentioned above using np.argwhere:

>>> np.argwhere(diff == 255)
array([[ 0, 11],
       [ 0, 12],
       [ 1,  0],
       [ 1, 12],
       [ 2,  0],
       [ 4, 10],
       [ 5, 10],
       [ 6,  5]], dtype=int64)

CodePudding user response:

You can use numpy.where or numpy.argwhere

They will give you the same results but the output shape will be different:

numpy.where will give you a tuple with two elements: the array of all the x coordinates (x1, x2, ...) and the array of all the y coordinates (y1, y2, ...)

numpy.argwhere will give you an array of shape (N, 2) with each row correponfing to the coordinates of one pixel

import numpy as np

my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

np.where(my_array < 4)
Out: (array([0, 0, 0]), array([0, 1, 2]))

np.argwhere(my_array < 4)
Out: array([[0, 0],
            [0, 1],
            [0, 2]])

Note: using np.argwhere should throw an error when trying to run the following line diff[matchingInd] = 1

  • Related