This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
cv_utils.py
61 lines (47 loc) · 1.67 KB
/
cv_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
copyright 2013-2014 Talin Salway
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import numpy
import cv
import cv2
def img_from_buffer(buffer):
np_arr = numpy.fromstring(buffer,'uint8')
np_mat = cv2.imdecode(np_arr,0)
return cv.fromarray(np_mat)
def show_scaled(win, img):
min, max, pt1, pt2 = cv.MinMaxLoc(img)
cols, rows = cv.GetSize(img)
tmp = cv.CreateMat(rows, cols,cv.CV_32FC1)
cv.Scale(img, tmp, 1.0/(max-min), 1.0*(-min)/(max-min))
cv.ShowImage(win,tmp)
def float_version(img):
tmp = cv.CreateImage( cv.GetSize(img), 32, 1)
cv.ConvertScale(img, tmp, 1/255.0)
return tmp
def sum_squared(img1, img2):
tmp = cv.CreateImage(cv.GetSize(img1), 8,1)
cv.Sub(img1,img2,tmp)
cv.Pow(tmp,tmp,2.0)
return cv.Sum(tmp)[0]
def ccoeff_normed(img1, img2):
size = cv.GetSize(img1)
tmp1 = float_version(img1)
tmp2 = float_version(img2)
cv.SubS(tmp1, cv.Avg(tmp1), tmp1)
cv.SubS(tmp2, cv.Avg(tmp2), tmp2)
norm1 = cv.CloneImage(tmp1)
norm2 = cv.CloneImage(tmp2)
cv.Pow(tmp1, norm1, 2.0)
cv.Pow(tmp2, norm2, 2.0)
#cv.Mul(tmp1, tmp2, tmp1)
return cv.DotProduct(tmp1, tmp2) / (cv.Sum(norm1)[0]*cv.Sum(norm2)[0])**0.5