-
Notifications
You must be signed in to change notification settings - Fork 0
/
align.go
366 lines (310 loc) · 10.4 KB
/
align.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"fmt"
"github.com/spf13/cobra"
"gocv.io/x/gocv"
"math"
"sort"
)
const (
PrefixForAlignedFile = "align_"
MaxPairToMatch = 3
threshold = 1.29107 // 根据论文描述的阈值
)
var alignCmd = &cobra.Command{
Use: "align",
Short: "align",
Long: `usage description::TODO::`,
Run: alignRun,
}
func init() {
flags := alignCmd.Flags()
flags.StringVarP(¶m.rawAFile, "source",
"a", "A.mp4", "golf -a A.mp4")
flags.StringVarP(¶m.rawBFile, "dest",
"b", "B.mp4", "golf -b B.mp4")
flags.IntVarP(¶m.alignGap, "gap", "g", 60, "-g gap about similarity center")
}
func saveAlign(idx int, match [2]int, videoA, videoB *gocv.VideoCapture) {
idxA, idxB := match[0], match[1]
if idxB < 0 || idxA < 0 {
panic("find time start frame failed")
}
fmt.Println("time align =>", idx, idxA, idxB)
var startA, startB = 0, 0
if idxA > idxB {
startA = idxA - idxB
startB = 0
} else {
startB = idxB - idxA
startA = 0
}
fmt.Println("time align =>", idx, startA, startB)
saveVideoFromFrame(videoA, startA, fmt.Sprintf("q_%d_", idx)+PrefixForAlignedFile+param.rawAFile)
saveVideoFromFrame(videoB, startB, fmt.Sprintf("q_%d_", idx)+PrefixForAlignedFile+param.rawBFile)
saveVideoFromFrame(videoA, idxA, fmt.Sprintf("s_q_%d_", idx)+PrefixForAlignedFile+param.rawAFile)
saveVideoFromFrame(videoB, idxB, fmt.Sprintf("s_q_%d_", idx)+PrefixForAlignedFile+param.rawBFile)
}
func alignRun2(_ *cobra.Command, _ []string) {
videoA, videoB, err := readFile(param.rawAFile, param.rawBFile)
if err != nil {
panic(fmt.Errorf("failed tor read video file %s", param.rawAFile))
}
defer videoA.Close()
defer videoB.Close()
aHisGram, _ := parseHistogram(videoA)
bHisGram, _ := parseHistogram(videoB)
// 应用阈值处理
aHisGramFloat := distributeGradientMagnitude(aHisGram, threshold)
bHisGramFloat := distributeGradientMagnitude(bHisGram, threshold)
//idxA, idxB := findTimeStartOfFrame(aHisGramFloat, bHisGramFloat)
aligns := findTopThreeMatches(aHisGramFloat, bHisGramFloat)
for i, align := range aligns {
fmt.Println("queue:", i)
saveAlign(i, align, videoA, videoB)
}
//aHisGramFloat = aHisGramFloat[startA:]
//bHisGramFloat = bHisGramFloat[startB:]
}
func alignRun(_ *cobra.Command, _ []string) {
videoA, videoB, err := readFile(param.rawAFile, param.rawBFile)
if err != nil {
panic(fmt.Errorf("failed tor read video file %s", param.rawAFile))
}
defer videoA.Close()
defer videoB.Close()
aHisGram, _ := parseHistogram2(videoA)
bHisGram, _ := parseHistogram2(videoB)
ncc := nccOfAllFrame(aHisGram, bHisGram)
startA, startB, _ := findMaxNCCSequence(ncc, testTool.window)
saveVideoFromFrame(videoA, startA, "align_"+param.rawAFile)
saveVideoFromFrame(videoB, startB, "align_"+param.rawBFile)
}
type Match struct {
IndexA int // 视频A中帧的索引
IndexB int // 视频B中帧的索引
Score float64 // 相似度分数
}
func isFrameAlreadySelected(matches [3][2]int, match Match) bool {
for _, m := range matches {
if m[0] == match.IndexA || m[1] == match.IndexB {
return true
}
}
return false
}
func findTopThreeMatches(aHisGramFloat, bHisGramFloat [][]float64) (matches [3][2]int) {
var allMatches []Match // Match 是一个结构体,包含两个视频中帧的索引和它们之间的相似度分数
for i, histA := range aHisGramFloat {
for j, histB := range bHisGramFloat {
score := calculateNCC(histA, histB) // 计算帧对 (i, j) 之间的相似度分数
allMatches = append(allMatches, Match{i, j, score})
}
}
// 根据相似度分数对所有匹配进行排序,这里假设有一个自定义的比较函数
sort.Slice(allMatches, func(i, j int) bool {
return allMatches[i].Score > allMatches[j].Score
})
// 选取前三个最匹配的帧对,同时确保每个视频中的每帧只被选取一次
selected := 0
for _, match := range allMatches {
if selected == MaxPairToMatch {
break
}
if !isFrameAlreadySelected(matches, match) {
matches[selected] = [2]int{match.IndexA, match.IndexB}
selected++
}
}
return matches
}
func nccOfAllFrame(aHisGramFloat, bHisGramFloat [][]float64) [][]float64 {
videoALength := len(aHisGramFloat) // Video A frame count
videoBLength := len(bHisGramFloat) // Video B frame count
// Initialize a 2D array to store the NCC values
nccValues := make([][]float64, videoALength)
for i := range nccValues {
nccValues[i] = make([]float64, videoBLength)
}
// Iterate over all frame pairs of Video A and Video B, calculate their NCC values
for i, histogramA := range aHisGramFloat {
for j, histogramB := range bHisGramFloat {
nccValues[i][j] = calculateNCC(histogramA, histogramB)
}
}
return nccValues // These are the indices of the frames that best align in time
}
func findMaxNCCSequence(nccValues [][]float64, sequenceLength int) (int, int, [][]float64) {
maxSum := -1.0 // 假设NCC值范围是-1到1,开始时设置为最小可能的和
maxI, maxJ := -1, -1 // 用于存储最大和对应的起始帧索引
newRow := len(nccValues) - sequenceLength
newCol := len(nccValues[0]) - sequenceLength
fmt.Println("new row and col:", newRow, newCol)
weightedSum := make([][]float64, newRow+1)
for i := 0; i <= newRow; i++ {
weightedSum[i] = make([]float64, newCol+1)
for j := 0; j <= newCol; j++ {
sum := 0.0
for k := 0; k < sequenceLength; k++ {
sum += nccValues[i+k][j+k] // 计算连续sequenceLength帧的NCC值之和
}
weightedSum[i][j] = sum
if sum > maxSum {
maxSum = sum
maxI, maxJ = i, j
}
}
}
return maxI, maxJ, weightedSum // 返回连续sequenceLength帧NCC值之和最大的起始帧索引
}
func computeFrameVector(quantizedGradients [][][]float64) []float64 {
frameVector := make([]float64, 10) // 一个帧的10维向量 q_t^A
// 遍历每个像素的量化梯度向量
for _, row := range quantizedGradients {
for _, pixelVector := range row {
for i, value := range pixelVector {
//fmt.Println(x, y, value, frameVector[i])
frameVector[i] += value // 对每一维度进行累加
}
}
}
// 归一化 frameVector
//norm := norm2Float(frameVector)
//if norm > 0 {
// for i := range frameVector {
// frameVector[i] /= norm // 对每一维度的值进行归一化
// }
//}
return frameVector
}
func parseHistogram2(video *gocv.VideoCapture) ([][]float64, error) {
// 初始化前一帧变量
var prevFrame gocv.Mat
firstFrame := true
var idx = 0
histograms := make([][]float64, 0)
for {
var frame = gocv.NewMat()
if ok := video.Read(&frame); !ok || frame.Empty() {
fmt.Println("[parseHistogram] read frame from video finished", idx)
frame.Close()
break
}
idx++
// Convert to grayscale
var grayFrame = gocv.NewMat()
gocv.CvtColor(frame, &grayFrame, gocv.ColorRGBToGray)
frame.Close()
if firstFrame {
fmt.Println("[parseHistogram] read first frame from video", idx)
firstFrame = false
prevFrame = grayFrame.Clone()
continue
}
// Calculate spatial gradients
gradX := gocv.NewMat()
gradY := gocv.NewMat()
gradT := gocv.NewMat()
gocv.Sobel(grayFrame, &gradX, gocv.MatTypeCV16S, 1, 0, 3, 1, 0, gocv.BorderDefault)
gocv.Sobel(grayFrame, &gradY, gocv.MatTypeCV16S, 0, 1, 3, 1, 0, gocv.BorderDefault)
gocv.AbsDiff(grayFrame, prevFrame, &gradT)
prevFrame.Close()
// Make the current frame the new previous frame for the next iteration
prevFrame = grayFrame.Clone()
// Quantize gradients into a histogram using an icosahedron
histogram := quantizeGradients2(&gradX, &gradY, &gradT)
sumHistogram := computeFrameVector(histogram)
fmt.Println("[parseHistogram] parse histogram for frame:", idx, sumHistogram)
histograms = append(histograms, sumHistogram) // 将当前帧的直方图添加到数组中
gradX.Close()
gradY.Close()
gradT.Close()
grayFrame.Close()
}
// Release the last previous frame
if !prevFrame.Empty() {
prevFrame.Close()
}
return histograms, nil
}
func parseHistogram(video *gocv.VideoCapture) ([][]int, error) {
// 初始化前一帧变量
var prevFrame gocv.Mat
firstFrame := true
var histograms [][]int // 用于存储每一帧的直方图
var idx = 0
for {
var frame = gocv.NewMat()
if ok := video.Read(&frame); !ok || frame.Empty() {
fmt.Println("[parseHistogram] read frame from video finished", idx)
frame.Close()
break
}
idx++
// Convert to grayscale
var grayFrame = gocv.NewMat()
gocv.CvtColor(frame, &grayFrame, gocv.ColorRGBToGray)
frame.Close()
if firstFrame {
fmt.Println("[parseHistogram] read first frame from video", idx)
firstFrame = false
prevFrame = grayFrame.Clone()
continue
}
// Calculate spatial gradients
gradX := gocv.NewMat()
gradY := gocv.NewMat()
gradT := gocv.NewMat()
gocv.Sobel(grayFrame, &gradX, gocv.MatTypeCV16S, 1, 0, 3, 1, 0, gocv.BorderDefault)
gocv.Sobel(grayFrame, &gradY, gocv.MatTypeCV16S, 0, 1, 3, 1, 0, gocv.BorderDefault)
gocv.AbsDiff(grayFrame, prevFrame, &gradT)
prevFrame.Close()
// Make the current frame the new previous frame for the next iteration
prevFrame = grayFrame.Clone()
// Quantize gradients into a histogram using an icosahedron
histogram := quantizeGradients(&gradX, &gradY, &gradT)
fmt.Println("[parseHistogram] parse histogram for frame:", idx)
histograms = append(histograms, histogram) // 将当前帧的直方图添加到数组中
gradX.Close()
gradY.Close()
gradT.Close()
grayFrame.Close()
}
// Release the last previous frame
if !prevFrame.Empty() {
prevFrame.Close()
}
return histograms, nil // 返回包含每一帧直方图的数组
}
// 分配梯度幅度和应用阈值
func distributeGradientMagnitude(hists [][]int, threshold float64) [][]float64 {
processedHists := make([][]float64, len(hists))
fmt.Println("apply threshold to q^")
for i, hist := range hists {
// 计算原始直方图的范数 (gNorm)。
gNorm := norm2(hist)
// 应用阈值处理。
qPrime := make([]float64, len(hist))
sumSq := 0.0 // 这将用于存储 qPrime 的平方和。
for j, value := range hist {
newValue := float64(value) - threshold
if newValue < 0 {
newValue = 0
}
qPrime[j] = newValue
sumSq += newValue * newValue
}
// 计算 qPrime 的范数。
qPrimeNorm := math.Sqrt(sumSq)
// 计算 q,使用 gNorm 乘以 qPrime 的每个元素。
processedHists[i] = make([]float64, len(hist))
for j, qPrimeValue := range qPrime {
if qPrimeNorm == 0 {
processedHists[i][j] = 0
} else {
processedHists[i][j] = (gNorm * qPrimeValue) / qPrimeNorm
}
}
}
return processedHists
}