OpenCV: Error with adaptive thresholding (Error -215) - python

I am trying to do adaptive thresholding after applying Sobel filtering on an image, as seen in the code below:
import numpy as np
import matplotlib.pyplot as plt
import cv2
image = cv2.imread("train.jpg")
img = np.array(image, dtype=np.uint8)
#convert to greyscale
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#remove noise
img_smooth = cv2.GaussianBlur(img_grey, (13,13), 0)
sobely = cv2.Sobel(img_smooth,cv2.CV_64F,0,1,ksize=9)
thres = cv2.adaptiveThreshold(sobely, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 73, 2)
However, when I try to do adaptive thresholding, I get the following error:
cv2.error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-
python/opencv/modules/imgproc/src/thresh.cpp:1627: error: (-215:Assertion
failed) src.type() == CV_8UC1 in function 'adaptiveThreshold'
I have read that this error occurs if your image is not greyscale, however, I converted my image to greyscale at the start of the code. I am not sure why I am getting this error. Any insights are appreciated.

What you read is correct, that error actually means that your Mat is not a gray-scale image.
That happens because Sobel is changing the data to cv2.CV_64F (see the second parameter in the documentation here).
So after Sobel you need to convert the image to a gray-scale, you can do that with convertScaleAbs and after that pass it's output to adaptiveThreshold

Related

cv2 Error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' [duplicate]

This question already has an answer here:
OpenCV VideoCapture and error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
(1 answer)
Closed 1 year ago.
I'm trying to filter red color in my images but I got an error which is
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed .
I have no idea how can I fix this. Here is my code
import cv2
import numpy as np
path = "C:\\Users\\MERYEM\\OneDrive\\Masaüstü\\scan\\Img_Data\\Chosen\\frame3802.jpg"
image = cv2.imread(path)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_red = np.array([178,179,0])
upper_red = np.array([255,255,255])
mask = cv2.inRange(hsv,lower_red, upper_red)
cv2.imshow('Original Image',image)
cv2.imshow('Detection', hsv)
cv2.waitKey(0)
cv2.destroyAllWindows()
I need you help, thanks in advance. Btw, I am using PyCharm if it matters
Check the image address again or select another image for test.
Also; HSV range for OpenCV is (0-180, 0-255, 0-255)
import cv2
import sys
import numpy as np
im = cv2.imread(sys.path[0]+'/back.png')
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
redL = np.array([180*350/360, 0, 0])
redU = np.array([180*360/360, 255, 255])
mask = cv2.inRange(hsv, redL, redU)
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
cv2.imwrite(sys.path[0]+'/out.png',np.hstack((im, mask)))
Output of image and output mask:
More information about modules and their licenses:
OpenCV, NumPy

OpenCV2 converting image into gray scale & finding edges but getting this error

i am getting error, OpenCV2 converting image into gray scale & finding edges but getting this error
import cv2
import numpy as np
img = cv2.imread(r"F:\Python_Folder\lena.jpg")
# img = cv2.imread(r"F:\Python_Folder\lena.jpg",0) also tried this
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
laplacian = cv2.Laplacian(grey_img, cv2.CV_64F)
cv2.imshow("Laplacian image",laplacian)
cv2.waitKey(0)
cv2.destroyAllWindows()
error:
Traceback (most recent call last):
File "F:/Python_Folder/new.py", line 7, in <module>
cv2.imshow("Laplacian image",laplacian)
cv2.error: OpenCV(4.0.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261: error: (-2:Unspecified error) in function '__thiscall cv::CvtHelper<struct cv::Set<1,-1,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Unsupported depth of input image:
> 'VDepth::contains(depth)'
> where
> 'depth' is 6 (CV_64F)
You have to convert your image back to uint8. Check following edited code snippet:
import cv2
import numpy as np
img = cv2.imread("F:\Python_Folder\lena.jpg")
# img = cv2.imread(r"F:\Python_Folder\lena.jpg",0) also tried this
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
laplacian = cv2.Laplacian(grey_img, cv2.CV_64F)
# convert back to uint8
laplacian = cv2.convertScaleAbs(laplacian)
cv2.imshow("Laplacian image",laplacian)
cv2.waitKey(0)
cv2.destroyAllWindows()

newbie - attempting object tracking / locating in opencv python, problems with findContours()

I'm attempting to locate a rectangular object by its color, and then find the coordinates of the center of the object, or the borders, I"m not picky about which.
I've been successful in isolating the color and creating a mask, but the findContours function is not working and i think it's because i've not successfully provided a proper bimodal image to findContours.
%matplotlib inline
import matplotlib.image as mpimg
from matplotlib import pyplot as plt
import cv2
import numpy as np
red_image = mpimg.imread('/vagrant/notebooks/red_thing.jpg')
hsv = cv2.cvtColor(red_image, cv2.COLOR_BGR2HSV)
lower_red = np.array([30,150,50])
upper_red = np.array([255,255,180])
mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(red_image,red_image, mask= mask)
kernel = np.ones((20,20),np.uint8)
ret,thresh1 = cv2.threshold(res,60,255,cv2.THRESH_BINARY)
# perform 'open' operation to homogenize object
opened = cv2.morphologyEx(thresh1, cv2.MORPH_OPEN, kernel)
image, contours, hierarchy = cv2.findContours(opened,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
The last line produces the following error, I can't figure out how to resolve this.
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-130-06cc5691b64a> in <module>()
----> 1 image, contours, hierarchy = cv2.findContours(opened,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
error: /home/vagrant/opencv/modules/imgproc/src/contours.cpp:199: error: (-210) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function cvStartFindContours_Impl
i was able to fix the error with a conversion to gray, like this: gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)

Opencv Pythonprogram

I am trying opencv with python now.
What mean this error?
OpenCV Error: Assertion failed (m.dims >= 2) in Mat, file /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/core/src/matrix.cpp, line 269
Traceback (most recent call last):
File "sabun5.py", line 16, in <module>
img_m = cv2.threshold(img_df, 50, 255, cv2.THRESH_BINARY)[1]
cv2.error: /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/core/src/matrix.cpp:269: error: (-215) m.dims >= 2 in function Mat
You can see in the OpenCV documentation, that the threshold function just allow single-channel images.
If your image is a color one, it won't work. If it's grayscale but you are loading it with imread, it might be possible that OpenCV load it as a 3-channel one. You can add the flag to load it as a single-channel with CV_8UC1 (supposing it is an 8 bit unsigned one, which is the more common for a grayscale image).
For example:
img_df = cv2.imread("image/path", cv2.CV_8UC1)
you have to convert the image into grayscale before thresholding your image has more then two dimentions i.e (height,width,color-channel) gray scale image has only two dimention(height,width)
it might help
import cv2
img = cv.imread('x.png',0)
# where 0 converts the image in grayscale or gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_m = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)[1]
cv2.waitKey(0)

adaptive thresholding ---ValueError: too many values to unpack

I'm pretty amateur at image processing. I could successfully do normal thresholding but however I'm facing an error in Adaptive Thresholding.
Here is my code:
import cv2
import numpy as np
img = cv2.imread("vehicle004.jpg")
img = cv2.medianBlur(img,5)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_,th2=cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
cv2.imshow("window2",th2)
cv2.waitKey(0)
cv2.destroyAllWindows()
Error Message:
line 7, in <module>
_,th2 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
ValueError: too many values to unpack
Any help is appreciated.
As per the documentation, the cv2.adaptiveThreshold() returns only 1 value that is the threshold image and in this case you are trying to receive 2 values from that method, that is why ValueError: too many values to unpack error is raised.
After fixing the issue the code may look like:
import cv2
import numpy as np
img = cv2.imread("vehicle004.jpg")
img = cv2.medianBlur(img,5)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
th2=cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
cv2.imshow("window2",th2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Categories

Resources