Implementation : Vision Chapter 2
Vision-ColorDetection
การทำงานนส่วนของ Vision จะทำหน้าที่เป็น Input รับข้อมูลสะภาพแวดล้อมทางกายภาพแล้วแปลงข้อมูลเป็นภาพสองมิติและสามมิติ โดยใช้อุปกรณ์ที่เรียกว่า Kinect แล้วทำการประมวลภาพผลเพื่อให้ได้ตำแหน่งของวัตถุที่สนใจเพื่อส่งให้ Task Planning ทำงานในขั้นต่อไป
Input
ภาพ RGB และ Depth จาก KinectOutput
Position X,Y,ZProcess
1.แปลงรูปภาพที่ได้จาก kinect แบบ (RGB) ให้เป็น HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_blue = np.array([140, 50, 50]) upper_blue = np.array([180, 255, 255])
# Threshold the HSV image to get only blue colorsblue_mask = cv2.inRange(hsv, lower_blue, upper_blue) mask = blue_mask # Bitwise-AND mask and original imageres = cv2.bitwise_and(frame,frame, mask= mask)
2.หา Contour
blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 50, 250, cv2.THRESH_BINARY)[1]
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(cnt, contours, -1, (0, 255, 0), 2)
3.หาจุดศูนย์กลางมวล
M = cv2.moments(contours[c])
if (M['m00'] != 0): cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00'])
4.แปลงค่า pixel (x,y) เป็น position xyz
position_available, POSITION = mycv.GetWorldCoordinateFromPixel(img_depth_original, cx, cy,floor_ref=True)
5.ถ้ามีการร้องขอ color จะส่งค่า position xyz กลับไปให้
if mypx.Req_Color: mypx.Req_Color = False
data2 = "[TASKOYAKI:RETURN=1 X={0:.0f} Y={1:.0f} Z={2:.0f}]".format(src[0], src[1], src[2])
mypx.Send(data2)
ความคิดเห็น
แสดงความคิดเห็น