① 求 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