① 求 c/c++ 做比較兩張圖片相似度的代碼
循環 for [i , j]
{
讀出圖片A 一點(像素)的 RGB 數值。
計算出灰度 YA[j][i] = 0.3*R + 0.59*G + 0.11*B
讀出圖片B 一點衡橡(像素)的 RGB 數值。
計算出灰度鋒攔或 YB[j][i] = 0.3*R + 0.59*G + 0.11*B
計算 一點 的 相似系數,
例如 灰度差除以兩點平均灰度:
fabs(YA[j][i]-YB[j][i]) / ((YA[j][i]+YB[j][i])/2.0) -- 數值越小越相似
}
有了所有點的相似系數,做統計算,例如,把相似系數分20檔,
計算落入各檔的像素點的個數--就是概率啦。
畫 概率分布圖 和 累加 概率分布銀伍圖。
當然,你可以設 累加 概率等於 幾的地方 為 相似度 判據。
② 用按鍵精靈如何實現 屏幕選定區域內的圖片與圖片庫中的圖片對比
一、 首先定義一個變數並賦值 VBS bing="zb1007zb" SayString bing&"的同學,我試過了,行" 二、 滑鼠形狀 1.關於 滑鼠特徵碼:滑鼠特徵碼就是腳本運行時的滑鼠形狀的代碼 2.獲得腳本運行時時的滑鼠特徵碼:通過getCursorShape這個標准VBS庫函數來返回當前滑鼠的 滑鼠形狀特徵碼 3.在腳本編寫前如何得到滑鼠的特徵碼呢:使用抓點抓色里的滑鼠形狀,使用方法,按鍵精靈抓點抓色窗口裡的滑鼠形狀區塊有兩個復選框,當兩個都勾上時(ctrl,alt),表示在該窗口打開時(最小化也可以),運行其它程序,滑鼠的形狀發生變化後,如變為"忙"的滑鼠指針,你按下ctrl+alt+q這個<抓點抓色窗口>注冊的全局快捷鍵時,會將滑鼠特徵碼捕獲到<抓點抓色窗口>第一個滑鼠特徵碼 的文本框中,並將捕獲到的滑鼠形狀的圖形也放在它前面的預覽框中,按信棗耐下ctrl+alt+w捕獲到第二個框中.ctrl+alt+e捕獲到第三個.之前是兩上勾,去掉ctrl前的勾就表示,注冊alt+q為捕獲滑鼠形狀的全局快捷鍵.然後你就可以 復制 框中的滑鼠特徵碼 的數值 4.例子(改官方幫助): //獲滑春得當前滑鼠形狀的滑鼠特徵碼 VBSCall shape=GetCursorShape(0) //與你已知的滑鼠特徵碼進行比較 If shape="你復制的一個整數的岩悔滑鼠特徵碼" VBSCall MessageBox(shape) EndIf 終於寫完... 使用點陣圖 1.我想你想問的還是<抓點抓色窗口>上的點陣圖功能吧 2.是這樣的,有些時候不方便你在開啟了<抓點抓色窗口>後去屏幕上取色,如:一個全屏的畫面變換很快的游戲,很難在你一邊玩的時候還能一邊抓色.所以你可以事先把全屏游戲一個畫面截圖下來,在<抓點抓色窗口>中點載入點陣圖,或在開啟<抓點抓色窗口>後玩游戲時按下PrintScreen鍵,就可以將<抓點抓色窗口>的下面多一個區域,這個區域有你要抓的動態畫面的一截圖,然後就可以好比你抓靜態屏幕一樣的抓取坐標和色值. 對第二部分的總結: <抓點抓色窗口>只是為程序運行時提供可以判斷的值,有顏色值、坐標值、滑鼠形狀值,在腳本運行時,再獲取這些值,與事先抓好值比較,由比較結果來決定執行些語句。 以上全是個人理解,第一個字都是自己打的,有回答得不好,或認為不妥,網路消息我。(在消息的內容里加上問題的網址哦)
③ matlab批量對比圖片相似度
批量讀取500張圖片,轉化成灰度圖,然後批量轉化成直方圖,最後你就可以計算相似度了
④ 使用Python 製作對比圖片相似度的程序
import media
def red_average(pic):
'''Return an integer that represents the average red of the picture.
'''
total=0
for pixel in pic:
total = total + media.get_red(pixel)
red_average = total / (media.get_width(pic)*media.get_height(pic))
return red_average
def green_average(pic):
'''Return an integer that represents the average green of the picture
'''
total = 0
for pixel in pic:
total = total + media.get_green(pixel)
green_average = total / (media.get_width(pic)*media.get_height(pic))
return green_average
def blue_average(pic):
''臘梁'Return an integer that represents the average blue of the picture
'''
total = 0
for pixel in pic:
total = total + media.get_blue(pixel)
blue_average = total /瞎局攜 (media.get_width(pic)*media.get_height(pic))
return blue_average
def scale_red(pic, value):
'''Return the picture that the average of the red is value which has been set.
'''
averaged = red_average(pic)
factor = float(value) / averaged
for pixel in pic:
new_red = min(255, int(factor * media.get_red(pixel)))
media.set_red(pixel,new_red)
return pic
def scale_green(pic, value):
'''Return the picture that the average of the green is value which has been set.
'''
averaged = green_average(pic)
factor = float(value) / averaged
for pixel in pic:
new_green = min(255, int(factor * media.get_green(pixel)))
media.set_green(pixel,new_green)
return pic
def scale_blue(pic, value):
'''Return the picture that the average of the blue is value which has been set.
''磨伏'
averaged = blue_average(pic)
factor = float(value) / averaged
for pixel in pic:
new_blue = min(255, int(factor * media.get_blue(pixel)))
media.set_blue(pixel,new_blue)
return pic
def expand_height(pic, factor):
'''Return a newpicture that has been vertically stretched by the factor which has been set.
'''
new_width = pic.get_width()
new_height = pic.get_height()*factor
newpic = media.create_pic(new_width, new_height, media.black)
for pixel in pic:
x = media.get_x(pixel)
y = media.get_y(pixel)
newpixel = media.get_pixel(newpic, x, y*factor)
for newpixel in newpic:
new_red = media.get_red(pixel)
new_green = media.get_green(pixel)
new_blue = media.get_blue(pixel)
media.set_red(newpixel,new_red)
media.set_green(newpixel,new_green)
media.set_blue(newpixel,new_blue)
return newpic
def expand_width(pic,factor):
'''Return a newpicture that has been horizontally stretched by the factor which has been set.
'''
new_width = pic.get_width() * factor
new_height = pic.get_height()
newpic = media.create_pic(new_width,new_height,media.black)
for newpixel in newpic:
x = media.get_x(newpixel)
y = media.get_y(newpixel)
pixel = media.get_pixel(pic,x / factor, y)
new_red = media.get_red(pixel)
new_green = media.get_green(pixel)
new_blue = media.get_blue(pixel)
media.set_red(newpixel,new_red)
media.set_green(newpixel,new_green)
media.set_blue(newpixel,new_blue)
return newpic
def rece_height(pic, factor):
'''return a new pic that has been compressed vertically by the factor which has been set
'''
# Create a new, all-black pic with the appropriate new height and
# old width; (all colour components are zero).
new_width = pic.get_width
new_height = (pic.get_height() - 1) / factor + 1
newpic = media.create_pic(new_width, new_height, media.black)
# Iterate through all the pixels in the original (large) image, and
# a portion of each pixel's colour components into the correct
# pixel position in the smaller image.
for pixel in pic:
# Find the corresponding pixel in the new pic.
x = media.get_x(pixel)
y = media.get_y(pixel)
newpixel = media.get_pixel(newpic, x, y / factor)
# Add the appropriate fraction of this pixel's colour components
# to the components of the corresponding pixel in the new pic.
new_red = newpixel.get_red()+pixel.get_red()/factor
new_green = newpixel.get_green()+pixel.get_green()/factor
new_blue = newpixel.get_blue()+pixel.get_blue()/fctor
media.set_red(newpixel,new_red)
media.set_green(newpixel,new_green)
media.set_blue(newpixel,new_blue)
return newpic
def rece_width(pic,factor):
'''Return a newpic that has been horizontally compressed by the factor which has been set.
'''
new_width = (media.get_width() - 1) / factor + 1
new_height = media.get_height()
newpic = media.create_pic(new_width, new_height, media.black)
for pixel in pic:
x = media.get_x(pixel)
y = media.get_y(pixel)
new_pixel = media.get_pixel(newpic, x / factor, y)
new_red = newpixel.get_red() + pixel.get_red() / factor
new_green = newpixel.get_green() + pixel.get() / factor
new_blue = newpixel.get_blue() + pixel.get()/factor
media.set_red(newpixel, new_red)
media.set_green(newpixel, new_green)
media.set_blue(newpixel, new_blue)
return newpic
def distance(pixel1, pixel2):
red1 = media.get_red(pixel1)
green1 = media.get_green(pixel1)
blue1 = media.get_blue(pixel1)
red2 = media.get_red(pixel2)
green2 = media.get_green(pixel2)
blue2 = media.get_blue(pixel2)
sum = abs(red1 -red2) + abs(green1 - green2) + abs(blue1 - blu2)
return sum
def simple_difference(pic1, pic2):
for pixel in pic1:
x = media.get_x(pixel)
y = media.get_y(pixel)
pixel2 = media.get_pixel(pic2, x, y)
sum = media.distance(pixel, pixel2)
return sum
def smart_difference(pic1,pic2):
height1 = media.get_height(pic1)
height2 = media.get_height(pic2)
factorh = float(height1 / height2)
if factorh >= 1:
height1 = media.rece_height(pic1, factorh)
else:
height2 = media.rece_height(pic2, 1 / factorh)
width1 = media.get_width(pic1)
width2 = media.get_width(pic2)
factorw = float(width1 / width2)
if factorw >= 1:
width1 = rece_width(pic1, factorw)
else:
width2 = rece_width(pic2, 1 / factorw)
red1 = red_average(pic1)
green1 = green_average(pic1)
blue1 = blue_average(pic1)
red2 = media.scale_red(pic2, red1)
green2 = media.scale_green(pic2, green1)
blue2 = media.scale_blue(pic2, blue1)
#if __name__ == '__main__':
#media.show(newpic)
⑤ C#圖片比較(截圖和本地圖片)
1、要保持截圖的解析度,最好保存為BMP格式
2、圖像對比,你采正伏用的什麼方式對比的
3、你所截取的圖像,來自什麼設備或什麼視頻源
4、胡世最好共享一下代舉做攜碼,在代碼基礎之上修改
⑥ 用c#怎麼比較兩張圖片的不同
圖片轉換為2進制流,再比較。
下面為轉換參考代盯純碼:
//圖片轉二進制流,Blob 最大 64K, MediumBlob 最大16M
private byte[] PictureToByte(string picturePath)
{
FileStream fs = new FileStream(picturePath, FileMode.Open);
byte[] streamByte = new byte[fs.Length];
fs.Read(streamByte, 0, streamByte.Length);
fs.Close();
return streamByte;
}
//二進制流轉凱慧咐圖片
private System.Drawing.Image ByteToPicture(byte[] streamByte)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
return img;
}
注意碧梁加引用 using System.IO;
⑦ matlab中比較圖片相似性,下面是我找到的一個代碼,第二行就運行不出來,圖片在哪裡導入小白一個,求助呀
這基拿前半部分不是在建函數嗎,怎麼和腳本放在一起了。應該另外先把搏皮搭函數建好(注意文件名必須和函數名相同,即』tineyesearch_hist『)。然後在重新建個腳本調用你自己建好的握芹函數。
⑧ 怎麼用C#比較兩張圖片是否相同不是表面的比較,比較內容,通過圖片的內容。
以圖片的形式打開他困笑們,如果其中一張無法正常打開,則圖片已損壞
如果兩張都可以打開,則襪尺橡對比兩張的每個像素點,如果相同位告旁置的像素點的RGB值不一樣,則兩張圖片不同
⑨ VB如何實現比較兩幅圖片
如果兩個圖像文件是同樣格式的,那就不要用傳統的方法打開圖像、載入圖像什麼的,因為我試驗過,VB對圖像的支持是很差的,圖像太大或是格式太新,都沒法正常載入的,所以,我覺的,如果只是判斷兩個圖像是否一樣,並且兩個圖像文件弊姿培的格式相同的話,完全可租唯以把兩個圖像文件當成一般的二進制文件打開,然後一個位元組一個位元組的對比……冊答
Option Explicit
Private Sub Command1_Click()
Const CD = 1024 '一次進行比較的位元組數
Dim i As Long, j As Long, k As Long
Dim A As Long, B As Long, C As Long
Dim Sa(1 To CD) As Byte, Sb(1 To CD) As Byte
Dim Fa As String, Fb As String
Fa = "e:\c.bmp" '要比較的兩個文件
Fb = "e:\d.bmp"
C = FileLen(Fa)
If C <> FileLen(Fb) Then
MsgBox "不一樣,文件長度不同", , ""
Exit Sub
End If
A = C \ CD
B = C Mod CD
Open Fa For Binary Access Read As #1
Open Fb For Binary Access Read As #2
For i = 1 To A
For j = 1 To CD
DoEvents
Get #1, , Sa(j)
Get #2, , Sb(j)
If Sa(j) <> Sb(j) Then
MsgBox "不一樣,文件內容不同", , ""
Exit Sub
End If
Next
Next
For j = 1 To B
Get #1, , Sa(j)
Get #2, , Sb(j)
If Sa(j) <> Sb(j) Then
MsgBox "不一樣,文件內容不同", , ""
Exit Sub
End If
Next
Close #1
Close #2
MsgBox "一樣", , ""
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Unload Me
End
End Sub
⑩ vb圖像對比
'我只會靠對應像素顏色是否相同判斷(可以判斷找茬圖片,但兩副圖片尺寸要求嚴格對應),如果轎昌你說的是圖像匹配就相當復雜了~
'閉緩扒新建窗體,添加picture1(0),picture1(1),picture2,command1,hscroll1
'以下保存在模塊哪閉中:
Public Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long
Public Const STRETCH_HALFTONE = 4
Public Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Public Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Public Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
'以下保存在窗體中
Dim bit1() As Byte, bit2() As Byte, bit3() As Byte
Private Sub Form_Load()
Me.ScaleMode = 3 '像素模式
Picture1(0).ScaleMode = 3
Picture1(0).Move 10, 10, 400, 300
Picture1(0).AutoRedraw = True
Picture1(0).Print "基準圖像(雙擊選擇)"
Picture1(1).Move 420, 10, 400, 300
Picture1(1).AutoRedraw = True
Picture1(1).Print "輸入圖像(雙擊選擇)" & vbCrLf & vbTab & "點擊對比按鈕該圖像與基準圖像相同部分變暗,不同部分亮顯。" & vbCrLf & "單擊右鍵還原上次對比前輸入圖片。"
Command1.Move 10, 320, 100, 30
Command1.Caption = "對比:閥值30"
Command1.Tag = 0
Command1.Default = True
HScroll1.Move 120, 320, 700, 30
HScroll1.Max = 255
HScroll1.Value = 30
Picture2.ScaleMode = 3
Picture2.AutoSize = True
Picture2.AutoRedraw = True
Picture2.Visible = False
End Sub
Private Sub HScroll1_Change()
If Command1.Tag = 1 Then SetBitmapBits Picture1(1).Image, UBound(bit2), bit2(1) '還原上次對比前圖片
Command1.Caption = "對比:閥值" & HScroll1.Value
End Sub
Private Sub Picture1_DblClick(Index As Integer)
filenam = openfile
If filenam <> "" Then
Picture2.Picture = LoadPicture(filenam)
SetStretchBltMode Picture1(Index).hdc, 4
StretchBlt Picture1(Index).hdc, 0, 0, 400, 300, Picture2.hdc, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, vbSrcCopy
Picture1(Index).Refresh
End If
End Sub
Private Sub Picture1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 And Index = 1 And Command1.Tag = 1 Then SetBitmapBits Picture1(1).Image, UBound(bit2), bit2(1)
End Sub
Private Sub Command1_Click()
getbit Picture1(0).Image, bit1()
getbit Picture1(1).Image, bit2()
ReDim bit3(1 To UBound(bit2))
CopyMemory bit3(1), bit2(1), UBound(bit2)
For i = 1 To UBound(bit1) Step 4
If Abs(CInt(bit1(i)) - CInt(bit2(i))) <= HScroll1.Value And Abs(CInt(bit1(i + 1)) - CInt(bit2(i + 1))) <= HScroll1.Value And Abs(CInt(bit1(i + 2)) - CInt(bit2(i + 2))) <= HScroll1.Value Then
bit3(i) = 0.2 * bit2(i) 'b
bit3(i + 1) = 0.2 * bit2(i + 1) 'g
bit3(i + 2) = 0.2 * bit2(i + 2) 'r
n = n + 1
End If
Next
r = Round(400 * n / UBound(bit1), 2)
Me.Caption = "相似度:" & r & "%" & IIf(r = 100, "(一樣)", "(不一樣)")
SetBitmapBits Picture1(1).Image, UBound(bit3), bit3(1)
Command1.Tag = 1
End Sub
Function getbit(ByVal hbmp As Long, ByRef bit() As Byte)
Dim picinfo As BITMAP
GetObject hbmp, Len(picinfo), picinfo
ReDim bit(1 To picinfo.bmHeight * picinfo.bmWidth * 4)
GetBitmapBits hbmp, UBound(bit), bit(1)
End Function
Function openfile() As String
Dim ofn As OPENFILENAME
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "PICTURE FILE ONLY" & Chr(0) & "*.jpg;*.bmp;*.gif" & Chr(0)
ofn.lpstrFile = Space(254)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
ofn.lpstrTitle = "打開圖片"
ofn.flags = 6148
If GetOpenFileName(ofn) Then openfile = ofn.lpstrFile
End Function