0

I am trying to the do edge detection and apply laplacian, HOG on multiple images stored in a folder at a time for feature extraction but I am unable to do so. I have actually saved the images of the directory in a list and now when i am trying to process these images, it is throwing an error.

import numpy as np
import cv2
import os
import matplotlib.pyplot as plt
def resize():
    data = []
    img_size = 244
    data_dir = r'C:\technocolab project2\testing'
    for img in os.listdir(data_dir):
        try:
            imgPath = os.path.join(data_dir,img)
            global images
            images = cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE)
            global image_resized
            image_resized = cv2.resize(images,(img_size,img_size))
            data.append(image_resized)
        #except Exception as e:
            #print(e)
        except:
            pass
    return data

data = resize()

for i in range(len(data)):
    data[i]=data[i].astype('float32')

splitting the dataset

training = data[:int(0.2*len(data))]
validation = data[int(0.2*len(data)):int(0.4*len(data))]
testing = data[int(0.4*len(data)):int(0.5*len(data))]



train_norm =[]
valid_norm = []
test_norm = []

for a in range(len(training)):
    
    train_norm.append(training[a]/255)
print(train_norm[0])

for b in range(len(validation)):
    
    valid_norm.append(validation[b]/255)
print(valid_norm[0])

for c in range(len(testing)):
    
    test_norm.append(testing[c]/255)
print(test_norm[0])


train_edge =  []
for e in range(len(train_norm)):
    train_edge.append(cv2.Canny(train_norm[e],90,300))

OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\canny.cpp:829: error: (-215:Assertion failed) _src.depth() == CV_8U in function 'cv::Canny'

  • 1
    Images present in `train_norm` are of `float` datatype due to normalization. You need to convert it to `np.uint8` before performing Canny edge detection – Jeru Luke Jul 26 '22 at 08:57
  • 1
    You can also OpenCV's `normalize()` to limit pixel values within the range (0-255) and ensure it has the right datatype `CV_8U`: `dst = cv2.normalize(train_norm[e], dst=None, alpha=0, beta=255,norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)`. Pass `dst` to perform Canny edge detection. – Jeru Luke Jul 26 '22 at 09:00
  • Thank you so much! I am actually new to this domain so if possible could you please elaborate on the last comment and also is there any way to connect? @JeruLuke –  Jul 26 '22 at 09:44
  • Please have a look at the answers here: https://stackoverflow.com/questions/38025838/normalizing-images-in-opencv – Jeru Luke Jul 29 '22 at 08:13

0 Answers0