修改图像以获得清晰、无噪声的光流/图像稳定特征

2024-05-19 00:40:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试开发一个管道,用于在Python中使用opencv稳定流体实验的图像。Here is an example raw image(实际尺寸:1920x1460)

该管道应能稳定低频漂移和高频“抖动”,这在试验期间阀门打开/关闭时偶尔发生。我目前的方法following the example here是应用一个双边滤波器,然后进行自适应阈值处理,以显示图像中的通道。然后,我使用goodFeaturesToTrack在阈值图像中查找角点。然而,由于对比度低,图像中存在大量的噪声,图像的角部会产生一些光学效应。虽然我可以找到通道的角点as shown here,但它们在一帧一帧地移动seen here。我跟踪了每一帧相对于calcOpticalFlowPyrLK计算的第一帧的x和y像素偏移量,并使用estimategidtransform shown here计算了一个刚性变换。在这幅图中,我可以看到0:200帧的低频漂移,以及225帧附近的剧烈跳跃。这些跳跃与视频中观察到的相符。然而,大量的噪声(振幅约为5-10像素)与视频中观察到的不匹配。如果我将这些变换应用到我的图像堆栈中,反而会得到增加的抖动,这不会稳定图像。此外,如果我尝试计算一帧到下一帧(而不是所有帧到第一帧)的变换,在处理少数帧之后,我将得到刚性变换矩阵的None返回,可能是因为噪声阻止了刚性变换的计算。你知道吗

下面是我如何计算转换的示例:

# Load required libraries
import numpy as np
from skimage.external import tifffile as tif
import os
import cv2
import matplotlib.pyplot as plt
from sklearn.externals._pilutil import bytescale

#Read in file and convert to 8-bit so it can be processed
os.chdir(r"C:\Path\to\my\processingfolder\inputstack")
inputfilename = "mytestfile.tif"
input_image = tif.imread(inputfilename)
input_image_8 = bytescale(input_image)
n_frames, vid_height, vid_width = np.shape(input_image_8)


transforms = np.zeros((n_frames-1,3),np.float32)
prev_image = starting_image
prev_f = cv2.bilateralFilter(prev_image,9,75,75)
prev_t = cv2.adaptiveThreshold(prev_f,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,49,2)
prev_pts = cv2.goodFeaturesToTrack(prev_t,maxCorners=100,qualityLevel=0.5,minDistance=10,blockSize=25,mask=None)

for i in range(1,n_frames-2):

    curr_image = input_image_8[i]
    curr_f = cv2.bilateralFilter(curr_image,9,75,75)
    curr_t = cv2.adaptiveThreshold(curr_f,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,49,2)

    #Detect features through optical flow:
    curr_pts, status, err = cv2.calcOpticalFlowPyrLK(prev_t,curr_t,prev_pts,None)

    #Sanity check 
    assert len(prev_pts) == len(curr_pts)
    #Filter to only the valid points
    idx = np.where(status==1)[0]
    prev_pts = prev_pts[idx]
    curr_pts = curr_pts[idx]

    #Find transformation matrix
    m = cv2.estimateRigidTransform(prev_pts,curr_pts, fullAffine=False) #will only work with OpenCV-3 or less

    # Extract translation
    dx = m[0,2]
    dy = m[1,2]

    # Extract rotation angle
    da = np.arctan2(m[1,0], m[0,0])

    # Store transformation
    transforms[i] = [dx,dy,da]

    print("Frame: " + str(i) +  "/" + str(n_frames) + " -  Tracked points : " + str(len(prev_pts)))

我怎样才能处理我的图像不同,使我挑选出这些渠道的线条,而没有噪音检测角落?这种稳定/校准不需要动态进行,它可以在事后应用于整个堆栈。你知道吗


Tags: 图像imageimportinputframeshereasnp

热门问题