> shape_context <- function(shape1, shape2, nbins_theta = 12, nbins_r = 5, r_inner = 0.125, r_outer = 2.0) {
+ # 获取形状1和形状2的特征点数量
+ n1 <- nrow(shape1)
+ n2 <- nrow(shape2)
+
+ # 创建形状1和形状2的距离矩阵
+ dist_mat <- matrix(0, n1, n2)
+ for(i in 1:n1) {
+ for(j in 1:n2) {
+ dist_mat[i, j] <- sqrt(sum((shape1[i, ] - shape2[j, ])^2))
+ }
+ }
+
+ # 初始化形状1和形状2的形状上下文矩阵
+ F1 <- matrix(0, n1, nbins_theta * nbins_r)
+ F2 <- matrix(0, n2, nbins_theta * nbins_r)
+
+ # 计算形状1的形状上下文
+ for(i in 1:n1) {
+ # 确定形状1特征点到原点的极坐标
+ polar_coords <- cart2pol(shape1[i, 1], shape1[i, 2])
+
+ # 将极坐标转换为形状上下文的索引
+ theta_idx <- floor(nbins_theta * (polar_coords$theta + pi) / (2 * pi)) %% nbins_theta + 1
+ r_idx <- sum(polar_coords$r > r_inner) + 1
+
+ # 更新形状上下文矩阵
+ F1[i, (r_idx - 1) * nbins_theta + theta_idx] <- F1[i, (r_idx - 1) * nbins_theta + theta_idx] + 1
+ }
+
+ # 计算形状2的形状上下文
+ for(j in 1:n2) {
+ # 确定形状2特征点到原点的极坐标
+ polar_coords <- cart2pol(shape2[j, 1], shape2[j, 2])
+
+ # 将极坐标转换为形状上下文的索引
+ theta_idx <- floor(nbins_theta * (polar_coords$theta + pi) / (2 * pi)) %% nbins_theta + 1
+ r_idx <- sum(polar_coords$r > r_inner) + 1
+
+ # 更新形状上下文矩阵
+ F2[j, (r_idx - 1) * nbins_theta + theta_idx] <- F2[j, (r_idx - 1) * nbins_theta + theta_idx] + 1
+ }
+
+ # 归一化形状上下文矩阵
+ F1 <- F1 / sum(F1)
+ F2 <- F2 / sum(F2)
+
+ # 计算形状1和形状2的形状距离
+ dist_mat_norm <- dist_mat / max(dist_mat)
+ cost_mat <- dist_mat_norm^2 + 1 / nbins_theta^2 * abs(F1 - F2)^2
+ cost_mat <- cbind(cost_mat, matrix(Inf, n1, n1))
+ cost_mat <- rbind(cost_mat, matrix(Inf, n2, n2))
+
+ # 使用匈牙利算法求解形状匹配
+ matching_indices <- solve_LSAP(cost_mat)$cols[-(n1 + 1):-(n1 + n2)]
+
+ # 返回最佳匹配点对索引
+ return(matching_indices)
+ }
> # 将笛卡尔坐标转换为极坐标
> cart2pol <- function(x, y) {
+ r <- sqrt(x^2 + y^2)
+ theta <- atan2(y, x)
+ return(list(r = r, theta = theta))
+ }
> shape1 <- matrix(c(1, 2, 3, 4, 5, 6), ncol = 2, byrow = TRUE)
> shape2 <- matrix(c(2, 3, 4, 5, 6, 7), ncol = 2, byrow = TRUE)
> matching_indices <- shape_context(shape1, shape2)
Error in dist_mat_norm^2 + 1/nbins_theta^2 * abs(F1 - F2)^2 :
non-conformable arrays
> matching_indices <- shape_context(shape1, shape2)
Error in dist_mat_norm^2 + 1/nbins_theta^2 * abs(F1 - F2)^2 :
non-conformable arrays
请问一下,这种问题要怎么解决?


雷达卡



京公网安备 11010802022788号







