fatma cheour
fatma cheour

Reputation: 25

Reducing processing time of images in python

I have a python scripts that processes black and white images. The first thing that I do is the usual process that takes not more than 170 ms which is

  img = img[200:900, 250:1100]
  imag = image_resize(img, height=960, width=1280)
  ImgBlur = cv2.GaussianBlur(imag, (11, 11), 0)
  ret, thresh1 = cv2.threshold(ImgBlur, 100, 255, cv2.THRESH_BINARY)
  imgCanny = cv2.Canny(thresh1, 100, 100)

Immediately after I need to find the white part in the image and I execute this function

  A = np.array(imgCanny)
  xpos = []
  ypos = []
  for i in range(len(A)):
      for j in range(len(A[i])):
          if A[i][j] == 255:
              ypos.append(i)
              xpos.append(j)

My problem is that this function is taking 2.3 seconds, mostly 98% of the processing time. Does anyone have an idea of how can I decrease that by another alternative?

The imageCanny result that I get is this kind of image imgCanny

Upvotes: 1

Views: 172

Answers (1)

Tom S
Tom S

Reputation: 631

Depending on what you need the results for Numpy.where and numpy.argwhere should do exactly what you are looking for, as seen here: Convert mask (boolean) array to list of x,y coordinates

np.argwhere(a==255)

will return a array of all coordnites

Upvotes: 1

Related Questions