软件:OpenCV,Python
你好, 我正在一个项目中,尝试使用感兴趣的区域获取某些立方体的真实世界x,y坐标。我能够获得感兴趣的区域并找到对象的中心坐标。在我的情况下,卡住的部分是相对于感兴趣区域的中心坐标,这是一张白纸,大约8 1/4 x 11 3/4英寸。如何找到对象的真实世界中心坐标(如图所示)相对于感兴趣区域的大小?例如,坐标将以3,2返回,即在右侧x方向为3英寸,在y方向为2英寸。这只是一个原型,以后我将使用25cmx25cm的感兴趣区域。我已经附上了我的工作图片和代码以供参考。
谢谢
import cv2
import numpy as np
#find the center of an object
def center(contours):
# calculate moments for each contour
for c in contours:
M = cv2.moments(c)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = 0, 0
tempStr = str(cX) + ", " + str(cY)
cv2.circle(frame, (cX, cY), 1, (0, 0, 0), -1) #make a dot at the center of the object
cv2.putText(frame, tempStr, (cX - 25, cY - 25),cv2.FONT_HERSHEY_TRIPLEX, 0.4, (0, 0, 0), 1) #print the coordinates on the image
#get the region of interest
def find_ROI(frame):
image = frame.copy()
#change to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#Get binary image
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
#create structural element
struc_ele = np.ones((5, 5), np.uint8)
#Use Open Morphology
img_open = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, struc_ele, iterations = 1)
#find contours
_, ctrs, _ = cv2.findContours(img_open.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[1])
for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
# Getting ROI
roi = image[y:y + h, x:x + w]
# show ROI
cv2.imshow('Region of Interest',roi)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('marked areas', image)
cap = cv2.VideoCapture(0)
if __name__ == "__main__":
while True:
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
find_ROI(frame)
#Create kernel to use in filter
kernel = np.ones((5, 5), np.uint8)
#Create filter for yellow
lower_yellow = np.array([15, 100, 100]) #Lower boundary values for HSV
upper_yellow = np.array([30, 255, 255]) #Upper boundary values for HSV
yellow_mask = cv2.inRange(hsv, lower_yellow, upper_yellow) # Threshold the HSV image to get only yellow colors
opening_y = cv2.morphologyEx(yellow_mask, cv2.MORPH_OPEN, kernel, iterations = 2) #Use morphology open to rid of false pos and false neg (noise)
result_y = cv2.bitwise_and(frame, frame, mask = opening_y) #bitwise and the opening filter with the original frame
#Values for green
lower_green = np.array([45, 45, 10])
upper_green = np.array([100, 255, 255])
#Create filter for green
green_mask = cv2.inRange(hsv, lower_green, upper_green)
opening_g = cv2.morphologyEx(green_mask, cv2.MORPH_OPEN, kernel, iterations = 1)
result_g = cv2.bitwise_and(frame, frame, mask = opening_g)
#Tracking the color yellow
_, contours, _ = cv2.findContours(opening_y, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for y_pix, contour in enumerate (contours):
area = cv2.contourArea (contour)
if (area > 300):
center(contours)
x, y, w, h = cv2.boundingRect(contour)
frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255), 2)
cv2.putText(frame, "Yellow", (x, y), cv2.FONT_HERSHEY_TRIPLEX, 0.7, (0, 255, 255))
#Tracking the color green
_, contours, _ = cv2.findContours(opening_g, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for g_pix, contour in enumerate (contours):
area = cv2.contourArea (contour)
if (area > 300):
center(contours)
x, y, w, h = cv2.boundingRect(contour)
frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (87, 139, 46), 2)
cv2.putText(frame, "Green", (x, y), cv2.FONT_HERSHEY_TRIPLEX, 0.7, (87, 139, 46) )
cv2.imshow("Detection and coordinates", frame)
#cv2.imshow("Colors Seen ", res)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()