0

In opencv in python I loaded an black and white image. After resize this image I added pading 5 pixel into each side of image :

resized_digit = cv2.resize(digit, (18,18))
cv2.imshow("resized Image", digit)

# Padding the digit with 5 pixels of black color (zeros) in each side to finally produce the image of (28, 28)
padded_digit = np.pad(resized_digit, ((5,5),(5,5)), "constant", constant_values=0)

How can I‌ show padded_digit by opencv? Because when I try to show like this:

cv2.imshow("Padded Image", padded_digit)

I got this error:

<class 'list'>
Traceback (most recent call last):

  File "<ipython-input-4-295bf6efa8c4>", line 39, in <module>
    padded_digit = np.pad(resized_digit, ((5,5),(5,5)), "constant", constant_values=0)

  File "<__array_function__ internals>", line 5, in pad

  File "/home/Alt/anaconda3/lib/python3.8/site-packages/numpy/lib/arraypad.py", line 746, in pad
    pad_width = _as_pairs(pad_width, array.ndim, as_index=True)

  File "/home/Alt/anaconda3/lib/python3.8/site-packages/numpy/lib/arraypad.py", line 521, in _as_pairs
    return np.broadcast_to(x, (ndim, 2)).tolist()

  File "<__array_function__ internals>", line 5, in broadcast_to

  File "/home/Alt/anaconda3/lib/python3.8/site-packages/numpy/lib/stride_tricks.py", line 180, in broadcast_to
    return _broadcast_to(array, shape, subok=subok, readonly=True)

  File "/home/Alt/anaconda3/lib/python3.8/site-packages/numpy/lib/stride_tricks.py", line 123, in _broadcast_to
    it = np.nditer(

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2) and requested shape (3,2)
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149

1 Answers1

1

You can resolve the problem by reading with cv2.imread and then convert to the gray-scale (black-and-white) image to the cv2.COLOR_BGR2GRAY

import cv2
import numpy as np

img = cv2.imread("input.jpg")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
resized_digit = cv2.resize(gry, (18, 18))
padded_digit = np.pad(resized_digit, ((5, 5), (5, 5)), "constant", constant_values=0)
cv2.imshow("padded_digit", padded_digit)
cv2.waitKey(0)

imread loads the image in BGR fashion, then you can convert it to the gray-scale using COLOR_BGR2GRAY.

You can also use copyMakeBorder (source) instead of np.pad. Using np.pad is not wrong, but opencv is more suitable for image operations, since you are using opencv functions like imshow, waitKey etc.

For instance:

import cv2

img = cv2.imread("input.jpg")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
resized_digit = cv2.resize(gry, (18, 18))
padded_digit = cv2.copyMakeBorder(resized_digit, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=0)
cv2.imshow("padded_digit", padded_digit)
cv2.waitKey(0)

You can check the two image equality by comparing their shapes. Both of them print(padded_digit.shape) will be (28, 28)

Ahmet
  • 7,527
  • 3
  • 23
  • 47
  • Thanks. This is original image and resize and padding image https://pasteboard.co/JNw246F.png . The resized and padded image why has bad quality ? mean why has Edged all the side? How can i improve so that all edges be soft like main image?@Ahx – Cyrus the Great Feb 09 '21 at 06:29
  • You need to use [interplotaion](https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#resize) parameter while resizing you image. For instance: `resized_digit = cv2.resize(gry, (18, 18), interpolation=cv2.INTER_AREA)` – Ahmet Feb 09 '21 at 06:49
  • https://pasteboard.co/JNwc04b.png , This is end of improvement? Because I want to add into my model that created from mnist dataset to predict image number to number @Ahx – Cyrus the Great Feb 09 '21 at 06:54
  • Wait! resizing mnist? why are you resizing mnist digits?, they are already `28, 28`? yet you can easily reach 99.2% accuracy, without any preprocessing. – Ahmet Feb 09 '21 at 06:57
  • No, I resized my handwriting image to be same mnist image @Ahx – Cyrus the Great Feb 09 '21 at 06:59
  • Well if you are going to do learning, then try the current state of the images. Try, the outputs of the `resized_digit = cv2.resize(gry, (18, 18), interpolation=cv2.INTER_AREA)` . – Ahmet Feb 09 '21 at 07:02
  • Keep in mind that the current state-of-the-art of mnist is [99.79%](https://benchmarks.ai/mnist) – Ahmet Feb 09 '21 at 07:12
  • Yes but when you want to predict real image number in some numbers like 6 , It can't predict good. So I added some augmentation to fix the problem .Now my model's accuracy is 99.4 and It can predict 100 percent from my handwriting image number. I use CNN to create model@Ahx – Cyrus the Great Feb 09 '21 at 07:20
  • 1
    Thats perfect, congrats – Ahmet Feb 09 '21 at 07:22