I am trying to make a simple program to detect a circle in an image. I've written this code, and the result can be seen in the attached picture. Could someone please tell me what am I doing wrong?
import time
import cv2
import imutils
import numpy as np
img=cv2.imread('circle.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
binary=cv2.threshold(gray,150,255,cv2.THRESH_BINARY)[1]
cv2.imshow('binary',binary)
circles=cv2.HoughCircles(binary,cv2.HOUGH_GRADIENT,1,50,50,30,5,100)
if circles is not None:
circles=np.round(circles[0,:]).astype("int")
for i in circles:
cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),4)
cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
else:
print"no circle"
cv2.imshow('circle',img)
k=cv2.waitKey(0)
if k==27:
cv2.destroyAllWindows()
Image
Related
I am trying to find a way to show an image without the dependency of waitKey(). I want the image to be shown and continue on with next operations (like a plot using matplotlib). How can this be achieved?
If you're wanting to show the window and have the program continue execution without relying on cv2.waitKey(), then cv2.startWindowThread() is what you're looking for.
Example:
import cv2
img = cv2.imread("C:\\Test\\so1.png")
cv2.imshow("Test", img)
cv2.startWindowThread()
for x in range(0, 10000000):
print(x)
This will display the image and continue execution without using waitKey
I tried multiple methods, but what worked was using matplotlib
import matplotlib.pyplot as plt
#obtain I as a numpy array
plt.imshow(cv2.cvtColor(I, cv2.COLOR_BGR2RGB))
I have been working on trying to get my python program and I need to use matplotlib for image processing.
I've been having a lot of trouble but I followed all the examples that I can find online. I had a hard time getting matplotlib to install.
Image of Errors
Second image of errors
tried:
import matplotlib.pyplot as plt
import matplotlib.image as aImage
img = 'Alabama.jpg'
img_Alabama = plt.imread(img)
plt.imshow(img_Alabama)
&
img_Alabama = plt.imread('StepOne\MatLibPhotos\Alabama.jpg')
plt.imshow(img_Alabama)
I am trying to run this program in jupyter notebook, but it's not running at all. It's just showing the asterisk beside the program. Can anyone tell me why is that happening? I have also tried other programs of OpenCV as well, but the result was the same. None of them are running.
import cv2
import numpy as np
image = cv2.imread('images/input.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Original image',image)
cv2.imshow('Gray image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
Try this following code, opencv imshow cause issues with notebook.
from matplotlib import pyplot as plt
import cv2
image = cv2.imread('images/input.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(gray)
plt.title('Gray image')
plt.show()
I installed openCV and numpy libraries in python 2.7.
I've tested them using commands import cv2 and import numpy and it compiled.
But when I use the cv2.imshow('frame', ----) function it displays a window but not displaying the image. And it's showing " frame is Not Responding".
So, I tried with matplotlib functions for displaying image and it worked.
I inserted cv2.imshow function in the 2nd case and it worked.
Versions [Python-2.7.10, OpenCV-2.4.11]
Below is the code,
Case 1: Not Working,displaying window but not image (showing FRAME IS NOT RESPONDING)
import cv2
import numpy
img = cv2.imread('a.jpg')
cv2.imshow('FRAME',img)
Case 2: Working
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
img = mpimg.imread('a.jpg')
img2 = cv2.imread('b.jpg')
cv2.imshow('FRAME',img2)
plt.imshow(img)
plt.show()
imshow should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won’t display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame). Here's a working example:
import cv2
img = cv2.imread('a.jpg')
cv2.imshow('FRAME', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Try using imread like this
img = cv2.imread('a.jpg',0)#grayscale
img = cv2.imread('a.jpg',1)#rgb
This question already has answers here:
how to use hough circles in cv2 with python?
(3 answers)
Closed 7 years ago.
I have a small test code block trying to process a simple photo with a ball in it:
#!/usr/local/bin/python
import cv2
import numpy as np
img = cv2.imread("b.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray,cv2.CV_HOUGH_GRADIENT)
When I try to run this I get:
AttributeError: 'module' object has no attribute 'HOUGH_GRADIENT'
I've been installing and reinstalling for two days trying to figure out whats wrong.
Any help or pointers would be appreciated!
add this line : import cv2.cv as cv and change circles
circles = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT)
i.e. cv.CV_HOUGH_GRADIENT in place of cv2.CV_HOUGH_GRADIENT
This will solve the AttributeError that you were getting but still you'll get a type error, you'll have to provide arguments for dp and minDist
(arguments at pos 3 and pos 4) and that you can give accordingly.(Here I've given 1 and 10)
#!/usr/local/bin/python
import cv2
import cv2.cv as cv
import numpy as np
img = cv2.imread("test.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT, 1, 10)
also please see a similar problem solved here:
http://answers.opencv.org/question/1497/errors-with-cv2houghcircles/
try
circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 75)
works, to me