I have an image below. Its shape is 720x1280. I want to draw a line to fit this white pattern.
我有一张图片如下。它的形状是720x1280。我想绘制一条线以适应这种白色图案。
I used y range instead of x is because y is more easy to fit as 2nd order polynomial.
我使用y范围代替x是因为y更容易适合作为二阶多项式。
y_range = np.linspace(0, 719, num=720) # to cover same y-range as image
fit = np.polyfit(y_range, image, 2) # image.shape = (720, 1280)
print(fit.shape) # (3, 1280)
I expect fit.shape = (3,)
, but it's not.
我希望fit.shape =(3,),但事实并非如此。
- Can np.polyfit() be used in this situation?
- If 1. is true, how to do this? I want to use
fit
to calculate curve as following.
在这种情况下可以使用np.polyfit()吗?
如果1.是真的,怎么办?我想使用fit来计算曲线如下。
f = fit[0]*y_range**2 + fit[1]*y_range + fit[2]
Thank you.
1 个解决方案
#1
1
Your image
is 2-D, that is the problem. The 2-D image contains information about the coordinates of each point, so you only have to put it into a suitable format.
你的形象是2-D,这就是问题所在。二维图像包含有关每个点坐标的信息,因此您只需将其放入合适的格式即可。
Since it seems that you are interested only in the location of the white pixels (and not the particular value of each pixel), convert the image into binary values. I don't know particular values of your image
but you could do for example:
由于您似乎只对白色像素的位置感兴趣(而不是每个像素的特定值),因此将图像转换为二进制值。我不知道您图像的特定值,但您可以这样做:
import numpy as np
curoff_value = 0.1 # this is particular to your image
image[image > cutoff_value] = 1 # for white pixel
image[image <= cutoff_value] = 0 # for black pixel
Get the coordinates of the white pixels:
获取白色像素的坐标:
coordinates = np.where(image == 1)
y_range = coordinates[0]
x_range = coordinates[1]
fit = np.polyfit(y_range, x_range, 2)
print(fit.shape)
Returns (3, )
as you would expect.
如您所料,返回(3,)。
#1
1
Your image
is 2-D, that is the problem. The 2-D image contains information about the coordinates of each point, so you only have to put it into a suitable format.
你的形象是2-D,这就是问题所在。二维图像包含有关每个点坐标的信息,因此您只需将其放入合适的格式即可。
Since it seems that you are interested only in the location of the white pixels (and not the particular value of each pixel), convert the image into binary values. I don't know particular values of your image
but you could do for example:
由于您似乎只对白色像素的位置感兴趣(而不是每个像素的特定值),因此将图像转换为二进制值。我不知道您图像的特定值,但您可以这样做:
import numpy as np
curoff_value = 0.1 # this is particular to your image
image[image > cutoff_value] = 1 # for white pixel
image[image <= cutoff_value] = 0 # for black pixel
Get the coordinates of the white pixels:
获取白色像素的坐标:
coordinates = np.where(image == 1)
y_range = coordinates[0]
x_range = coordinates[1]
fit = np.polyfit(y_range, x_range, 2)
print(fit.shape)
Returns (3, )
as you would expect.
如您所料,返回(3,)。