Ask Your Question

ss32's profile - activity

2020-04-15 11:25:11 -0600 received badge  Famous Question (source)
2018-03-02 01:55:24 -0600 received badge  Notable Question (source)
2017-04-19 18:50:55 -0600 received badge  Popular Question (source)
2014-04-19 17:04:23 -0600 asked a question Error when trying to draw line or rectangle

I'm getting an error when trying to run the following code and have no idea why. This is essentially the exact same code that's used in the tutorials.

The error:

Traceback (most recent call last):
  File "cv_trackbar2.py", line 41, in <module>
    cv2.imshow('frame',img)
cv2.error: /build/buildd/opencv-2.4.5+dfsg/modules/core/src/array.cpp:2482: error: (-206)     Unrecognized or unsupported array type in function cvGetMat

And the code:

import cv2
import numpy as np


# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a green rectangle
img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

while (True):

    cv2.imshow('draw',img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
cv2.destroyAllWindows()
2014-04-18 09:49:10 -0600 commented question Trackbar with video keeps giong to zero

edit: Got it working. You were right, I just had some stuff out of place, thanks.

2014-04-17 19:25:20 -0600 asked a question Trackbar with video keeps giong to zero

I'm trying to use the slider to control my lower and upper bounds for HSV masking. I'm able to get the slider but can't get it to hold the position I set; it keeps going back to zero each time a new frame is pulled in.

import numpy as np
import cv2

def nothing(x):
    pass

cap = cv2.VideoCapture(0)

while(True):

    # Make a window for the video feed  
    cv2.namedWindow('frame',cv2.CV_WINDOW_AUTOSIZE)

    # Capture frame-by-frame
    ret, frame = cap.read()

    # Make the trackbar used for HSV masking    
    cv2.createTrackbar('HSV','frame',0,255,nothing)

    # Name the variable used for mask bounds
    j = cv2.getTrackbarPos('HSV','image')

    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of color in HSV
    lower = np.array([j-10,100,100])
    upper = np.array([j+10,255,255])

    # Threshold the HSV image to get only selected color
    mask = cv2.inRange(hsv, lower, upper)

    # Bitwise-AND mask the original image
    res = cv2.bitwise_and(frame,frame, mask= mask)

    # Display the resulting frame
    cv2.imshow('frame',res)

    # Press q to quit
    if cv2.waitKey(3) & 0xFF == ord('q'):
        break


# When everything is done, release the capture
cap.release()
cv2.destroyAllWindows()
2014-04-13 10:20:40 -0600 received badge  Student (source)
2014-04-13 10:15:19 -0600 asked a question Code coming to a halt after 45 seconds

I'm using opencv to do some object tracking and after running it for more than ~45 it slows down and quickly becomes totally unresponsive and I have to manually kill the process. CPU and RAM usage stay constant at 35% and 2.5% respectively for the first ~35 seconds and then in the following 10 seconds CPU usage blows up and quickly goes to 99% and locks up the machine.

The basic algorithm goes like this:

  1. Import the video frame
  2. Cut size in half to reduce calculation time
  3. Convert to HSV color space
  4. Define upper and lower bounds to define color to track
  5. Create mask based on that color
  6. Apply mask to isolate the color
  7. Dilate the resulting image and erode the edges to reduce noise
  8. Use numpy.where to locate white pixels in resulting binary array
  9. Find the average value of the x-axis position
  10. Compare to left/middle/right boundaries of the frame
  11. Print out relative x-axis position, and send character to arduino
  12. Rinse and repeat

The arduino is running a short code to look for a hex character and has some if cases to turn on LEDs to give visual feedback of the object's location. I have it sleep for 1/100th of a second after sending commands to the arduino to ensure that my computer and the arduino are synced up, and I'm communiating with it via serial at 9600 baud. I don't see any reason why CPU usage would go crazy after such a short time when it has no problem handling the initial 35 seconds.

2014-04-07 19:30:26 -0600 answered a question Locating color in video frame - python

In case someone finds this in a search, I came up with a functional, albeit slow, method.

import cv2
import numpy as np
import serial


cap = cv2.VideoCapture(1)

loc = input('ACM: ')
ser = serial.Serial('/dev/ttyACM'+str(loc), 9600)

while(1):

    # Take each frame
    _, frame = cap.read()
    width = len(frame[0,:])
    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # Define color range for masking
    lower = np.array([110,100,100])
    upper = np.array([130,255,255])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower, upper)

    # Bitwise-AND mask and original image
    result = cv2.bitwise_and(frame,frame, mask= mask)

    gray = cv2.cvtColor(result,cv2.COLOR_BGR2GRAY)

        #(image, max corners, quality level, minimum distance, mask)
    corners = cv2.goodFeaturesToTrack(gray,4,0.01,10)
    corners = np.int0(corners)

    for i in corners:

        x_array = []
        y_array = []


        x,y = i.ravel()
        cv2.circle(result,(x,y),3,255,-1)

        x_array.append(x)
        y_array.append(y)
        # Compare the average value of the X position to the midpoint of the frame
        # and print its relative location
        if (np.mean(x_array) <= int(width/2)):

            print 'left'

        else: 

            print 'right'


    #cv2.imshow('res',result)
    if cv2.waitKey(3) & 0xFF == ord('q'):
        break
        ser.write(str(chr(2)))
        break


cv2.destroyAllWindows()
2014-04-06 17:43:34 -0600 commented question Why is this simple mask not working?

I solved it. Here's the code that got it working. My bounds were off

# Convert to HSV colorspace
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)

# Define color range for masking
lower = n.array([0,100,100])
upper = n.array([20,255,255])

# Apply the mask
mask = cv2.inRange(hsv, lower, upper)

    #(source, destination, mask to apply)
result = cv2.bitwise_and(im,im, mask=mask)
2014-04-06 09:22:08 -0600 commented question Why is this simple mask not working?

I'm not sure I follow you on the lower/upper arrays. The example code I have acts like I would expect, placing lower and upper limits on HSV values.

# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

lower_red = np.array([0,100,100])
upper_red = np.array([20,255,255])

# Threshold the HSV image to get only red colors
mask2 = cv2.inRange(hsv, lower_red, upper_red)

I tried changing the lower/upper arrays in my original code to this:

lower = n.array([0,0,0])
upper = n.array([20,100,255])

And it works, but not well. The resulting mask is extremely rough, and the resulting image is still black, like the mask wasn't applied at all.

2014-04-05 21:00:00 -0600 asked a question Why is this simple mask not working?

I'm in the process of learning how to use masking and the bitwise_and function. I have some tutorial code that works but mine is not and I can't see why. The source image I'm using is here

import numpy as n
import cv2 


# Import the image
im = cv2.imread('orange.jpg')



hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)

upper = n.array([-20,100,100])
lower = n.array([25,100,255])

mask = cv2.inRange(hsv,lower,upper)
result = cv2.bitwise_and(im,im, mask= mask)

# Display the image, esc to kill the window
cv2.imshow('result',result)
cv2.imshow('mask',mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
2014-04-05 11:26:56 -0600 received badge  Editor (source)
2014-04-05 11:25:51 -0600 asked a question Locating color in video frame - python

I'm trying to locate a given color within the frame and compare its location to a given value, ideally I would like a color range. Right now I would like to know if it's on the left or the right hand side of the frame. I'm able to do this for a single frame using a really slow search method and I feel like there has to be a more efficient way of doing this using an opencv function.

Here's what I'm currently using

# Searches an image for a given color and plots results


import cv2 
import pylab as p

# Import the image
im = cv2.imread('orange.jpg')

width = len(im[0,:])
height = len(im[:,0])

# Define color
B = 34
G = 36
R = 36

result_x = []
result_y = []
for i in range(height):
    for j in range(width):
        for k in range(2):
            if (im[i,j,0]==B and im[i,j,1]==G and im[i,j,2]==R):

                result_x.append(j)
                result_y.append(i)


p.plot(result_x,result_y,'ro')
p.xlim(0,width)
p.ylim(0,height)
p.gca().invert_yaxis()
p.show()