古代Microsoft DBMS中的汉明距离

时间:2014-07-21 13:57:20

标签: sql-server tsql hamming-distance sql-server-7

问题

我想在MS SQL Server 7中找到图像副本和类似图像。

修改

我使用sql游标运行它 - 它很慢但它有效,再次感谢评论。请参阅我的答案,了解我提出的解决方案。

具体......

我有一个数据库,其中包含在this dhash algorithm的帮助下计算出的相关图像的图像路径和指纹。我正在使用一种变体,我在BINARY(16)列中每个图像存储128位(水平和垂直渐变)。

我希望能够做的是:

DECLARE @id INT
SET @id = ...

DECLARE @given_hash BINARY(16)
SET @given_hash = ...

SELECT TOP 10 file_path,hash,
    (hamming_distance(hash, @given_hash)) AS distance
FROM my_table
WHERE distance <= 20
ORDER BY distance ASC

什么工作?

获得完全相同的重复是微不足道的 - 只需使用WHERE hash = @hash_to_compare,就是这样。

什么不起作用?

但是,我希望能够使用相似性度量(Hamming Distance)来解释小的操作/缺陷/压缩伪像等。 我想出了一个实现距离测量的存储过程:

CREATE PROCEDURE hamming_distance128
    @hash BINARY(16),
    @supplied BINARY(16)
AS
    DECLARE @i INT, @j INT
    SET @i = 1
    SET @j = 1

    DECLARE @count INT
    SET @count = 0

    DECLARE @byte TINYINT

    DECLARE @length TINYINT
    SET @length = 16

    WHILE @i <= @length
    BEGIN
        SET @j = 1
        SET @byte = CAST((ASCII(SUBSTRING(@hash,@i,1)) ^ ASCII(SUBSTRING(@supplied,@i,1))) AS TINYINT)

        WHILE @j < 256
        BEGIN
            SET @count = @count + (CASE (@byte & @j) WHEN 0 THEN 0 ELSE 1 END)
            SET @j = @j * 2
        END

        SET @i = @i + 1
    END

    SELECT @count
GO

不幸的是DBMS(SQL Server 7--无法升级/更改它)不允许我用它来计算查询中的距离,而这条j * nk不支持用户定义的函数。果然我找不到像MySQL BIT_COUNT这样的东西,这使得这对T-SQL毫无疑问。

是否有希望在SQL Server 7上使用T-SQL?

非常感谢帮助!

1 个答案:

答案 0 :(得分:2)

我使用SQL游标和临时表运行它。再次感谢@Tomalak

我提供了我的实现以供将来参考,希望这对任何人都有用

CREATE PROCEDURE hamming_distance128
    @hash BINARY(16),
    @supplied BINARY(16),
    @distance TINYINT OUTPUT
AS
    DECLARE @i INT, @j INT
    SET @i = 1
    SET @j = 1

    DECLARE @count TINYINT
    SET @count = 0

    DECLARE @byte TINYINT

    DECLARE @length TINYINT
    SET @length = 16

    WHILE @i <= @length
    BEGIN
        SET @j = 1
        SET @byte = CAST((ASCII(SUBSTRING(@hash,@i,1)) ^ ASCII(SUBSTRING(@supplied,@i,1))) AS TINYINT)

        WHILE @j < 256
        BEGIN
            SET @count = @count + (CASE (@byte & @j) WHEN 0 THEN 0 ELSE 1 END)
            SET @j = @j * 2
        END

        SET @i = @i + 1
    END

    SET @distance = @count
GO


DECLARE @min_similarity FLOAT
SET @min_similarity = 0.85 -- | 85%

DECLARE @supplied_hash BINARY(16)
SET @supplied_hash = 0x392929295B4B13371B0301272D2B2509

IF OBJECT_ID('tempdb..#distances') IS NOT NULL
BEGIN DROP TABLE #distances END

CREATE TABLE #distances
(id INT NOT NULL,
 similarity FLOAT NOT NULL)

DECLARE
    @tmp_id INT,
    @dhash BINARY(16),
    @distance TINYINT,
    @similarity FLOAT

DECLARE rowCursor CURSOR
LOCAL FORWARD_ONLY READ_ONLY
FOR
    SELECT id,dhash_value
    FROM hashes
    OPEN rowCursor
        FETCH NEXT FROM rowCursor
        INTO @tmp_id,@dhash

        WHILE @@FETCH_STATUS = 0
        BEGIN
            EXECUTE hamming_distance128 @dhash, @supplied_hash, @distance OUTPUT

            IF @distance < 128
            BEGIN
                SET @similarity = CAST(128 - @distance AS FLOAT) / 128.0

                IF @similarity >= @min_similarity
                BEGIN
                    INSERT INTO #distances (id,similarity)
                    VALUES (@tmp_id,@similarity)
                END
            END

            FETCH NEXT FROM rowCursor
            INTO @tmp_id,@dhash
        END
    CLOSE rowCursor
DEALLOCATE rowCursor

SELECT hashes.id,#distances.similarity
FROM #distances
    INNER JOIN hashes
    ON hashes.id = #distances.id
ORDER BY #distances.similarity DESC

DROP TABLE #distances