How to draw a triangle in an image in MATLAB?

I need to draw a triangle in an image that I have uploaded. The triangle should look like this:

1 0 0 0 0 0  
1 1 0 0 0 0  
1 1 1 0 0 0  
1 1 1 1 0 0  
1 1 1 1 1 0  
1 1 1 1 1 1  

      

But the main problem is that I don't know how I can create such a matrix. I want to multiply this matrix with an image, and the image matrix has 3 parameters (W, H, RGB).

+2


source to share


1 answer


You can create a matrix like in your question using TRIL and ONES functions:

>> A = tril(ones(6))

A =

     1     0     0     0     0     0
     1     1     0     0     0     0
     1     1     1     0     0     0
     1     1     1     1     0     0
     1     1     1     1     1     0
     1     1     1     1     1     1

      

EDIT: . Based on your comment below, it looks like you have a 3D matrix of RGB images B

and that you want to multiply each color plane B

by the matrix A

. This will give the net result of setting the top triangular portion of the image (corresponding to all zeros in A

) to black. Assuming it B

is a 6-by-6-by-3 matrix (i.e., the rows and columns from B

match to characters A

), here is one solution that uses indexing (and the REPMAT function ) instead of multiplication:



>> B = randi([0 255],[6 6 3],'uint8');  % A random uint8 matrix as an example
>> B(repmat(~A,[1 1 3])) = 0;           % Set upper triangular part to 0
>> B(:,:,1)                             % Take a peek at the first plane

ans =

    8    0    0    0    0    0
  143  251    0    0    0    0
  225   40  123    0    0    0
  171  219   30   74    0    0
   48  165  150  157  149    0
   94   96   57   67   27    5

      

The REPMAT call replicates the negative version A

3 times, so that it has the same dimensions as B

. The result is used as a logical index in B

setting non-zero indexes to 0. Using instead of multiplying the indexing, you can not worry about the conversion A

and B

the same data type (that would be required for multiplication in this case, as A

has the type double

and B

has a type uint8

).

+9


source







All Articles