def get_diagonal_edges(dominant_pixels, CCA_size_thresh, pca_angles, pca_angle_thresh, aspect_ratio_thresh): diagonal_edges = np.zeros_like(dominant_pixels) _, contours, _ = cv2.findContours(dominant_pixels, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) components_list = [] # stores mean and angle of each component for cnt in contours: if filter_components_aspect_ratio(cnt, aspect_ratio_thresh): continue # Skip if returns True cnt = cnt[:, 0, :] if cnt.shape[0] > CCA_size_thresh: mean, eigenvectors, eigenvalues = cv2.PCACompute2( cnt.astype(np.float64), np.empty((0))) angle = np.degrees( np.arctan2(eigenvectors[0, 1], eigenvectors[0, 0])) for pca in pca_angles: if angle >= pca - pca_angle_thresh and angle <= pca + pca_angle_thresh: components_list.append({ "mean": mean, "slope": eigenvectors[0, 1] / float(eigenvectors[0, 0]) }) for [x, y] in cnt: diagonal_edges[y, x] = 255 return diagonal_edges, components_list
def getOrientation(pts, img, draw): # Construct a buffer used by the pca analysis sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i, 0] = pts[i, 0, 0] data_pts[i, 1] = pts[i, 0, 1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0, 0]), int(mean[0, 1])) # Draw the principal components cv.circle(img, cntr, 3, (42, 89, 247), -1) p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0], cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0]) p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0], cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0]) if (draw): drawAxis(img, cntr, p1, (91, 249, 77), 2) drawAxis(img, cntr, p2, (190, 192, 91), 2) # orientation in radians angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) return cntr, angle
def getOrientationOld(self, contour, frame): sz = len(contour) data_contour = np.empty((sz, 2), dtype=np.float64) for i in range(data_contour.shape[0]): data_contour[i, 0] = contour[i, 0, 0] data_contour[i, 1] = contour[i, 0, 1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_contour, mean) # Store the center of the object cntr = (int(mean[0, 0]), int(mean[0, 1])) cv2.circle(frame, cntr, 3, (255, 0, 255), 2) p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0], cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0]) p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0], cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0]) drawAxis(frame, cntr, p1, (0, 255, 0), 1) drawAxis(frame, cntr, p2, (255, 255, 0), 5) angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) # orientation in radians if (angle > 0): radians = angle else: radians = 2 * pi + angle return radians
def dcs_transform(img, targetMean, targetSigma): # flatten to 2d data = img.reshape(-1, img.shape[-1]) # compute mean and stddev of input dataMu, dataSigma = cv2.meanStdDev(img) # compute pca mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv2.PCACompute2(data, mean) # scaling matrix (Sc) eigDataSigma = np.sqrt(eigenvalues) scale = np.diagflat(1 / eigDataSigma) # stretching matrix (St) # if targetSigma is empty, set sigma of transformed data equal to that of original data if targetSigma is None: stretch = np.diagflat(dataSigma) else: stretch = np.diagflat(targetSigma) # stretching matrix (St) repMu = cv2.repeat(dataMu.T, data.shape[0], 1) zmudata = cv2.subtract(data.astype(float), repMu) if targetMean is not None: repMu = np.tile(targetMean.T, (data.shape[0], 1)) # compute pca transformation transformed = zmudata @ (eigenvectors.T @ scale @ eigenvectors @ stretch) transformed = np.add(transformed, repMu) return transformed.reshape(img.shape)
def getOrientation(pts, img): sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i, 0] = pts[i, 0, 0] data_pts[i, 1] = pts[i, 0, 1] # Perform Principal Component Analysis mean = np.empty((0)) mean, eigenvektoren, eigenwerte = cv.PCACompute2(data_pts, mean) # print("eigenvektoren, eigenwerte: ",eigenvektoren,", ",eigenwerte) centre = (int(mean[0, 0]), int(mean[0, 1])) #creates a yellow point in the centre of the rectangles cv.circle(img, centre, 3, (0, 255, 255), 2) p1 = (centre[0] + 0.02 * eigenvektoren[0, 0] * eigenwerte[0, 0], centre[1] + 0.02 * eigenvektoren[0, 1] * eigenwerte[0, 0]) p2 = (centre[0] - 0.02 * eigenvektoren[1, 0] * eigenwerte[1, 0], centre[1] - 0.02 * eigenvektoren[1, 1] * eigenwerte[1, 0]) drawAxis(img, centre, p1, (0, 0, 255), 1) drawAxis(img, centre, p2, (255, 255, 0), 1) #calculates each rectangles angle in radiant and degree angle = atan2(eigenvektoren[0, 1], eigenvektoren[0, 0]) angleDegree = degrees(angle) anglernd = round(angleDegree, 2) angleString = str(anglernd) font = cv.FONT_HERSHEY_SIMPLEX cv.putText(img, angleString, centre, font, 1, (0, 0, 255), 2, cv.LINE_AA) return angleDegree, centre
def principal_axis(self, crop): if self.seg_type == "edge": X_row, X_col = np.where(crop > 200) X = np.zeros((len(X_row), 2)) X[:, 0] = X_row X[:, 1] = X_col elif self.seg_type == "kmeans": cv2.imwrite("im3.png", crop) X_row, X_col = np.where(crop[:, :, 2] < 80) X = np.zeros((len(X_row), 2)) X[:, 0] = X_row X[:, 1] = X_col if self.pose_type == 0: mean = np.empty((0)) if len(X) > 0: _, eigenvectors, _ = cv2.PCACompute2(X, mean) principal_axis = eigenvectors[0, :] else: principal_axis = [0, 0] elif self.pose_type == 1: if len(X) < 5: return 0 ellipse = cv2.fitEllipse(np.array(X).astype(int)) principal_axis = -np.deg2rad(ellipse[2]) return principal_axis
def getOrientation(pts, img): sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i, 0] = pts[i, 0, 0] data_pts[i, 1] = pts[i, 0, 1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0, 0]), int(mean[0, 1])) # cv.circle(img, cntr, 3, (255, 0, 255), 2) p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0], cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0]) p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0], cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0]) # drawAxis(img, cntr, p1, (0, 255, 0), 1) # drawAxis(img, cntr, p2, (255, 255, 0), 5) angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) # orientation in radians print(eigenvectors) return angle
def getOrientation(pts): # Transfer segmentations into data points sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i, 0] = pts[i, 0, 0] data_pts[i, 1] = pts[i, 0, 1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0, 0]), int(mean[0, 1])) # Draw the circle #cv2.circle(img, cntr, 3, (255, 0, 255), 2) p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0], cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0]) p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0], cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0]) # Draw the axis that represent eigenvector orientation #drawAxis(img, cntr, p1, 30, 1,(0, 0, 255)) #drawAxis(img, cntr, p2, (255, 255, 0), 5) # Calculate angles angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) # orientation in radians return angle
def getOrientation(pts, img): ## [pca] # Construct a buffer used by the pca analysis sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i,0] = pts[i,0,0] data_pts[i,1] = pts[i,0,1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0,0]), int(mean[0,1])) ## [pca] ## [visualization] # Draw the principal components cv.circle(img, cntr, 3, (255, 0, 255), 2) p1 = (cntr[0] + 0.02 * eigenvectors[0,0] * eigenvalues[0,0], cntr[1] + 0.02 * eigenvectors[0,1] * eigenvalues[0,0]) p2 = (cntr[0] - 0.02 * eigenvectors[1,0] * eigenvalues[1,0], cntr[1] - 0.02 * eigenvectors[1,1] * eigenvalues[1,0]) drawAxis(img, cntr, p1, (0, 255, 0), 1) drawAxis(img, cntr, p2, (255, 255, 0), 5) angle = atan2(eigenvectors[0,1], eigenvectors[0,0]) # orientation in radians ## [visualization] return angle
def getOrientation(pts, img): sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i,0] = pts[i,0,0] data_pts[i,1] = pts[i,0,1] mean = np.empty((0)) mean, eigenvektoren, eigenwerte = cv.PCACompute2(data_pts, mean) #Speichert das Centre der Rechtecke centre = (int(mean[0,0]), int(mean[0,1])) #erstellt an der Mitte der Rechtecke einen gelben Punkt cv.circle(img, centre, 3, (0, 255, 255), 2) #achsen p1 = (centre[0] + 0.02 * eigenvektoren[0,0] * eigenwerte[0,0], centre[1] + 0.02 * eigenvektoren[0,1] * eigenwerte[0,0]) p2 = (centre[0] - 0.02 * eigenvektoren[1,0] * eigenwerte[1,0], centre[1] - 0.02 * eigenvektoren[1,1] * eigenwerte[1,0]) drawAxis(img, centre, p1, (0, 0, 255), 1) drawAxis(img, centre, p2, (255, 255, 0), 5) #Orientierung am Winkel in radiant angle = atan2(eigenvektoren[0,1], eigenvektoren[0,0]) #Output: Winkel in Degree print(degrees(angle)) return angle
def getOrientation(pts, img): # Se crea un bufer usado durante el analisis PCA sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i, 0] = pts[i, 0, 0] data_pts[i, 1] = pts[i, 0, 1] # Se realiza analisis PCA mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Se almacena el centro del objeto cntr = (int(mean[0, 0]), int(mean[0, 1])) # Se dibujan los componentes principales cv.circle(img, cntr, 3, (255, 0, 255), 2) p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0], cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0]) p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0], cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0]) drawAxis(img, cntr, p1, (0, 255, 0), 1) drawAxis(img, cntr, p2, (255, 255, 0), 5) angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) # orientacion en radianes return angle
def deco_stretch(file, target_mean=None, target_sigma=None): data_input = file data_mean, data_std = cv2.meanStdDev(data_input) stretch = None pca_data = data_input.flatten() #np.asarray(data_input).reshape(-1) print(pca_data) pca_obj, pca_eigen, eigen_values = cv2.PCACompute2(pca_data, mean=target_mean) eig_data_sigma = np.sqrt(eigen_values) print(eig_data_sigma) scale = np.diag(1 / eig_data_sigma) print(scale) if target_sigma is None: stretch = np.diag(data_std) else: stretch = np.diag(target_sigma) stretch = np.float32(stretch) repmat_data = cv2.repeat(np.transpose(pca_obj), pca_data.shape[0], 1) zmat_data = cv2.subtract(pca_data, repmat_data, dtype=cv2.CV_32F) if target_mean is not None: repmat_data = cv2.repeat(np.transpose(target_mean), pca_data.shape[0], 1) transformed = zmat_data * (np.transpose(pca_eigen) * scale * pca_eigen * stretch) transformed = cv2.add(transformed, repmat_data, dtype=cv2.CV_32F) dstr32f = transformed.reshape(data_input.shape[0], -1, data_input.shape[2]) return dstr32f
def do_PCA(contour): ''' Calculates the best fitting line for a set of data points using Principal Component Analysis. In this context, the set of dataPoints equals the set of pixels of the widest contour. The pixels can be interpreted as coordinates in a coordinate system and PCA will find a line that is essentially the median of all points. PCA is perfect for reducing dimensionalities. Our sets of pixel are scattered over a coordinate system in a two dimensional manner, while the PCA line is one dimensional. ''' dataPoints = np.empty((len(contour), 2), dtype=np.float64) for i, dp in enumerate(dataPoints): # put the x coordinate of the pixel[i] in the left column of the dataset dp[0] = contour[i, 0, 0] # put the y coordinate of the pixel[i] in the right column of the dataset dp[1] = contour[i, 0, 1] # Calculate the mean of the contour and then do the PCA mean = np.empty((0), dtype=np.float64) mean, eigenvectors, eigenvalues = cv2.PCACompute2(dataPoints, mean) # Calculate the angle in radians using the arctangent angleInRadians = math.atan2(eigenvectors[0, 1], eigenvectors[0, 0]) return angleInRadians
def build_PCA(self): """ Builds the model structure with Principal Component Analysis :return: None """ print("Performing PCA...") length = self.training_images[0].shape_vector.n_points * 2 pca_data = np.empty((self.n_images, length), np.float) for i in range(self.n_images): for j in range(length): pca_data[i, j] = self.training_images[i].shape_vector.vector[j] self.pca_shape, self.eigenvectors, self.eigenvalues = cv.PCACompute2( pca_data, mean=None, maxComponents=12) eigenvalues_sum = np.sum(self.eigenvalues) s_cur = 0 for eigenvalue in self.eigenvalues: s_cur += eigenvalue[0] if s_cur > eigenvalues_sum * 0.98: break vd = self.training_images[0].shape_vector.n_points * 2 self.sigma2 = (eigenvalues_sum - s_cur) / (vd - 4) self.pca_full_shape = { 'mean': self.pca_shape, 'eigenvalues': self.eigenvalues, 'eigenvectors': self.eigenvectors }
def getPrincipalAxes(contourPoints): mean = np.empty((0)) mean, eigenvectors, _ = cv.PCACompute2(contourPoints, mean) x = [eigenvectors[0][0], eigenvectors[1][0]] y = [eigenvectors[0][1], eigenvectors[1][1]] rotation = getAngle(horizontal, x, False) return x, y, np.ravel(mean), rotation
def orientation(img, th=130): img_th = img.copy() img_th[img_th < th] = 0 # was 140 # Find all the contours in the thresholded image contours, _ = cv2.findContours(img_th, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) if len(contours) < 1: return None for i, contour in enumerate(contours): # Calculate the area of each contour area = cv2.contourArea(contour) # Ignore contours that are too small or too large if area > 10000: break # Find the orientation of each shape img_pts = np.empty((len(contour), 2), dtype=np.float64) img_pts[:, 0], img_pts[:, 1] = contour[:, 0, 0], contour[:, 0, 1] # PCA analysis mean = np.empty((0)) _, eigenvectors, _ = cv2.PCACompute2(img_pts, mean) angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) return angle
def calcPCA(_, points): mean, eigenVec, eigenVal = cv2.PCACompute2( np.array(points, dtype=np.float32), np.array([], dtype=np.float32)) center = mean[0][0], mean[0][1] val1, val2 = math.sqrt(eigenVal[0][0]), math.sqrt(eigenVal[1][0]) angle = math.atan2(eigenVec[0][1], eigenVec[0][0]) * 180 / math.pi if angle < 0: angle = angle + 180 if math.isnan(val2): val2 = 0 return center, val1, val2, angle
def get_orientation(pts): sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i, 0] = pts[i, 0, 0] data_pts[i, 1] = pts[i, 0, 1] mean = np.empty((0)) mean, eigenvectors, eigenvalues = opencv.PCACompute2(data_pts, mean) return mean, eigenvectors, eigenvalues
def get_orientation(pts, img): sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i, 0] = pts[i, 0, 0] data_pts[i, 1] = pts[i, 0, 1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean) angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) # orientation in radians return angle
def getOrientation(pts): sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i,0] = pts[i,0,0] data_pts[i,1] = pts[i,0,1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean) angle = cv2.phase(eigenvectors[1,1], eigenvectors[1,0],angleInDegrees=True) # convert angle to 0-180 angle180 = angle[0][0] if angle[0][0]<180 else angle[0][0]-180 return angle180
def get_orientation(contour): contour_len = len(contour) data_contour = np.empty((contour_len, 2), dtype=np.float64) for i in range(data_contour.shape[0]): data_contour[i, 0] = contour[i, 0, 0] data_contour[i, 1] = contour[i, 0, 1] mean = np.empty(0) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_contour, mean) center = (int(mean[0, 0]), int(mean[0, 1])) # longest(most distribution = body alignment) eigenvector is eigenvectors[0] return center, eigenvectors[0]
def getOrientation(pts, img): ''' pts:单个轮廓 img:彩色图像,用于显示 ''' sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i, 0] = pts[i, 0, 0] data_pts[i, 1] = pts[i, 0, 1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0, 0]), int(mean[0, 1])) cv.circle(img, cntr, 3, (255, 0, 255), 2) p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0], cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0]) p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0], cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0]) cv.circle(img, (int(p1[0]), int(p1[1])), 3, (0, 0, 255), 2) cv.circle(img, (int(p2[0]), int(p2[1])), 3, (255, 0, 0), 2) drawAxis(img, cntr, p1, (0, 0, 255), 1) drawAxis(img, cntr, p2, (255, 0, 0), 2) # orientation in radians angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) cv.putText(img, F'{angle:.3}', cntr, cv.FONT_HERSHEY_SIMPLEX, 0.8, (155, 136, 254), 2) m = cv.getRotationMatrix2D(cntr, angle * 180 / pi, 1) newp = np.ndarray((sz, 1, 2), dtype=np.int32) for i, p in enumerate(data_pts): px = p[0] * m[0, 0] + p[1] * m[0, 1] + m[0, 2] py = p[0] * m[1, 0] + p[1] * m[1, 1] + m[1, 2] newp[i, 0, 0] = int(px) newp[i, 0, 1] = int(py) rect = cv.boundingRect(newp) rrect = ((rect[0] + 0.5*rect[2], rect[1] + 0.5 * rect[3]), (rect[2], rect[3]), 0) points = cv.boxPoints(rrect) m = cv.getRotationMatrix2D(cntr, -angle * 180 / pi, 1) newp = list() for i, p in enumerate(points): px = p[0] * m[0, 0] + p[1] * m[0, 1] + m[0, 2] py = p[0] * m[1, 0] + p[1] * m[1, 1] + m[1, 2] newp.append((px, py)) return newp, rect
def getOrientation(pts, img, gray): # Construct a buffer used by the PCA analysis. if False: # Uses points on contour. """ sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i,0] = pts[i,0,0] data_pts[i,1] = pts[i,0,1] """ data_pts = np.reshape(pts, [pts.shape[0], -1]).astype(np.float64) elif False: # Uses all points in contour. mask = np.zeros(img.shape[:2], dtype=np.uint8) cv.drawContours(mask, [pts], 0, (255, 255, 255), cv.FILLED) data_pts = cv.findNonZero(mask) data_pts = np.reshape(data_pts, [data_pts.shape[0], -1]).astype(np.float64) else: # Uses non-zero points in contour. mask = np.zeros(img.shape[:2], dtype=np.uint8) cv.drawContours(mask, [pts], 0, (255, 255, 255), cv.FILLED) mask = np.where(mask > 0, gray, mask) data_pts = cv.findNonZero(mask) data_pts = np.reshape(data_pts, [data_pts.shape[0], -1]).astype(np.float64) # Perform PCA analysis. mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Store the center of the object. cntr = (int(mean[0, 0]), int(mean[0, 1])) # Draw the principal components. cv.circle(img, cntr, 3, (255, 0, 255), 2) p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0], cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0]) p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0], cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0]) drawAxis(img, cntr, p1, (0, 255, 0), 1) drawAxis(img, cntr, p2, (255, 255, 0), 5) angle = math.atan2(eigenvectors[0, 1], eigenvectors[0, 0]) # Orientation in radians. return angle
def getOrientation(pts, img, order): sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i,0] = pts[i,0,0] data_pts[i,1] = pts[i,0,1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0,0]), int(mean[0,1])) getLength(pts,cntr, order) font = cv.FONT_HERSHEY_SIMPLEX
def getOrientation(img, pts): ## [pca] # Construct a buffer used by the pca analysis sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i,0] = pts[i,0,0] data_pts[i,1] = pts[i,0,1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0,0]), int(mean[0,1])) return cntr, eigenvectors
def getOrientation(self, pts): sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i,0] = pts[i,0,0] data_pts[i,1] = pts[i,0,1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0,0]), int(mean[0,1])) p1 = (cntr[0] + - 0.02 * eigenvectors[0,0] * eigenvalues[0,0], cntr[1] + - 0.02 * eigenvectors[0,1] * eigenvalues[0,0]) angle = atan2(eigenvectors[0,1], eigenvectors[0,0]) # orientation in radians self.angles.append(angle) if self.draw_arrow: self.drawAxis(cntr, p1)
def getOrientation(data_pts, img): # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0, 0]), int(mean[0, 1])) cv2.circle(img, cntr, 3, (255, 0, 0), 2) p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0], cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0]) p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0], cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0]) drawAxis(img, cntr, p1, (0, 255, 0), 1) drawAxis(img, cntr, p2, (0, 0, 255), 5) angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) # orientation in radians return angle
def getOrientation(pts, img): sz = len(pts) data_pts = np.empty((sz, 2), dtype=np.float64) for i in range(data_pts.shape[0]): data_pts[i,0] = pts[i,0,0] data_pts[i,1] = pts[i,0,1] # Perform PCA analysis mean = np.empty((0)) mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0,0]), int(mean[0,1])) #cv2.circle(img, cntr, 3, (255, 0, 255), 2) angle = atan2(eigenvectors[0,1], eigenvectors[0,0]) # orientation in radians length = 100 x2 = cntr[0] + length*cos(angle) y2 = cntr[1] + length*sin(angle) cv2.line(img,cntr,(int(x2),int(y2)),(0,255,0),1,cv2.LINE_AA) return angle
def get_Orientation(pts, img): ## [pca] # Construct a buffer used by the pca analysis sz = len(pts) data_pts = numpy.empty((sz, 2), dtype=numpy.float64) for i in range(data_pts.shape[0]): data_pts[i, 0] = pts[i, 0, 0] data_pts[i, 1] = pts[i, 0, 1] # Perform PCA analysis mean = numpy.empty((0)) mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean) # Store the center of the object cntr = (int(mean[0, 0]), int(mean[0, 1])) angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0]) # orientation in radians return angle
def basic_blob_classifier(blob_mask): # coords in an array of [x,y] pairs (formatted according to PCACompute2 function) coords = np.array(np.flip(np.where(blob_mask == 0)).T).astype(np.uint8) blob_mask_connected_components, stats = cv2.connectedComponents( blob_mask), cv2.PCACompute2(coords, mean=None) #look for the number of holes and elongatedness holes_count = blob_mask_connected_components[0] - 2 if holes_count == 0: obj_type = "BOLT" elif holes_count == 1: elongatedness = sqrt(stats[EIGENVALUES][0] / stats[EIGENVALUES][1]) obj_type = "WASHER" if elongatedness < ELONGATEDNESS_RATIO else "ROD_A" elif holes_count == 2: obj_type = "ROD_B" else: obj_type = "ERROR" return obj_type, blob_mask_connected_components, stats