知道线的中心点和斜率

时间:2016-08-12 07:18:00

标签: matlab coordinates

请帮助我找到附加图像中标有绿点的点的坐标。线的斜率是已知的,并且中心的坐标对于图像是已知的。我想在MATLAB中编写代码。请给我相同的想法。
图像由坐标已知的中心点组成,绿点坐标要知道通过中心点的直线的斜率。

2 个答案:

答案 0 :(得分:1)

如果已知中心点,我认为没有必要进行图像处理。 你需要的是一个线性方程。

y = tan(slope) * x

然后您只需找到x1x2,因为照片中也知道y1y2

答案 1 :(得分:0)

我创建了通过中心坐标的坐标向量,并具有所需的斜率 我用过极坐标来创建X,Y坐标向量 找到坐标后,我搜索了曲线上的绿点 我的解决方案并不是那么容易理解(不是最优雅的)......

这是我的代码:

%Input image (for testing).
I = imread('cameraman.tif');
I = cat(3, I, I, I); %Make I it "color" image where (R = G = B).

center = [100, 100];
slope = 1.5;

%Mark the center point with red (for debugging).
I(center(1)-1:center(1)+1, center(2)-1:center(2)+1, 1) = 255;

%Put green dots (for debugging).
x0 = 123;y0 = 65;
x1 = 12;y1 = 232;
I(y0-1:y0+1, x0-1:x0+1, 2) = 255;I(y0, x0, 1) = 0;I(y0, x0, 3) = 0;
I(y1-1:y1+1, x1-1:x1+1, 2) = 255;I(y1, x1, 1) = 0;I(y1, x1, 3) = 0;

%    #
% 1.5#   #
%    #  #
%    # #
%    ## alpha
%    ############
%        1

alpha = -atan2(slope, 1);

%Create vector of "radius" (distance from the center).
r = -norm(size(I))/2:0.2:norm(size(I))/2;

%Each (x,y) coordinate is on the line passing through center point
x = r*cos(alpha) + center(2);
y = r*sin(alpha) + center(1);

%Remove x and y outsize image boundaries from x, y arrays.
X = x;Y = y;
X((x < 1) | (x > size(I,2)) | (y < 1) | (y > size(I,1))) = [];
Y((x < 1) | (x > size(I,2)) | (y < 1) | (y > size(I,1))) = [];

%Round X and Y (to be legal pixel coordinates). 
X = round(X);
Y = round(Y);

R = zeros(size(X)) + 1; %Coordinate of 1'rd plane (red channel).
G = zeros(size(X)) + 2; %Coordinate of 2'rd plane (green channel).
B = zeros(size(X)) + 3; %Coordinate of 3'rd plane (blue channel).

%V gets values of pixels channel pixels in the curve.
rV = I(sub2ind(size(I), Y, X, R)); %Red channel values.
gV = I(sub2ind(size(I), Y, X, G)); %Green channel values.
bV = I(sub2ind(size(I), Y, X, B)); %Blue channel values.

%Find green dots where r, g, b = 255.
V = (rV == 0) & (gV == 255) & (bV == 0);

%Mark X,Y coordinates with blue color (for debugging).
I(sub2ind(size(I), Y, X, B)) = 255;

figure;imshow(I)

v0 = find(V, 1, 'last');
v1 = find(V, 1);
greenDot0 = [Y(v0), X(v0)]
greenDot1 = [Y(v1), X(v1)]

结果图像(用于测试):
enter image description here