Nandhini
Nandhini

Reputation: 53

error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'GaussianBlur'

Trying to count number of objects in .npy image file

import numpy as np
img=np.load('A03_00Ac_Object Identities.npy')
from matplotlib import pyplot as plt
plt.imshow(img,cmap='gray')

A03_00Ac_Object Identities.npy image

import cv2
plt.imshow(img, cmap='gray')
blur = cv2.GaussianBlur(gray, (11, 11), 0)
canny = cv2.Canny(blur, 30, 40, 3)
plt.imshow(canny, cmap='gray')
dilated = cv2.dilate(canny, (1, 1), iterations=0) 
plt.imshow(dilated, cmap='gray')
(cnt, hierarchy) = cv2.findContours(
dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.drawContours(rgb, cnt, -1, (0, 255, 0), 2)
plt.imshow(rgb)
print("No of circles: ", len(cnt))

Received Error

Traceback (most recent call last)
Input In [32], in <cell line: 1>()
----> 1 blur = cv2.GaussianBlur(img, (11, 11), 0)

error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'GaussianBlur'
> Overload resolution failed:
>  - src data type = 8 is not supported
>  - Expected Ptr<cv::UMat> for argument 'src'

Please help me on this...Opencv Ref: How to count objects in image using python?

Upvotes: 1

Views: 14628

Answers (1)

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15366

Many OpenCV functions expect specific data types. Your array has type np.uint32/CV_32U. GaussianBlur won't accept that. It will accept np.uint8/CV_8U.

If the range of your array's values fits in uint8 (check whether img.max() <= 255), you can convert it using

img_u8 = img.astype(np.uint8)

If your array's values happen to exceed uint8 range but the values don't matter, you can simply "threshold" the data and use the result:

mask = np.uint8(img > 0)

Upvotes: 2

Related Questions