Vandita Srivastava
Vandita Srivastava

Reputation: 31

I'm getting an error during the gaussian blur of an image using opencv in python. The code was working properly before and error appeared suddenly

I have written the following code:

import cv2 
import lxml.etree as xml
import os
import shutil
for filename in os.listdir(paths['labels']):

    with open(paths['labels']+filename,'r'):
        img2 = cv2.imread(filename, cv2.IMREAD_COLOR) 

        # Reading same image in another  
        # variable and converting to gray scale. 
        img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) 

        # Converting image to a binary image 
        # ( black and white only image). 
        #_, threshold = cv2.threshold(img, 110, 255, cv2.THRESH_BINARY) 

        blur = cv2.GaussianBlur(img,(5,5),0)
        _, threshold = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

        # Detecting contours in image. 
        contours, _= cv2.findContours(threshold, cv2.RETR_TREE,      #here it finds the total number of contours, i.e, number of rectangles in the image file
                                       cv2.CHAIN_APPROX_SIMPLE)

I am receiving an Error in the line 18: (blur = cv2.GaussianBlur(img,(5,5),0)):

Error : (-215:Assertion failed) dims <= 2 && step[0] > 0 in function 'cv::Mat::locateROI'

The code was working properly before and error appeared suddenly. I tried changing the image extension from jpg to png but the error remained.

Upvotes: 1

Views: 3684

Answers (1)

Saurav Saha
Saurav Saha

Reputation: 1007

This error most often occurs when your code doesn't receive a proper image, that is, that the path to the image is probably invalid or the image itself has some issues with it. The thing is that you can try and check if your image is loading up correctly or not :

cv2.imshow('image',img)

This command is used to display an image in OpenCV, which I think you are obviously familiar with. So use this command before you use Gaussian Blur just to verify that the image that you are supplying is correctly loading up or not.

If the image does not get displayed then you can be sure that you are facing one of the problems mentioned above! You will receive the Assertion failed error in this line now.

PS: the full form of ROI is Region of Interest. In your case, OpenCV is not able to locate the region of interest over which it will perform Gaussian Blur.

Upvotes: 1

Related Questions