1

I have a project of Eye Controlled Wheel Chair where I need to detect the pupil of the Eye and according to its motion the Wheel Chair moves. As a test for the code I am writing I performed the script on a static image. The image is exactly where the camera will be put. The camera will be an IR one.

Note: I am using compiled OpenCV 3.1.0-dev and Python2.7 on Windows Platfrom

The detected circle I wanted using Houghcircle transform:

original image result

After that I am working on a code to detect the same thing only by using an IR camera.

The results from the static image code is very reliable to me, but the problem is the code with the IR camera.

The code I have wrote so far is:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:

    ## Read Image
    ret, image = cap.read()
    ## Convert to 1 channel only grayscale image
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ## CLAHE Equalization
    cl1 = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
    clahe = cl1.apply(gray)
    ## medianBlur the image to remove noise
    blur = cv2.medianBlur(clahe, 7)
    ## Detect Circles
    circles = cv2.HoughCircles(blur ,cv2.HOUGH_GRADIENT,1,20,
                                param1=50,param2=30,minRadius=7,maxRadius=21)

    if circles != None:
        circles = np.round(circles[0,:]).astype("int")

    for circle in circles[0,:]:
        # draw the outer circle
        cv2.circle(image,(circle[0],circle[1]),circle[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(image,(circle[0],circle[1]),2,(0,0,255),3)

    if cv2.waitKey(1) in [27, ord('q'), 32]:
        break

cap.release()
cv2.destroyAllWindows()

I always get this error:

**if circles != None:
FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.

Traceback (most recent call last):
    cv2.circle(image,(circle[0],circle[1]),circle[2],(0,255,0),2)
IndexError: invalid index to scalar variable.**

For any questions about the code for the static image, the code is:

import cv2
import numpy as np

## Read Image
image = cv2.imread('eye.tif')
imageBackup = image.copy()
## Convert to 1 channel only grayscale image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
## CLAHE Equalization
cl1 = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe = cl1.apply(gray)
## medianBlur the image to remove noise
blur = cv2.medianBlur(clahe, 7)

## Detect Circles
circles = cv2.HoughCircles(blur ,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=7,maxRadius=21)

for circle in circles[0,:]:
    # draw the outer circle
    cv2.circle(image,(circle[0],circle[1]),circle[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(image,(circle[0],circle[1]),2,(0,0,255),3)

cv2.imshow('Final', image)
cv2.imshow('imageBackup', imageBackup)

cv2.waitKey(0)
cv2.destroyAllWindows()
Tes3awy
  • 2,166
  • 5
  • 29
  • 51
  • What exactly is the problem with the IR camera? Does it find any circle or just nothing? Can you provide a sample image? – PSchn Aug 26 '16 at 13:49
  • @PSchn There is no problem with the camera, the problem is that if no circle is detected I get a NoneType error in the Python Script. I should put this also in the question. – Tes3awy Aug 26 '16 at 13:52
  • Please add the error message too. – PSchn Aug 26 '16 at 13:57
  • Would it make sense to capture a bunch of images with the camera to save off and run with the "static" code? That way you might find which types of images are causing problems... – ebyrob Aug 26 '16 at 13:59
  • to test if your code is correct, yes! – PSchn Aug 26 '16 at 14:04
  • So the error means the houghlines function founds no circles, just check the result (like [here](http://stackoverflow.com/questions/16144015/python-typeerror-nonetype-object-has-no-attribute-getitem)) this does not solve your problem but your program wont crash either – PSchn Aug 26 '16 at 14:08
  • @PSchn I got another error when I read the link you provided me the error is: `for circle in circles[0,:]: TypeError: 'NoneType' object has no attribute '__getitem__'` – Tes3awy Aug 26 '16 at 14:17
  • But the result is the same. Circles is empty and so your program crashes?! Am i wrong? – PSchn Aug 26 '16 at 14:21
  • @PSchn yes, circles is empty. I want the code to run without errors so I have time at least to adjust the IR camera, do you get it ? – Tes3awy Aug 26 '16 at 14:23
  • Yes so check if circle != NONE ([here](https://github.com/aleksandaratanasov/opencv/commit/cf0df733dad004df3505866a67c1b62474d8cbf0)) – PSchn Aug 26 '16 at 14:31
  • @PSchn I did, and got the error in the above comment. – Tes3awy Aug 26 '16 at 14:32
  • Check before you iterate over it. – PSchn Aug 26 '16 at 14:36
  • @PSchn I promise I did :D – Tes3awy Aug 26 '16 at 14:37
  • Thwn you should not enter this code! Post your code please :D – PSchn Aug 26 '16 at 14:42
  • @PSchn I modified the code in the question above. – Tes3awy Aug 26 '16 at 14:45
  • Did you tried it like the link above suggested? – PSchn Aug 26 '16 at 14:50
  • If you post an Image i can test it later myself – PSchn Aug 26 '16 at 15:16
  • please post some of the IR images – Micka Aug 26 '16 at 16:23
  • @Micka my problem is not with the IR camera, it is with the code, and the code can also work on any standard webcam as well – Tes3awy Aug 26 '16 at 16:25
  • for reproduction it would be helpful to take an image one of your images that fails so we can see why. – PSchn Aug 26 '16 at 16:30
  • @OsamaAbbas can you remove the rounding stuff? From the error message (without knowing anything about python arrays) it looks like your circles (array of 3 floats) are converted to single integers. Later you try to access each int as it was an array. But since I don't know python, it is just a guess and an interpretation of the error message. – Micka Aug 26 '16 at 16:39

1 Answers1

0

So i tried it out my self and i had the same error. So i modified the code like i already proposed. Here is the snipped:

if circles != None:
    for circle in circles[0,:]:
        # draw the outer circle
        cv2.circle(image,(circle[0],circle[1]),circle[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(image,(circle[0],circle[1]),2,(0,0,255),3)

In addition you can try to use cv2.Canny for better results. Over and out :)

PSchn
  • 718
  • 4
  • 14
  • did you remove circles = np.round(circles[0,:]).astype("int") too? – Micka Aug 26 '16 at 19:24
  • Sorry, but this is not a reliable answer. this [link](http://docs.opencv.org/2.4/modules/imgproc/doc/feature_detection.html#yuen90) gives you a hint of what `cv2.HoughCircles` does. It's opencv 2.4 but it's the same as in opencv 3.1.0-dev though. – Tes3awy Aug 27 '16 at 15:55