-1

I can convert the RGB image into binary but its dimensions are still too large (1280x720x3). Since each pixel of the binary image only has a value of 0 or 1, I want to reduce its dimension to (1280x720x1) so I won't have to deal with memory issues (since I'm working with thousands of images).

import cv2
import glob

def convert_to_binary(source_path, destination_path):
    i = 0

    for filename in glob.iglob("{}*.png".format(source_path)):
        im_gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
        (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
        cv2.imwrite("{}.png".format(destination_path + str(i)), im_bw)
        i += 1

How could I modify the above code to change the dimensions of saved images from (1280x720x3) to (1280x720x1)?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Huy Vo
  • 2,418
  • 6
  • 22
  • 43

2 Answers2

2

Use np.newaxis or np.reshape to convert (H,W) to (H,W,1).

>>> h,w = 3,4
>>> binary = np.zeros((h,w))
>>> binary.shape
(3, 4)

(1) use np.newaxis to add the new dimenssion

>>> new_binary = binary[..., np.newaxis]
>>> new_binary.shape
(3, 4, 1)
>>> 

(2) use reshape to change the dimension

>>> new_binary2 = binary.reshape((h,w,1))
>>> new_binary2.shape
(3, 4, 1)

Now see the result.

>>> binary
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
>>> new_binary
array([[[ 0.],
        [ 0.],
        [ 0.],
        [ 0.]],

       [[ 0.],
        [ 0.],
        [ 0.],
        [ 0.]],

       [[ 0.],
        [ 0.],
        [ 0.],
        [ 0.]]])
Kinght 金
  • 17,681
  • 4
  • 60
  • 74
0

From various sources, this is one incarnation of converting rgb to gray scale.

# num_92 gray scale image from rgb
def num_92():
    """num_92... gray-scale image from rgb
    :Essentially gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
    : np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
    : http://stackoverflow.com/questions/12201577/how-can-i-convert
    :       -an-rgb-image-into-grayscale-in-python
    : https://en.m.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
    : see https://en.m.wikipedia.org/wiki/HSL_and_HSV
     """
    frmt = """
    :---------------------------------------------------------------------:
    {}
    :---------------------------------------------------------------------:
    """
    import matplotlib.pyplot as plt
    a = np.arange(256).reshape(16, 16)
    b = a[::-1]
    c = np.ones_like(a)*128
    rgb = np.dstack((a, b, c))
    gray = np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140])
    plt.imshow(gray, cmap=plt.get_cmap('gray'))
    plt.show()
    args = [num_92.__doc__]
    print(frmt.format(*args))

rename the def, but in the interim, call it using

num_92()

or play around with portions

import matplotlib.pyplot as plt
a = np.arange(256).reshape(16, 16)
b = a[::-1]
c = np.ones_like(a)*128
rgb = np.dstack((a, b, c))
plt.imshow(rgb, cmap=plt.get_cmap('hot'))
plt.show()

rgbgrey

but if you average the rescaled rgb you get a different picture, so it depends on what you want

avg = np.average(rgb, axis=-1)
avg.shape
(16, 16)
plt.imshow(avg, cmap=plt.get_cmap('gray'))
plt.show()

rgb average

NaN
  • 2,212
  • 2
  • 18
  • 23