Please help me in finding the coordinates of the point marked with green dots in the attached image. The slope of the line is known and the coordinates of the center are known for an image. I want to write a code in MATLAB. Please give me ideas for the same.
The image consists of the center points whose coordinates are known, and green dots coordinates are to be determined knowing the slope of the line passing through the center point.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I created vector of coordinates that passes center coordinate, and have the desired slope.
I used went through polar coordinates to create X, Y coordinates vectors.
After finding the coordinates, I searched the green dots on the curve.
My solution is not so simple for understanding (not most elegant)...
Here is my code:
%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)]
Result image (used for testing):
回答2:
If the center point is known, I assume, there is no need to do image processing. What you need is a linear equation.
y = tan(slope) * x
And then you just simply find x1
and x2
, because y1
and y2
are also known from the photo.