Laplacian and Sobel Functions in open cv are not working properly - python

I am trying to run the Laplacian and Sobel Function to test the edge and gradients of the video streaming but while running the mentioned below code
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
# Take each frame
_, frame = cap.read()
hsv = cv2.cvtColor(frame, 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(frame,frame, mask= mask)
laplacian = cv2.Laplacian(frame,cv2.CV_64F)
sobelx = cv2.Sobel(frame,cv2.CV_64F,1,0,ksize=5)
sobely = cv2.Sobel(frame,cv2.CV_64F,0,1,ksize=5)
cv2.imshow('Original',frame)
cv2.imshow('Mask',mask)
cv2.imshow('laplacian',laplacian)
cv2.imshow('sobelx',sobelx)
cv2.imshow('sobely',sobely)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
I am facing the error using this problem, I though search from google also and trying to degrade the version of OpenCV 4 to OpenCV 3 and which is also not working on the laptop and facing error
Traceback (most recent call last):
File "C:\Users\Misha\Desktop\test\CV\Edge Detection and Gradients.py", line 24, in <module>
cv2.imshow('laplacian',laplacian)
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window_w32.cpp:1230: error: (-215:Assertion failed) dst.data == (uchar*)dst_ptr in function 'cvShowImage'
Any kind of useful advice on this will be a great help.

Related

python How can I fix OpenCV cvtColor error?

Here is the erroneous code:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
while(True):
ret, frame = cap.read()
frame = cv2.flip(frame, -1)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', frame)
cv2.imshow('gray', gray)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
this is the error I'm getting:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /home/piwheels/opencv-python/opencv/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor
I'm not sure how to fix this. Can you help me?
What the error message say is that your input image to cvtColor (frame) should have either three (R, G, B) or four (R, G, B, A) channels to convert it to grayscale, and it does not.
This can happen because:
Your camera does not capture an image at all
It captures the image in grayscale
First, comment the lines
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
and
cv2.imshow('gray', gray)
and test if you can see the captured frames. Most likely you won't see color images, and that (image capture) could be where the problem is.

edge detection using python

import cv2
import numpy as np
cap = cv2.VideoCapture()
while True:
_, frame = cap.read()
laplacia = cv2.Laplacian(frame, cv2.CV_64F)
cv2.imshow('original', frame)
cv2.imshow('laplacian', laplacia)
k = cv2.waitKey(5) & 0xFF
if k==27:
break
cv2.destroyAllWindows()
cap.release()
I am getting this error
#laplacia = cv2.Laplacian(frame, cv2.CV_64F)
cv2.error: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core\src\matrix.cpp:981: error: (-215) dims <= 2 && step[0] > 0 in function cv::Mat::locateROI
cv2.Laplacian() won't work with Color images.
You can go through OpenCV Documentation for knowing more..Image Gradients
You must convert the frame you have captured to gray scale and then apply Laplacian
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
You can convert to gray scale as shown above..

opencv with python filtering a color give error

what i am trying to do in my code below is to create an opencv program with python to open my laptop webcam and the filter the camera so that it will only show my clothes. but i coudnt even run the program because i have encounter an error that seem to be coming from the 10th line of the code. it is definitely not a misspeal error, i double checked it.
the code sample
#color filtering
import cv2
import numpy as np
#use camera
cap = cv2.VideoCapture(1)
while True:
_, frame = cap.read()
`this line seem to be the source-->` hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# hsv hue sat value
# try to get the value of the color that you want
lower_red = np.array([150,150,150])
upper_red = np.array([180,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(frame,frame, mask = mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('result',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
#release camera
cap.release()
the error
Traceback (most recent call last):
File "D:/Program_Files/Python/legit8.py", line 10, in <module>
hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cv2.error: C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:10705: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::cvtColor
Your frame is probably None. This could be because of VideoCapture(1). If your webcam is the only cam connected to your computer, use VideoCapture(0)!

an error of opencv on python about boundingRect

When I try the code about motion capture,I can't run succesfully because of this error.
Traceback (most recent call last):
File "G:\machine learning\CV\Video Capture\motion capture with square.py", line 21, in <module>
x,y,w,h=cv2.boundingRect(thresh)
error: ..\..\..\..\opencv\modules\imgproc\src\contours.cpp:1895: error: (-215) points.checkVector(2) >= 0 && (points.depth() == CV_32F || points.depth() == CV_32S) in function cv::boundingRect
Firtly, I thought maybe the data type may wrong, so I change the type to the 'float32' and 'int32'. But it can't help, so I have no idea.
And here is my code:
import cv2
import numpy as np
camera=cv2.VideoCapture(0)
firstframe=None
while True:
ret,frame = camera.read()
#cv2.imshow("frame", frame)
if not ret:
break
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray,(21,21),0)
if firstframe is None:
firstframe=gray
continue
frameDelta = cv2.absdiff(firstframe,gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
#(cnts,_)= cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h=cv2.boundingRect(thresh)
frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("frame", frame)
cv2.imshow("Thresh", thresh)
cv2.imshow("frame2", frameDelta)
key = cv2.waitKey(1)&0xFF
if key == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
Because cv2.boundingRect() expects a set of points (x, y) coordinates in a special format to calculate the bounding rect, Your input image is not a set of (x, y) points. This method is not meant to be applied directly onto images. You must find contours of the given binary mask, then you can iterate all the contours and call cv2.boundingRect() on the individual contours as:
cnts, hierarchy= cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# Iterate over all the contours.
for contour in cnts:
print cv2.boundingRect(contour)

Using the convexhull function in opencv

I am trying to implement Convexhull in my python project but I am getting the error
Traceback (most recent call last): File "contourfeaturestest.py", line 22, in <module>
hull2 = cv2.convexHull(cnt)
cv2.error:convhull.cpp:134: error: (-215) total >= 0 && (depth == CV_32F || depth == CV_32S) in function convexHull
I am trying to use convexhull with frames that come from the computer's video camera and I am not sure why my code is producing the error above.
My code is provided below.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
ret1,thresh = cv2.threshold(gray,127,255,0)
contours,hierarchy,ret2= cv2.findContours(thresh, 1, 2)
cnt = contours[0]
hull2 = cv2.convexHull(cnt)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
From what I can guess by your code, it seems that you are using Opencv3.1, under which the return value from cv2.findContours() has been changed to: ret_image, contours, hierarchy as opposed to what you are assuming it to be: contours,hierarchy,ret2 So basically the second value in the returned tuple contains the list of contours.
However, in the previous OpenCV version cv2.findContours() returned only 2 values: contours, hierarchy, so a slight change in the naming conventions would work for you.

Categories

Resources