Python 如何通过双线性插值生成新的采样点?

Python 如何通过双线性插值生成新的采样点?,python,opencv,Python,Opencv,我有一张灰色的图片,我需要通过双线性插值生成新的采样点(图片中红色圆圈)。python中是否有计算这些点的值的公式或函数 根据图表,它看起来不像双线性插值,它看起来像两个角度之间的平均值 我不确定我的解决方案是否是您所寻找的,但我认为它会给您一个线索 我尝试通过使用cv2.HoughCircles自动找到圆来解决它,并使用三角法标记x,y位置 解决方案使用以下阶段: 将图像转换为灰色和二进制 使用cv2查找圆。HoughCircles 迭代圆,并找到圆心最接近图像中心的圆 以45度为步长计算角

我有一张灰色的图片,我需要通过双线性插值生成新的采样点(图片中红色圆圈)。python中是否有计算这些点的值的公式或函数


根据图表,它看起来不像双线性插值,它看起来像两个角度之间的平均值

我不确定我的解决方案是否是您所寻找的,但我认为它会给您一个线索

我尝试通过使用
cv2.HoughCircles
自动找到圆来解决它,并使用三角法标记
x
y
位置

解决方案使用以下阶段:

  • 将图像转换为灰色和二进制
  • 使用cv2查找圆。HoughCircles
  • 迭代圆,并找到圆心最接近图像中心的圆
  • 以45度为步长计算角度
    [0,45,90,135…]
    ,命名为
    alpha

    计算上述角度之间的角度
    [22.5,67.5,112.5…]
    ,将它们命名为
    beta

    我们实际上不需要
    alpha
    来计算
    beta

    这只是为了演示你想做的插值
  • 使用三角法计算每个点的
    x
    y

    用青色圆圈标记“alpha”点。
    用黄色圆圈标记“beta”点。
    您可以存储
    x
    y
    的“beta”点-这些是您正在寻找的点

代码如下:

import cv2
import numpy as np

# Read input imgae
img = cv2.imread('image.png')

# Convert to Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Convert to binary image, and invert polarity
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

rows, cols = thresh.shape

circles = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, 1, minDist=rows//8, param1=50, param2=60, minRadius=rows//8, maxRadius=rows//2)

# Find the circle with center closest to the center of the image
min_dist_from_center = 1e9
min_c = []
for c in circles[0,:]:
    # Euclidean distance from the center of circle to center of image
    dist_from_center = np.linalg.norm([c[0] - cols/2, c[1] - rows/2])
    if dist_from_center < min_dist_from_center:
        min_dist_from_center = dist_from_center
        min_c = c

c = min_c

# Draw circle for testing
cv2.circle(img, (c[0], c[1]), c[2], (0, 255, 0), 2)

# Array of angles in 45 degrees difference
alpha_arr = np.arange(0, 360+45, 45)  # [  0,  45,  90, 135, 180, 225, 270, 315, 360]
betta_arr = (alpha_arr[1:] + alpha_arr[0:-1])/2  # [ 22.5,  67.5, 112.5, 157.5, 202.5, 247.5, 292.5, 337.5] Points between alpha

# Compute x, y coordinates by angle and radius
r = c[2]
for alpha, beta in zip(alpha_arr[:-1], betta_arr):
    x = r*np.cos(np.deg2rad(alpha)) + c[0] # x = r*cos(alpha) + center_x
    y = r*np.sin(np.deg2rad(alpha)) + c[1] # y = r*sin(alpha) + center_y

    # Draw small cyan circle to mark alpha points
    cv2.circle(img, (int(x), int(y)), 12, (255, 255, 0), 3)

    x = r*np.cos(np.deg2rad(beta)) + c[0] # x = r*cos(alpha) + center_x
    y = r*np.sin(np.deg2rad(beta)) + c[1] # y = r*sin(alpha) + center_y

    # Draw small yellow circle to mark beta points
    cv2.circle(img, (int(x), int(y)), 10, (0, 255, 255), 3)


# Show images for testing
cv2.imshow('thresh', thresh)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
导入cv2
将numpy作为np导入
#读输入imgae
img=cv2.imread('image.png')
#转换为灰度
灰色=cv2.CVT颜色(img,cv2.COLOR\U BGR2GRAY)
#转换为二进制图像,并反转极性
_,thresh=cv2.阈值(灰色,0,255,cv2.thresh\u二进制\u INV+cv2.thresh\u大津)
行,列=thresh.shape
圆=cv2。霍夫圆(阈值,cv2.HOUGH_梯度,1,minDist=行//8,参数1=50,参数2=60,最小半径=行//8,最大半径=行//2)
#找到圆心最接近图像中心的圆
距离中心的最小距离=1e9
min_c=[]
对于圆[0,:]中的c:
#圆心到图像中心的欧氏距离
距离中心的距离=np.linalg.norm([c[0]-cols/2,c[1]-rows/2])
如果距离中心的距离<距离中心的最小距离:
距离中心的最小距离=距离中心的距离
最小值c=c
c=最小值
#画圆圈进行测试
cv2.圆(img,(c[0],c[1]),c[2],(0255,0),2)
#45度差的角度阵列
alpha_arr=np.arange(0,360+45,45)#[0,45,90,135,180,225,270,315,360]
betta_arr=(alpha_arr[1::+alpha_arr[0:-1])/2#[22.5,67.5,112.5,157.5,202.5,247.5,292.5,337.5]点之间
#通过角度和半径计算x、y坐标
r=c[2]
对于alpha,zip中的beta(alpha_-arr[:-1],betta_-arr):
x=r*np.cos(np.deg2rad(alpha))+c[0]#x=r*cos(alpha)+中心
y=r*np.sin(np.deg2rad(alpha))+c[1]#y=r*sin(alpha)+中心
#绘制小青色圆圈以标记alpha点
圆(img,(int(x),int(y)),12,(255,255,0),3)
x=r*np.cos(np.deg2rad(beta))+c[0]#x=r*cos(alpha)+中心
y=r*np.sin(np.deg2rad(beta))+c[1]#y=r*sin(alpha)+中心
#画一个黄色的小圆圈来标记测试点
圆(img,(int(x),int(y)),10,(0,255,255),3)
#显示用于测试的图像
cv2.imshow('thresh',thresh)
cv2.imshow(“img”,img)
cv2.等待键(0)
cv2.destroyAllWindows()

结果:

您是否有一个简单的示例可以开始?你尝试过什么?事实上,我正在寻找一个获得sarted的想法