Error when using cv2.findContours - python

I tried to find contours for image, but it gives an error.
My code is:
import cv2
import numpy as np
img = cv2.imread('star.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
error is:
Traceback (most recent call last): File "C:\Python27\OpenCVContore.py", line 5, in <module> contours,hierarchy,thresh = cv2.findContours(thresh, 1, 2) error: ........\opencv\modules\core\src\matrix.cpp:236: error: (-215) step[dims-1] == (size_t)CV_ELEM_SIZE(flags) in function cv::Mat::create
I am using Python 2.7 OpenCV 2.4.7,2.4.11
Any help is appreciated.

Solved this problem by updating my python version to 3.4 and opencv version 3. But cannot find real solutions for this. Why python 2.x versions not fully support for openCV

Related

Cannot load my YOLOv3 model into readNetFromDarknet

So I have trained a YOLOv3 model and want to test the accuracy of the model. I am trying to load the model by using the command 'cv2.dnn.readNetFromDarknet'. Every time I try I receive the error
Traceback (most recent call last):
File "C:\Users\Philip\PycharmProjects\Scriptie\venv\tester.py", line 9, in <module>
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\darknet\darknet_io.cpp:660: error: (-215:Assertion failed) separator_index < line.size() in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'
The python code that I am using is shown below:
import cv2
import numpy as np
import matplotlib.pyplot as pl
classes = ['TEETH']
modelConfiguration = r"C:\Users\Philip\PycharmProjects\Scriptie\venv\yolov3_custom.cfg"
modelWeights = r"C:\Users\Philip\PycharmProjects\Scriptie\venv\yolov3_custom_2000.weights"
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
I have tried changing the paths to absolute paths but it didn't work, anyone that can help me with this?

"with" keyword does not work with "cv2.VideoCapture(0)" in python

code:
import cv2
import numpy as np
with cv2.VideoCapture(0) as cap:
while True:
ret, frame = cap.read()
cv2.imshow("Title", frame)
if cv2.waitKey(1):
break
cv2.destroyAllWindows()`
Throws error:
"Traceback (most recent call last):
File "D:/Python/openCV/Basic_Video/Basic.py", line 4, in
with cv2.VideoCapture(0) as cap:
AttributeError: __enter__"
The with keyword is not supported for VideoCapture. All examples that I found always declare it first:
How to process images of a video, frame by frame in video streaming using Opencv python
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html
cv2.videocapture.read() does not return a numpy array

ImportError: cannot import name 'imshow'

unfortunalty I have not found a solution for this problem.
from imgutils import imshow
import cv2
img3 = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
plt.figure(figsize=(20,10))
plt.subplot(1,2,1); imshow(img)
plt.subplot(1,2,2); imshow(img3)
I get the following traceback:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-31-8006396b3a04> in <module>
----> 1 from imgutils import imshow
2
3 img3 = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
4
5 plt.figure(figsize=(20,10))
ImportError: cannot import name 'imshow'
I am working on a Mac OS X 10.11, with a anaconda environment, python 3.5.
I have search the web to the imgutils module and have not a module, which have a imshow function.
Any suggestions?
Update
from cv2 import imshow
img3 = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
plt.figure(figsize=(20,10))
plt.subplot(1,2,1); imshow(img)
plt.subplot(1,2,2); imshow(img3)
Traceback
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-d01b4c235975> in <module>
5 plt.figure(figsize=(20,10))
6
----> 7 plt.subplot(1,2,1); imshow(img)
8 plt.subplot(1,2,2); imshow(img3)
TypeError: Required argument 'mat' (pos 2) not found
imgutils does not seem to contain an imshow attribute (and therefore definitely not a function). It does not contain an imshow submodule, nor does it imports an imshow function in the __init__.py file: it basically reimports elements from the submodules like, but a fast search does not yield an imshow function.
You probably wan to use the imshow from the matplotlib.pyplot module, so you should replace:
from imgutils import imshow
with:
from matplotlib.pyplot import imshow
This then imports the cv2.imshow [pyplot-doc] function.

Error while opening the image in opencv and python

I am trying to start with basics of opencv with python but when i executed the below code :
import cv2
import numpy as np
img = cv2.imread('bg.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
But i am getting this error:-
Traceback (most recent call last):
File "F:\OpenCV Programs\OpenCV1.py", line 7, in <module>
cv2.imshow('image',img)
error: C:\build\master_winpack-bindings-win32-vc14-
static\opencv\modules\highgui\src\window.cpp:331: error: (-215) size.width>0
&& size.height>0 in function cv::imshow
Plz help me out thank you..!
The code you posted works, if there is an image called bg.jpg within the same directory where you are running the code.
Anyway, you could try to reference the full path to the image:
import os
img_path = os.path.join(os.getcwd(), 'bg.jpg')
img = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
The image you attempt to load does not exist.
As you cannot display a non-existing image you get an error messsage for trying.
You'll most likely messed up the path. Maybe use an absolute path to the image.

Python PIL NameError global name Image is not defined

I installed PIL 1.1.7 using the executable for Python 2.7. But when I run this code :
import requests
from PIL import *
def main() :
target = "http://target/image.php" #returns binary data for a PNG.
cookies = {"mycookie1", "mycookie2"}
r = requests.get(target, cookies=cookies)
im = Image.open(r.content) #r.content contains binary data for the PNG.
print im
if __name__ == "__main__" :
main()
It gives the error :
Traceback (most recent call last):
File "D:\Programming\Python\code\eg_cc1.py", line 17, in <module>
main()
File "D:\Programming\Python\code\eg_cc1.py", line 13, in main
im = Image.open(r.content)
NameError: global name 'Image' is not defined
I installed PIL in Lib\site-packages.
You need to explicitly import the Image module:
>>> from PIL import *
>>> Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Image' is not defined
>>> import PIL.Image
>>> Image
<module 'Image' from 'c:\Python27\lib\site-packages\PIL\Image.pyc'>
>>>
Or just
>>> import Image
Use
from PIL import Image
This way it will work, and your code will be forward-compatible with Pillow. On the other hand, import Image will not work with Pillow.
The development of PIL has been stopped in mid 2011, without any official announcement or explication. Pillow is a fork of original PIL and is actively developed.
If
import Image
is not working.
Try:
from PIL import Image
There is compatibility issue with all the packages in PIL. Try downloading PIL package manually for the latest version.
Actually worked with this in Python 3.2 Version, everything is working fine.

Categories

Resources