To change values in a matrix between two elements

I have a matrix of zeros 768*1024, considering 4 points (50,100) (450,100) (200,600) and (300,600) i change the values between them and create two lines when i show the image.
i want to change all the points between these two lines to 1's so that i achieve what looks like a white trapezoid in the black background image. Also, this should overwite the previous 0's to 1's in the matrix
code is as follows:
M=zeros(768,1024);
p1=[50,100];
p2=[200,600];
x_value=[50, 200];
y_value=[100, 600];
X=[x_value(1):x_value(2)];
Y=round(interp1(x_value,y_value,X));
i_max=size(Y);
for i=1:i_max(2)
M(Y(i),X(i))=255;
end
p4=[300,600];
p3=[450,100];
a_value=[450,300];
b_value=[100,600];
A = [a_value(2):a_value(1)];
B = round(spline(a_value,b_value,A));
j_max=size(B);
for i=1:j_max(2)
M(B(i),A(i))=255;
end
imshow(M);

답변 (1개)

Shashwat Bajpai
Shashwat Bajpai 2019년 7월 16일
Hi,
I understand that you are trying to fill the trapezoid created by the points (50,100) (450,100) (200,600) and (300,600).
Please refer to the following code to see if this is what you desire:
xf=[X' A'];
num_el=numel(xf);
yf=[Y' B'];
for i=1:num_el/2
M(yf(i):yf(end-i),xf(i):xf(end-i))=255;
end
imshow(M)
The following link provides a good understanding of how to index into matrices and modify their values:

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2019년 7월 2일

답변:

2019년 7월 16일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by