next step is to work on interface to move svg arround

main
Tony 2021-12-20 17:45:42 -05:00
rodzic 5a31e3a8ad
commit 4e79250d73
3 zmienionych plików z 82 dodań i 256 usunięć

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 68 KiB

BIN
cnc3.jpeg 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 1.2 MiB

Wyświetl plik

@ -429,8 +429,8 @@ def generate_dest_locations(boxWidth, corners, image):
return locations, image
def pixel_loc_at_cnc_bed(boxWidth, backward):
return np.array([[0,0],[-boxWidth*8,0],[-boxWidth * 8,boxWidth*44],[0,boxWidth*44]]), \
cv2.perspectiveTransform(np.array([[0,0],[-boxWidth*8,0],[-boxWidth * 8,boxWidth*44],[0,boxWidth*44]]).reshape(-1,1,2), backward)
return np.array([[0,0],[-boxWidth*8,0],[-boxWidth * 8,boxWidth*34],[0,boxWidth*34]]), \
cv2.perspectiveTransform(np.array([[0,0],[-boxWidth*8,0],[-boxWidth * 8,boxWidth*34],[0,boxWidth*34]]).reshape(-1,1,2), backward)
#############################################################################
@ -445,276 +445,102 @@ cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 800)
# cap.set(3,1280)
# cap.set(4,720)
# The above step is to set the Resolution of the Video. The default is 640x480.
# This example works with a Resolution of 640x480.
#Wait until 3 circles are found (contour for circle, contour for mask around circle)
contours = []
while len(contours) != 8:
# Capture frame-by-frame
#ret, frame = cap.read()
boxWidth = 32.8125
frame = cv2.imread('cnc2.jpeg')
img = cv2.imread('cnc2.jpeg')
# Capture frame-by-frame
#ret, frame = cap.read()
boxWidth = 32.8125
frame = cv2.imread('cnc3.jpeg')
img = cv2.imread('cnc3.jpeg')
# load the image, clone it for output, and then convert it to grayscale
# load the image, clone it for output, and then convert it to grayscale
output = frame.copy()
output = frame.copy()
#######################################################################
# Get grayscale image above threshold
#######################################################################
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#gray1,gray2 = crop_half_vertically(gray)
#gray = gray2
#gray = cv2.resize(gray, (int(sys.argv[1]), int(sys.argv[2])))
#######################################################################
# Get grayscale image above threshold
#######################################################################
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#gray = cv2.resize(gray, (1280, 1000))
#gray1,gray2 = crop_half_vertically(gray)
#gray = gray1
#gray = cv2.resize(gray, (int(sys.argv[1]), int(sys.argv[2])))
########################################
# Get arco box information
########################################
boxes, ids, rejectedImgPoints = cv2.aruco.detectMarkers(gray, cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_100))
print("boxes")
print(boxes)
print("ids")
print(ids)
########################################
# Transform output into list of points and physical locations of those points
########################################
pixelLoc = [None]*2
locations = [None]*2
pixelToPhysicalLoc = [None]*2
physicalToPixelLoc = [None]*2
pixelsAtBed = [None]*2
refPointsAtBed = [None]*2
for i in range(0, 2):
# try:
pixelLoc[i], locations[i], gray = boxes_to_point_and_location_list(boxes, ids, boxWidth, gray, i == 1)
print(locations[i])
########################################
# Get arco box information
#Determine forward and backward transformation through homography
########################################
boxes, ids, rejectedImgPoints = cv2.aruco.detectMarkers(gray, cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_50))
print(boxes)
print(ids)
pixelToPhysicalLoc[i], status = cv2.findHomography(np.array(pixelLoc[i]), np.array(locations[i]))
physicalToPixelLoc[i], status = cv2.findHomography(np.array(locations[i]), np.array(pixelLoc[i]))
#im_out = cv2.warpPerspective(gray, forward, (int(800), int(600)))
#cv2.rectangle(im_out, (int(32.8125*7),int(32.8125*1)),(int(32.8125*8),int(32.8125*2)),(255,0,0),-1)
########################################
# Transform output into list of points and physical locations of those points
########################################
pixelLoc = [None]*2
locations = [None]*2
pixelToPhysicalLoc = [None]*2
physicalToPixelLoc = [None]*2
pixelsAtBed = [None]*2
refPointsAtBed = [None]*2
#############################################################
# Draw vertical box on left and right vertical region of CNC
#############################################################
refPointsAtBed[i], pixelsAtBed[i] = pixel_loc_at_cnc_bed(boxWidth, physicalToPixelLoc[i])
for i in range(0, 2):
pixelLoc[i], locations[i], gray = boxes_to_point_and_location_list(boxes, ids, boxWidth, gray, i == 1)
print(locations[i])
########################################
#Determine forward and backward transformation through homography
########################################
pixelToPhysicalLoc[i], status = cv2.findHomography(np.array(pixelLoc[i]), np.array(locations[i]))
physicalToPixelLoc[i], status = cv2.findHomography(np.array(locations[i]), np.array(pixelLoc[i]))
#im_out = cv2.warpPerspective(gray, forward, (int(800), int(600)))
#cv2.rectangle(im_out, (int(32.8125*7),int(32.8125*1)),(int(32.8125*8),int(32.8125*2)),(255,0,0),-1)
refPointsAtBed[i], pixelsAtBed[i] = pixel_loc_at_cnc_bed(boxWidth, physicalToPixelLoc[i])
#out = cv2.perspectiveTransform(np.array([[32.8125*7,32.8125*1],[32.8125*8,32.8125*1],[32.8125*8,32.8125*2],[32.8125*7,32.8125*2]]).reshape(-1,1,2), backward)
line1 = tuple(pixelsAtBed[i][0][0].astype(np.int))
line2 = tuple(pixelsAtBed[i][1][0].astype(np.int))
line3 = tuple(pixelsAtBed[i][2][0].astype(np.int))
line4 = tuple(pixelsAtBed[i][3][0].astype(np.int))
cv2.line(gray, line1,line2,(125,0,0),3)
cv2.line(gray, line2,line3,(155,0,0),3)
cv2.line(gray, line3,line4,(155,0,0),3)
cv2.line(gray, line4,line1,(155,0,0),3)
refPoints = np.array([[0,0],[0,-boxWidth*44],[-boxWidth*44,0],[-boxWidth*44,-boxWidth*44]])
refPixels = np.array([pixelsAtBed[0][1],pixelsAtBed[0][2],pixelsAtBed[1][1],pixelsAtBed[1][2]])
print()
print()
print()
print()
print(refPixels)
print(refPoints)
bedPhysicalToPixelLoc, status = cv2.findHomography(refPoints, refPixels)
bedPixelToPhysicalLoc, status = cv2.findHomography(refPixels, refPoints)
print(bedPhysicalToPixelLoc)
pixels = cv2.perspectiveTransform(np.array([[0,0],[-boxWidth*44,0],[-boxWidth * 44,-boxWidth*44],[0,-boxWidth*44]]).reshape(-1,1,2), bedPhysicalToPixelLoc)
line1 = tuple(pixels[0][0].astype(np.int))
line2 = tuple(pixels[1][0].astype(np.int))
line3 = tuple(pixels[2][0].astype(np.int))
line4 = tuple(pixels[3][0].astype(np.int))
#out = cv2.perspectiveTransform(np.array([[32.8125*7,32.8125*1],[32.8125*8,32.8125*1],[32.8125*8,32.8125*2],[32.8125*7,32.8125*2]]).reshape(-1,1,2), backward)
line1 = tuple(pixelsAtBed[i][0][0].astype(np.int))
line2 = tuple(pixelsAtBed[i][1][0].astype(np.int))
line3 = tuple(pixelsAtBed[i][2][0].astype(np.int))
line4 = tuple(pixelsAtBed[i][3][0].astype(np.int))
cv2.line(gray, line1,line2,(125,0,0),3)
cv2.line(gray, line2,line3,(155,0,0),3)
cv2.line(gray, line3,line4,(155,0,0),3)
cv2.line(gray, line4,line1,(155,0,0),3)
# except:
# pass
#gray = cv2.resize(gray, (1280, 700))
#cv2.imshow('image',gray)
#cv2.waitKey()
#############################################################
# Draw box on CNC bed
#############################################################
bedPixelCorners = np.array([[1280.0,0.0],[1280.0,700.0],[0.0,0.0],[0.0,700.0]])
refPixels = np.array([pixelsAtBed[0][1],pixelsAtBed[0][2],pixelsAtBed[1][1],pixelsAtBed[1][2]])
bedPhysicalToPixelLoc, status = cv2.findHomography(bedPixelCorners, refPixels)
bedPixelToPhysicalLoc, status = cv2.findHomography(refPixels, bedPixelCorners)
print(bedPhysicalToPixelLoc)
pixels = cv2.perspectiveTransform(bedPixelCorners.reshape(-1,1,2), bedPhysicalToPixelLoc)
line1 = tuple(pixels[0][0].astype(np.int))
line2 = tuple(pixels[1][0].astype(np.int))
line3 = tuple(pixels[3][0].astype(np.int))
line4 = tuple(pixels[2][0].astype(np.int))
cv2.line(gray, line1,line2,(125,0,0),3)
cv2.line(gray, line2,line3,(155,0,0),3)
cv2.line(gray, line3,line4,(155,0,0),3)
cv2.line(gray, line4,line1,(155,0,0),3)
#cv2.imshow('dst',im_out)
gray = cv2.resize(gray, (1280, 700))
cv2.imshow('image',gray)
cv2.waitKey()
#######################################################################
# filter out noise
#######################################################################
# apply GuassianBlur to reduce noise. medianBlur is also added for smoothening, reducing noise.
#gray = cv2.GaussianBlur(gray,(7,7),0);
#gray = cv2.medianBlur(gray,7)
#######################################################################
# Get black and white image of gray scale
#######################################################################
blackWhite = gray.copy()
a,blackWhite = cv2.threshold(blackWhite, 100, 255, cv2.THRESH_BINARY)
im_out = cv2.warpPerspective(gray, bedPixelToPhysicalLoc, (int(1280), int(700)))
#gray = cv2.resize(gray, (1280, 700))
#cv2.imshow('image',gray)
#cv2.waitKey()
#######################################################################
# Find edges
#######################################################################
#Adaptive Guassian Threshold is to detect sharp edges in the Image. For more information Google it.
#gray = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
# cv2.THRESH_BINARY,9,10.5)
#kernel = np.ones((5,5),np.uint8)
#gray = cv2.erode(gray,kernel,iterations = 1)
#gray = cv2.dilate(gray,kernel,iterations = 1)
#######################################################################
# detect circles in the image
#######################################################################
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 50, \
param1=100, \
param2=15, \
minRadius=5, maxRadius=15)
# print circles
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
#######################################################################
# Find all circles
#######################################################################
print()
masks = []
blackScores = []
averageRgbs = []
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle in the image
# corresponding to the center of the circle
#print(output[y,x])
masks.append(np.zeros((output.shape[0], output.shape[1]), np.uint8))
cv2.circle(masks[-1], (x, y), r, (255, 255, 255), -1)
averageRgbs.append(cv2.mean(output, mask=masks[-1])[::-1])
blackScores.append(averageRgbs[-1][1] + averageRgbs[-1][2] + averageRgbs[-1][3])
#print(" " + str(averageRgbs[-1]))
#print(" " + str(blackScores[-1]))
#######################################################################
# Find circles that are black enough to be considered reference point
# put larger circle around those circles to mask out entire image but that area
#######################################################################
referenceCircles = []
circleMask = np.zeros((output.shape[0], output.shape[1]), np.uint8)
for ((x, y, r), blackScore) in zip(circles, blackScores):
#print(" " + str(blackScore))
if blackScore < 300:
referenceCircles.append((x,y,r))
cv2.circle(circleMask, (x, y), 30, (255, 255, 255), -1)
blackWhite = cv2.bitwise_and(blackWhite, blackWhite, mask=circleMask)
contours, hierarchy = cv2.findContours(blackWhite,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
#print("Contours found: " + str(len(contours)))
cv2.imshow('image',gray)
cv2.waitKey()
#######################################################################
# Find center of inside countour
#######################################################################
avgArea = 0
for contour in contours:
avgArea = avgArea + cv2.contourArea(contour)
avgArea = avgArea / len(contours)
insideContours = []
centers = []
i = 0
for contour in contours:
area = cv2.contourArea(contour)
if area < avgArea:
insideContours.append(contour)
M = cv2.moments(contour)
centers.append((M["m10"] / M["m00"], M["m01"] / M["m00"]))
x = int(centers[-1][0])
y = int(centers[-1][1])
if i == 0:
cv2.rectangle(output, (x - 1, y - 40), (x + 1, y + 1), (0, 0, 255), -1)
elif i == 1:
cv2.rectangle(output, (x - 1, y - 30), (x + 1, y + 1), (0, 0, 255), -1)
elif i == 2:
cv2.rectangle(output, (x - 1, y - 20), (x + 1, y + 1), (0, 0, 255), -1)
else:
cv2.rectangle(output, (x - 1, y - 10), (x + 1, y + 1), (0, 0, 255), -1)
i = i + 1
cv2.drawContours(output, insideContours, -1, (0,255,0), 3)
#print(centers)
distToOthers = []
#distToOthers.append(dist(centers[0], centers[1]) + dist(centers[0], centers[2]))
#distToOthers.append(dist(centers[1], centers[0]) + dist(centers[1], centers[2]))
#distToOthers.append(dist(centers[2], centers[0]) + dist(centers[2], centers[1]))
#distToOthers.append(dist(centers[3], centers[2]) + dist(centers[3], centers[1]))
distToOthers = [0,3,1,4]
# sort centers by dist to others, such that shortest distance is first (start of vector)
#centers = [x for _, x in sorted(zip(distToOthers, centers))]
#centers = [(100,0), (100, 100), (0,0)]
#centers = [(100,0), (0, 0), (100,100)]
#print(centers)
#assume x and y vector
origin = np.array(centers[0])
xVector = np.array([centers[1][0] - centers[0][0], centers[1][1] - centers[0][1]])
yVector = np.array([centers[2][0] - centers[0][0], centers[2][1] - centers[0][1]])
angle = math.atan2(xVector[0] * yVector[1] - xVector[1]*yVector[0], xVector[0]*yVector[0] + xVector[1]*yVector[1]) * 360 / 3.14159 / 2.0
# Make it so centers are ordered, origin, xAxis, yAxis
if angle >= 0:
temp = xVector
xVector = yVector
yVector = temp
print("origin: " + str(origin))
print("xVector: " + str(xVector))
print("yVector: " + str(yVector))
#33.25in Y
#40in X
#pixelToMmTransformMatrix = compute_transorm_matrix( {(0,0): origin, \
# (60 * 25.4, 0): origin + xVector, \
# (0, 40 * 25.4) : origin + yVector})
pixelToMmTransformMatrix = compute_transorm_matrix( {(0,0): np.array(centers[0]), \
(40 * 25.4, 0): np.array(centers[1]), \
(0, 60 * 25.4) : np.array(centers[2]),
(40 * 25.4, 60 * 25.4) : np.array(centers[3])})
print("centers")
print(centers)
h, status = cv2.findHomography(np.array([[centers[0][0], centers[0][1]], \
[centers[1][0], centers[1][1]], \
[centers[2][0], centers[2][1]], \
[centers[3][0], centers[3][1]]]), \
np.array([[0,600],[400,600],[0,0],[400,0]]) \
)
im_out = cv2.warpPerspective(output, h, (int(800), int(1200)))
#print(im_out)
#x = int(25.4 * 20)
#y = int(25.4 * 20)
#cv2.rectangle(im_out, (x - 1, y - 20), (x + 1, y + 1), (0, 0, 255), -1)
#x = int(25.4 * 40)
#y = int(25.4 * 20)
#cv2.rectangle(im_out, (x - 1, y - 20), (x + 1, y + 1), (0, 0, 255), -1)
#x = int(25.4 * 40)
#y = int(25.4 * 40)
#cv2.rectangle(im_out, (x - 1, y - 20), (x + 1, y + 1), (0, 0, 255), -1)
#x = int(25.4 * 20)
#y = int(25.4 * 40)
#cv2.rectangle(im_out, (x - 1, y - 20), (x + 1, y + 1), (0, 0, 255), -1)
#cv2.imshow("warpedImage", im_out)
#overlay = overlaySvg(im_out, origin, xVector, yVector)
#overlay = overlaySvg2(imout, pixelToMmTransformMatrix)
overlay = overlaySvg3(im_out)