有没有比使用for循环方式检查python中的灰度更快的方法

时间:2017-10-11 21:36:49

标签: python image cv2

我正在尝试编写一个函数来确定图像是否为灰度,然后如果没有则转换为灰度,并在最后返回该图像。

下面的for循环代码工作正常,但速度很慢。有没有更快的方法来检查这个?我查看了CV2文档但无法找到任何内容。

Sub SOExample()
    Dim mySheet         As Worksheet: Set mySheet = ThisWorkbook.Sheets("Sheet1")
    Dim headerRng       As Range: Set headerRng = mySheet.Range("A1:J1") 'Specify where to do replacements
    Dim mycell          As Range
    Dim vkey            As Variant
    Dim myDict          As Object: Set myDict = CreateObject("Scripting.Dictionary")

    'Iterate each header row add the address as the key, and the NEXT row's Text as the value
    For Each mycell In headerRng
        If Not myDict.exists(mycell.Offset(1, 0).Address) Then
            myDict.Add mycell.Offset(1, 0).Address, mycell.Text
        End If
    Next

    'Iterate each cells formula and replace it
    For Each mycell In headerRng
        For Each vkey In myDict.keys
            mycell.Offset(1, 0).Formula = Replace(mycell.Offset(1, 0).Formula, vkey, myDict(vkey), , , vbTextCompare)
        Next
    Next

End Sub

0 个答案:

没有答案