-1

So here is the incomplete code

import cv2
import numpy as np
arr= np.zeros((30,30))
# bw_image = <missing part>
cv2.imshow("BW Image",bw_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

#please use arr at 4th line to complete the code

I am actually new to this and don't know how to convert a given 2d array into a binary image using opencv.
Please use the name "arr" for the missing part, as in my code, the array is not a zeros array, instead it has some random values of 0 and 255 of 400x400 shape.

Rahul
  • 3
  • 2

1 Answers1

1

I think you want a Numpy array of random integers:

arr = np.random.randint(0, 256, (400,400), dtype=np.uint8)

If your question is actually about thresholding, maybe you want:

_, bw_image = cv2.threshold(arr, 128,255,cv2.THRESH_BINARY)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thx a lot, it worked! Btw, I am new to this field. Could you suggest me some resources to learning about opencv? Also, I wanted to know about '_' in the line: _, bw_image = cv2.threshold(arr, 128,255,cv2.THRESH_BINARY) – Rahul Nov 01 '20 at 09:53
  • The underscore is used in Python (and in `bash` shell) as a placeholder for a variable we are not interested in and can't be bothered to think up a name for - it just means *"unused and unnamed thing"*. – Mark Setchell Nov 01 '20 at 10:05