楼主: wxy111111111
621 1

[求助] 形状匹配算法函数出错要怎么解决 [推广有奖]

  • 0关注
  • 0粉丝

学前班

40%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
20 点
帖子
1
精华
0
在线时间
0 小时
注册时间
2023-10-7
最后登录
2023-12-30

楼主
wxy111111111 发表于 2023-10-7 21:01:39 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
> 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
请问一下,这种问题要怎么解决?

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:算法函数 Context Contex Shape Text 统计学 变点 代码

沙发
sun_man 在职认证  发表于 2023-10-8 11:37:11
在您提供的代码中,出现了两个错误:非一致的数组和未定义的函数solve_LSAP。让我们逐个解决这些问题。

首先,关于非一致的数组错误。这个错误是由于在计算cost_mat时,F1和F2的维度不匹配导致的。在shape_context函数中,我们可以看到F1和F2的维度是(n1, nbins_theta * nbins_r)和(n2, nbins_theta * nbins_r)。但是在计算cost_mat时,我们使用了abs(F1 - F2),这将导致维度不匹配。为了解决这个问题,我们需要确保F1和F2的维度相同。您可以尝试修改代码如下:

cost_mat <- dist_mat_norm^2 + 1 / nbins_theta^2 * abs(rep(F1, each = n2) - rep(F2, times = n1))
这样,我们将F1和F2进行了复制和重复,使它们的维度与dist_mat_norm匹配。

其次,关于未定义的函数solve_LSAP错误。根据您提供的代码,solve_LSAP函数并未定义。您可能需要确保在使用solve_LSAP函数之前,先定义或导入相应的包。如果您没有定义这个函数或不知道它的来源,您可以尝试搜索相关的R包或函数来替代它。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
扫码
拉您进交流群
GMT+8, 2026-1-28 14:06