文章目录
1. 框架
2. opticalFlow_label
3. 光流
1. 框架
2. opticalFlow_label
close all ; clear; clc;
% 使用光流进行标签的生成
% % 视频帧的读取
npy_data = readNPY( 'train.npy' ) ;
% % 提取标签的坐标
first_label = squeeze( npy_data( 2 , 1 , : , : ) ) ;
h = fspecial( "gaussian" , [ 3 , 3 ] , 1 ) ;
first_label_g = imfilter( first_label, h, 'replicate' ) ; % 'conv'
first_label_c = edge( first_label_g, "canny" ) ;
% imshow( uint8( first_label_c* 255 ) ) ;
first_label_double = im2double( first_label_c) ;
first_label_bw = im2bw( first_label_double, 0.5 ) ;
% imshow( uint8( first_label_bw * 255 ) ) ;
[ h, w] = size( first_label) ;
xPos = [ ] ;
yPos = [ ] ;
step = 0 ;
for i = 1 : h
for j = 1 : w
if first_label_bw( i, j) == 1
% xPos = [ xPos, i] ; % 保存为行
% yPos = [ yPos, j] ;
step = step + 1 ;
if mod( step, 1 ) == 0
xPos = [ xPos; j] ; % 保存为列
yPos = [ yPos; i] ;
end
end
end
end
% % 逐帧处理
first_frame = squeeze( npy_data( 1 , 1 , : , : ) ) ;
first_frame = uint8( first_frame) ;
% imshow( first_frame) ;
[ c, frame_num, img_h, img_w] = size( npy_data) ;
num = 0 ;
save_npy( 1 , 1 , : , : ) = first_frame;
save_npy( 2 , 1 , : , : ) = first_frame; % 预留一个通道,用于保存标签
for i = 2 % 2 : frame_num
num = num + 1 ;
currFrame = squeeze( npy_data( 1 , i, : , : ) ) ;
currFrame = uint8( currFrame) ;
pyramidLayer = 4 ;
kernelSize = 3 ;
sigma = 1.5 ;
iterNumMax = 5 ;
ww = 13 ;
[ u, v] = affineLKopticalFlow( first_frame, currFrame, xPos, yPos, pyramidLayer, kernelSize, sigma, iterNumMax, ww) ;
% 显示
newFrame = repmat( currFrame, [ 1 , 1 , 3 ] ) ;
new_xPos = xPos + u;
new_yPos = yPos + v;
newFrame1 = zeros( size( currFrame) ) ;
for kk = 1 : length( xPos)
% 显示
newFrame( int16( new_yPos( kk) ) , int16( new_xPos( kk) ) , 1 ) = 255 ;
newFrame( : , : , 2 ) = currFrame;
newFrame( int16( new_yPos( kk) ) , int16( new_xPos( kk) ) , 3 ) = 0 ;
newFrame1( int16( new_yPos( kk) ) , int16( new_xPos( kk) ) ) = 1 ;
end
save_npy( 1 , i, : , : ) = currFrame;
save_npy( 2 , i, : , : ) = first_frame;
% writeNPY( save_npy, "test.npy" ) ;
figure( 1 )
imshow( newFrame)
title( "Pre" )
% 形态学处理 离散点的连接
se = strel( 'disk' , 51 ) ; % 加大半径 可以进行填充
fc = imclose( newFrame1, se) ;
% imwrite( fc, "fc.jpg" )
% bw = im2bw( fc) ;
% fill_img = imfill( bw, "holes" ) ; % 封闭区域
figure( 2 )
imshow( fc)
title( "fc" )
% 将mask显示在图片上
mask_frame = repmat( currFrame, [ 1 , 1 , 3 ] ) ;
[ mask_h, mask_w, channel] = size( mask_frame) ;
for i = 1 : mask_h
for j = 1 : mask_w
if fc( i, j) == 1
mask_frame( i, j, 1 ) = 255 ;
mask_frame