600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > opencv python3 找图片不同_使用OpenCV和Python查找图片差异

opencv python3 找图片不同_使用OpenCV和Python查找图片差异

时间:2019-10-27 15:09:40

相关推荐

opencv python3 找图片不同_使用OpenCV和Python查找图片差异

使用OpenCV和Python查找图片差异

flyfish

方法1 均方误差的算法(Mean Squared Error , MSE)

下面的一些表达与《TensorFlow - 协方差矩阵》式子表达式一样的

平均数

M=x1+x2+⋯+xnnM

=

x

1

+

x

2

+

+

x

n

n

方差

s2=(x1−M)2+(x1−M)2+⋯+(xn−M)2ns

2

=

(

x

1

M

)

2

+

(

x

1

M

)

2

+

+

(

x

n

M

)

2

n

标准差(Standard Deviation) =均方差

σ=1n∑ni=1(xi−μ)2−−−−−−−−−−−−−√σ

=

1

n

i

=

1

n

(

x

i

μ

)

2

拟合 误差平方和( sum of squared errors)

residual sum of squares (RSS), also known as the sum of squared residuals (SSR) or the sum of squared errors of prediction (SSE),

also known as 就我们所说的

RSS, SSR ,SSE表达的是一个意思

RSS=∑i=1n(yi−f(xi))2R

S

S

=

i

=

1

n

(

y

i

f

(

x

i

)

)

2

RSS=∑i=1n(εi)2=∑i=1n(yi−(α+βxi))2R

S

S

=

i

=

1

n

(

ε

i

)

2

=

i

=

1

n

(

y

i

(

α

+

β

x

i

)

)

2

SSE=∑mi=1wi(yi−yi^)2S

S

E

=

i

=

1

m

w

i

(

y

i

y

i

^

)

2

yiy

i是真实数据,yi^y

i

^是拟合的数据,SSE越接近于0,说明模型越接近最优,WiW

i的取值越是最优

均方误差

MSE=SSEn=1n∑mi=1wi(yi−yi^)2M

S

E

=

S

S

E

n

=

1

n

i

=

1

m

w

i

(

y

i

y

i

^

)

2

def mse(imageA, imageB):

# the 'Mean Squared Error' between the two images is the

# sum of the squared difference between the two images;

# NOTE: the two images must have the same dimension

err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)

err /= float(imageA.shape[0] * imageA.shape[1])

# return the MSE, the lower the error, the more "similar"

# the two images are

return err

方法2 SSIM

​structural similarity index measurement (SSIM) system

一种衡量两幅图像结构相似度的新指标,其值越大越好,最大为1。

新建一个Python文件,命名为 image_diff.py

原理

根据参数读取两张图片并转换为灰度:

使用SSIM计算两个图像之间的差异,这种方法已经在scikit-image 库中实现

在两个图像之间的不同部分绘制矩形边界框。

代码如下 已编译通过

from skimage.measure import compare_ssim

#~ import skimage as ssim

import argparse

import imutils

import cv2

# construct the argument parse and parse the arguments

ap = argparse.ArgumentParser()

ap.add_argument("-f", "--first", required=True,

help="first input image")

ap.add_argument("-s", "--second", required=True,

help="second")

args = vars(ap.parse_args())

# load the two input images

imageA = cv2.imread(args["first"])

imageB = cv2.imread(args["second"])

'''

imageA = cv2.imread("E:\\1.png")

imageB = cv2.imread("E:\\2.png")

'''

# convert the images to grayscale

grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)

grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

# compute the Structural Similarity Index (SSIM) between the two

# images, ensuring that the difference image is returned

#​structural similarity index measurement (SSIM) system一种衡量两幅图像结构相似度的新指标,其值越大越好,最大为1。

(score, diff) = compare_ssim(grayA, grayB, full=True)

diff = (diff * 255).astype("uint8")

print("SSIM: {}".format(score))

# threshold the difference image, followed by finding contours to

# obtain the regions of the two input images that differ

thresh = cv2.threshold(diff, 0, 255,

cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,

cv2.CHAIN_APPROX_SIMPLE)

cnts = cnts[0] if imutils.is_cv2() else cnts[1]

# loop over the contours

for c in cnts:

# compute the bounding box of the contour and then draw the

# bounding box on both input images to represent where the two

# images differ

(x, y, w, h) = cv2.boundingRect(c)

cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)

cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2)

# show the output images

cv2.imshow("Original", imageA)

cv2.imshow("Modified", imageB)

cv2.imshow("Diff", diff)

cv2.imshow("Thresh", thresh)

cv2.waitKey(0)

使用方法 python image_diff.py –first original.png –second images/modified.png 如果不想使用参数将参数代码部分直接变成 imageA = cv2.imread(“E:\1.png”) imageB = cv2.imread(“E:\2.png”)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。