我有一个 exe,它返回一个包含 16 个元素的数组。我必须使用 php 将此数组传递给 Mysql 来计算欧几里德距离。我在 MySQL 中的表是这种形式。
id |img_id | features_1|features_2|features_3|features_4|features_5|features_6|features_7|...upto features_16
1 1 0.389 0.4567 0.8981 0.2345
2 2 0.9878 0.4567 0.56122 0.4532
3 3
4 4
......................
所以我为每个图像有 16 个特征,现在我有 30,000 个图像,img_id 最多为 30,000。我必须从通过 php 传递的 exe 中计算数组的欧几里德距离,并返回欧几里德距离最小的 6 个图像的 img_id。
即假设我有一个来自 exe 的数组,A[0.458,0.234,0.4567,0.2398]
我必须使用该数组计算每个 img_id 的距离,即对于 img_id=1,距离将是((0.458-0.389)^2+(0.234-0.4567)^2+(0.4567-0.8981)^2+(0.2398-0.2345)^2)
,我必须对所有 30,000 张图像重复此过程并返回距离最小的 6 个 img_id。我编写了以下存储过程,但它只返回 img_id NULL。
如果有任何快速的方法来执行此操作,请提及。
create procedure sp_euclideandistance(feature1 double,feature2 double,feature3 double,feature4 double,feature5 double,feature6 double,feature7 double,feature8 double,feature9 double,feature10 double,feature11 double,feature12 double,feature13 double,feature14 double,feature15 double,feature16 double)
Begin
Declare i int;
Declare distance double;
Declare img_id int;
Declare features_1 double;
Declare features_2 double;
Declare features_3 double;
Declare features_4 double;
Declare features_5 double;
Declare features_6 double;
Declare features_7 double;
Declare features_8 double;
Declare features_9 double;
Declare features_10 double;
Declare features_11 double;
Declare features_12 double;
Declare features_13 double;
Declare features_14 double;
Declare features_15 double;
Declare features_16 double;
Set i=92303;
Set distance=0.000;
create temporary table temptbl_dist(img_id int,distance int);
while(i<124232)
do
select img_id,features_1,features_2,features_3,features_4,features_5,features_6,features_7,features_8,features_9,features_10,features_11,features_12,features_13,features_14,features_15,features_16 INTO img_id,features_1,features_2,features_3,features_4,features_5,features_6,features_7,features_8,features_9,features_10,features_11,features_12,features_13,features_14,features_15,features_16 from tbl_features where img_id=i;
Set distance=((feature1-features_1)^2)+((feature2-features_2)^2)+((feature3-features_3)^2)+((feature4-features_4)^2)+((feature5-features_5)^2)+((feature6-features_6)^2)+((feature7-features_7)^2)+((feature8-features_8)^2)+((feature9-features_9)^2)+((feature10-features_10)^2)+((feature11-features_11)^2)+((feature12-features_12)^2)+((feature13-features_13)^2)+((feature14-features_14)^2)+((feature15-features_15)^2)+((feature16-features_16)^2);
insert into temptbl_dist values (i,distance);
Set i=i+1;
end while;
select img_id from temptbl_dist order by distance asc limit 6;
drop table temptbl_dist;
End