从Python使用OpenCV的图像哈希模块

时间:2019-04-15 09:05:01

标签: python opencv phash

我想从Python使用OpenCV的perceptual hashing functions

这不起作用。


@section('content')

<div class="row"> 
    <div class="col">
@if (Auth::user()->role == 1)
    <a class="btn btn-success" href="/projects/create">Add Project</a>
@endif
</div>
<div class="col"></div>
<div class="col">
@if(session()->has('store'))
        <div class="alert alert-success mt-2" role="alert">
            <strong>Project created</strong>
        </div>
    @elseif(session()->has('update'))
        <div class="alert alert-success mt-2" role="alert">
            <strong>Project updated</strong>
        </div>
    @elseif(session()->has('delete'))
        <div class="alert alert-success mt-2" role="alert">
            <strong>Project deleted</strong>
        </div>
    @endif
    </div>
    </div>
<br>
<table class="table table-bordered table-hover">
    <thead>
        <tr>
            <th>Project Id</th>
            <th>Title</th>
            <th>Description</th>
            <th>Client Id</th>
            <th>Created by</th>
            <th>Created on</th>
            @if (Auth::user()->role==1)
            <th>Admin</th>
            @endif
        </tr>

    </thead>
    <tbody class="">
        @foreach ($project as $project)
        <tr>
            <td>{{$project->proj_id}}</td>
            <td>{{$project->proj_title}}</td>
            <td>{{$project->proj_desc}}</td>
            <td>{{$project->client_id}}</td>
            <td>{{$project->Auth::user()->name}}</td>
            <td>{{$project->created_at}}</td>
            @if (Auth::user()->role==1)
            <td>
                <div class="dropdown">
                    <button class="btn btn-danger dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action</button>                                                  
                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                    <a class="dropdown-item" href="{{route('projects.edit',$project)}}">Edit</a>

                    <form method="POST" action="{{route('projects.destroy',$project)}}" onsubmit="return confirm('Are you sure you want to delete this?')">
                        @method('DELETE')
                        @csrf
                    <button class="dropdown-item" type="submit">Delete</button>
                    </form>
                 </div>
            </div>
            </td>
            @endif

        </tr>
        @endforeach
    </tbody>
</table>



@endsection

我得到:

import cv2
a_1 = cv2.imread('a.jpg')
cv2.img_hash_BlockMeanHash.compute(a_1)

这也失败了

TypeError: descriptor 'compute' requires a 'cv2.img_hash_ImgHashBase' object but received a 'numpy.ndarray'

我得到:

a_1_base = cv2.img_hash_ImgHashBase(a_1) 
cv2.img_hash_BlockMeanHash.compute(a_1_base)

Colab笔记本显示以下内容:

https://colab.research.google.com/drive/1x5ZxMBD3wFts2WKS4ip3rp4afDx0lGhi

3 个答案:

答案 0 :(得分:3)

这是OpenCV python接口与C ++接口之间的常见兼容性差距(即,类彼此继承的方式不同)。有*_create()个静态函数。

因此您应该使用:

hsh = cv2.img_hash.BlockMeanHash_create()
hsh.compute(a_1)

在您的collab笔记本的副本中: https://colab.research.google.com/drive/1CLJNPPbeO3CiQ2d8JgPxEINpr2oNMWPh#scrollTo=OdTtUegmPnf2

答案 1 :(得分:3)

在这里我向您展示如何使用 OpenCV 计算 64 位 pHash。 我定义了一个函数,它从传入的彩色 BGR cv2 图像返回无符号的 64 位整数 pHash:

import cv2
    
def pHash(cv_image):
        imgg = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY);
        h=cv2.img_hash.pHash(imgg) # 8-byte hash
        pH=int.from_bytes(h.tobytes(), byteorder='big', signed=False)
        return pH

您需要安装并导入 cv2 才能正常工作。

答案 2 :(得分:0)

pip install opencv-python
pip install opencv-contrib-python    #img_hash in this one 

https://pypi.org/project/opencv-python/