Я пытаюсь определить ориентацию продуктов, поэтому я могу использовать эту ориентацию для системы выбора и размещения.
- Я могу определить контур продукта
- Я могу вычислить центр контура
- Я могу рассчитать угол, подгоняя эллипс к контуру, однако результат не стабилен.
Проблема заключается в определении углаТак как продукты находятся на верхней стороне и нижней стороне почти идеальной массы.Расчет угла путем подбора эллипса не является стабильным.иногда вектор указывает влево, а иногда вправо.Как показано на следующем рисунке, вы можете видеть на следующем рисунке (https://answers.opencv.org/upfiles/15684659369233735.png)), что нарисованная линия угла не всегда указывает в одном и том же направлении.
Вот мой код:
import cv2
import numpy as np
import math
# read the image
cap = cv2.imread("20190909_170137.jpg")
def nothing(x):
pass
# create slider
cv2.namedWindow("Trackbars")
hh='Max'
hl='Min'
wnd = 'Colorbars'
cv2.createTrackbar("threshold", "Trackbars", 150, 255, nothing)
cv2.createTrackbar("Houghlines", "Trackbars", 255, 255, nothing)
while True:
frame = cv2.imread("20190909_170137.jpg", cv2.IMREAD_COLOR)
scale_percent = 60 # percent of original size
width = int(frame.shape[1] * scale_percent / 100)
height = int(frame.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
frame = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
# create sliders for variables
l_v = cv2.getTrackbarPos("threshold", "Trackbars")
u_v = cv2.getTrackbarPos("Houghlines", "Trackbars")
#convert frame to Black and White
bw = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#convert Black and White to binary image
ret,thresh4 = cv2.threshold(bw,l_v,255,cv2.THRESH_BINARY)
#find the contours in thresh4
im2, contours, hierarchy = cv2.findContours(thresh4, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
#calculate with contour
for contour in contours:
#calculate area and moment of each contour
area = cv2.contourArea(contour)
M = cv2.moments(contour)
if M["m00"] > 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
#Use contour if size is bigger then 1000 and smaller then 50000
if area > 1000:
if area <50000:
approx = cv2.approxPolyDP(contour, 0.001*cv2.arcLength(contour, True), True)
#draw contour
cv2.drawContours(frame, contour, -1, (0, 255, 0), 3)
#draw circle on center of contour
cv2.circle(frame, (cX, cY), 7, (255, 255, 255), -1)
perimeter = cv2.arcLength(contour,True)
approx = cv2.approxPolyDP(contour, 0.04 * perimeter, True)
#fit elipse
_ ,_ ,angle = cv2.fitEllipse(contour)
P1x = cX
P1y = cY
length = 35
#calculate vector line at angle of bounding box
P2x = int(P1x + length * math.cos(math.radians(angle)))
P2y = int(P1y + length * math.sin(math.radians(angle)))
#draw vector line
cv2.line(frame,(cX, cY),(P2x,P2y),(255,255,255),5)
#output center of contour
print (P1x , P2y, angle)
#detect bounding box
rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)
#draw bounding box
cv2.drawContours(frame, [box],0,(0,0,255),2)
#Detect Hull
hull = cv2.convexHull(contour)
#draw line
#img_hull = cv2.drawContours(frame,[hull],0,(0,0,255),2)
#print (angle)
# print (p)
cv2.imshow("Frame", thresh4)
key = cv2.waitKey(1)
cv2.imwrite('thresh4.png',thresh4)
key = cv2.waitKey(1)
cv2.imshow("bw2", frame)
key = cv2.waitKey(1)
cv2.imwrite('box.png',frame)
key = cv2.waitKey(1)
key = cv2.waitKey(1)
#if key == 27:
# break
break
#cap.release()
cv2.destroyAllWindows()
Есть ли у кого-нибудь идеи, как я могу убедиться, что вычисление угла (ориентации) на 100% правильно.
пример изображения, которое вы можете найти по следующей ссылке:
https://answers.opencv.org/upfiles/15684700185160563.jpg