Ask Your Question

New_H's profile - activity

2017-06-04 12:27:45 -0500 received badge  Editor (source)
2017-06-04 06:26:39 -0500 asked a question How to threshold 3 colors using trackbars ?

Hi,

I have a task says " Threshold three colours using trackbars. Paint the blobs to corresponding colors."

I have created the track bars and they work fine but how can I do this task ?

import numpy as np
import cv2

# open the camera
cap = cv2.VideoCapture(0)

def nothing(x):
    pass
cv2.namedWindow('result')

# Starting with 100's to prevent error while masking
h,s,v = 100,100,100

# Creating track bar


cv2.createTrackbar('h', 'result',0,179,nothing)
cv2.createTrackbar('s', 'result',0,255,nothing)
cv2.createTrackbar('v', 'result',0,255,nothing)

while True:

        #read the image from the camera
        ret, frame = cap.read()        

        #You will need this later
        frame = cv2.cvtColor(frame, 35)



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


        # get info from track bar and appy to result
        h = cv2.getTrackbarPos('h','result')
        s = cv2.getTrackbarPos('s','result')
        v = cv2.getTrackbarPos('v','result')


        # Normal masking algorithm
        lower_blue = np.array([h,s,v])
        upper_blue = np.array([180,255,255])

        mask = cv2.inRange(hsv,lower_blue, upper_blue)

        result = cv2.bitwise_and(frame,frame,mask = mask)

        cv2.imshow('result',result)

        #find center
        cnts=cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]

        center=None

        if len(cnts)>0:
            c=max(cnts, key=cv2.contourArea)
            ((x,y),radius)=cv2.minEnclosingCircle(c)
            M=cv2.moments(c)
            center=(int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

            if radius>10:
                #cv2.circle(frame, (int(x),int(y)), int(radius), 2)
                cv2.circle(frame, center,5,(0,0,255),-1)



        # color detection limits
        lB = 5
        lG = 50
        lR = 50
        hB = 15
        hG = 255
        hR = 255
        lowerLimits = np.array([lB, lG, lR])
        upperLimits = np.array([hB, hG, hR])

        # Our operations on the frame come here
        thresholded = cv2.inRange(frame, lowerLimits, upperLimits)
        outimage = cv2.bitwise_and(frame, frame, mask = thresholded)


        cv2.imshow('original', frame)

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




        # Quit the program when Q is pressed
        if cv2.waitKey(1) & 0xFF == ord('q'):
                break

# When everything done, release the capture
print 'closing program'
cap.release()
cv2.destroyAllWindows()
2017-06-03 12:09:00 -0500 commented answer How to find distance between centers of two green boxes?

but why the distance is changing when I move my cam forward or backward ?

2017-06-03 11:59:31 -0500 received badge  Scholar (source)
2017-06-03 11:59:24 -0500 commented answer How to find distance between centers of two green boxes?

Thank you so much

2017-06-03 08:07:16 -0500 commented answer How to find distance between centers of two green boxes?

but how can I get cX1,cX2 and cY1,cY2 ?

2017-06-03 08:07:16 -0500 commented answer How to find distance between centers of two green boxes?

Thanks a lot for your help :)

2017-06-03 07:03:15 -0500 commented question How to find distance between centers of two green boxes?

in cm or inches .. even in pixels is ok ..

2017-06-03 06:41:26 -0500 asked a question How to find distance between centers of two green boxes?

Hi , I am still beginner in opencv and I have this task :

find the distance between the centers of two green boxes using camera.

I could be able to find the centers of the boxes but how to find the distance between their centers ?

below is my code

really appreciate your help.

Many thanks.

import numpy as np
import cv2
import imutils


cap = cv2.VideoCapture(1)

while(1):

    #read frame and convert the color scheme of the frame BGR to HSV
    _, image = cap.read()
    image = imutils.resize(image, width=600)


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

    #Our operations on the frame come here

    lower_pink = np.array([38, 50, 50])
    upper_pink = np.array([75, 255, 255])

    #Our frame, the HSV image, is thresholded among upper and lower pixel ranges to get only green colors

    mask = cv2.inRange(hsv, lower_pink, upper_pink)
    kernel = np.ones((5,5),'int')
    dilated = cv2.dilate(mask,kernel)

    res = cv2.bitwise_and(image,image, mask=mask)

    # Then we thresholded the masked image and get the contours.


    ret,thresh = cv2.threshold(cv2.cvtColor(res,cv2.COLOR_BGR2GRAY),3,255,cv2.THRESH_BINARY)


    # find contours in the thresholded image
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
            cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]


    # loop over the contours
    for c in cnts:
            # compute the center of the contour
            area=cv2.contourArea(c)
            M = cv2.moments(c)
            if M["m00"] >1000:
                cX = int(M["m10"] / M["m00"])
                cY = int(M["m01"] / M["m00"])

                # draw the contour and center of the shape on the image
                cv2.drawContours(mask, [c], -1, (0, 255, 0), 2)
                cv2.circle(mask, (cX, cY), 7, (0, 255, 255), -1)
                cv2.putText(mask, "center", (cX - 20, cY - 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 2)



    cv2.imshow('frame',mask)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()