MATLAB Answers^{™}

- 6 questions
- 0 answers
- 0 accepted answers
- Reputation: 0

- 0 questions
- 15,943 answers
- 5,176 accepted answers
- Reputation: 26,074

**Direct link to this answer:**

http://www.mathworks.com/matlabcentral/answers/63312#answer_74918

Answer by Image Analyst
on 13 Feb 2013

- 0 questions
- 15,943 answers
- 5,176 accepted answers
- Reputation: 26,074

Hint: use poly2mask() and logical indexing.

yourImage = zeros(rows, columns, 'uint8'); % Initialize. % Assign values to triangle defined by "mask" image. yourImage(mask) = someValue;

Show 27 older comments

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129285

Thanks. Using patch() did create the triangle. But with only one color.

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129324

Well, did I *say* to use patch()? Looks like you need more hints. Create red, green, and blue channels

color1 = [100 150 200]; redChannel = color1(1) * ones(rows, columns, 'uint8'); % Display it imshow(redChannel); % Draw mask, for example, call roipolyold().

Then mask it like I said, then combine them all together into an rgb image:

rgbImage = cat(3, redChannel, greenChannel, blueChannel); imshow(rgbImage);

See if you can complete the script on your own.

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129351

You are right. I still need more hints.

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129356

Here is it practically done. Just 4 more lines and it's complete. You don't want me to do it completely for you do you? Would that be considered acceptable/ethical in your course?

color1 = uint8([100 150 200]); color2 = uint8([180 50 250]); rows = 480; columns = 640; redChannel = color1(1) * ones(rows, columns, 'uint8'); greenChannel = color1(2) * ones(rows, columns, 'uint8'); blueChannel = color1(3) * ones(rows, columns, 'uint8');

% Get/assign triangle vertices, triangleX, triangleY. % I'm sure you can figure out what goes here!

% Create mask. mask = poly2mask(triangleX, triangleY, rows, columns); % Display mask subplot(1,2,1); imshow(mask); % Assign values to triangle defined by "mask" image. redChannel(mask) = color2(1); % I'm sure you can figure out what goes here! rgbImage = cat(3, redChannel, greenChannel, blueChannel); subplot(1,2,2); imshow(rgbImage);

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129425

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129804

You got your 2 color triangle like you wanted. The left triangle is just the mask I made with poly2mask(). Here, I've put in some title's to explain that:

color1 = uint8([100 150 200]); color2 = uint8([180 50 250]); rows = 480; columns = 640; redChannel = color1(1) * ones(rows, columns, 'uint8'); greenChannel = color1(2) * ones(rows, columns, 'uint8'); blueChannel = color1(3) * ones(rows, columns, 'uint8');

% Get/assign triangle vertices, triangleX, triangleY. % I'm sure you can figure out what goes here! triangleX = [0 100 200]; triangleY = [0 200 0];

% Create mask. mask = poly2mask(triangleX, triangleY, rows, columns); % Display mask subplot(1,2,1); imshow(mask); title('This is the mask poly2mask made', 'FontSize', 25); % Assign values to triangle defined by "mask" image. redChannel(mask) = color2(1); greenChannel(mask) = color2(2); blueChannel(mask) = color2(3); rgbImage = cat(3, redChannel, greenChannel, blueChannel); subplot(1,2,2); imshow(rgbImage); title('This is your triangle', 'FontSize', 25); % Enlarge figure to full screen. set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Give a name to the title bar. set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

If you want, you can just get rid of the first subplot() and imshow(). This solves your problem, so go ahead and mark it "Accepted."

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129810

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129812

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129815

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129816

Walter Roberson
on 15 Feb 2013

- 22 questions
- 17,925 answers
- 6,330 accepted answers
- Reputation: 33,506

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129821

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129828

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129829

**we're back to two triangles of a single color each** and you can use my code. If that's still not what you want you'll have to upload a picture somewhere of exactly what you want (mock something up in Photoshop if you have to), because we're wasting a lot of time doing your homework for you and getting nowhere.

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129835

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129836

Each half of the triangle must be in a different color. Is that clear enough?

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129837

That's just two triangles stuck together. Can you tell me it isn't?

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129841

They must be shown as 1 triangle....1 image.

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129843

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129849

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129870

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129874

That's fine if you can not do it. You did not solve it by any means. I was just asking a question.

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129877

*I* cannot do it. It's way beyond my skill set. Perhaps I need to re-enroll in graduate school.

Walter Roberson
on 15 Feb 2013

- 22 questions
- 17,925 answers
- 6,330 accepted answers
- Reputation: 33,506

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129906

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129944

You didnt solve it after all.

Walter Roberson
on 15 Feb 2013

- 22 questions
- 17,925 answers
- 6,330 accepted answers
- Reputation: 33,506

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129954

Cesar, you need to look more carefully at the image you supplied. Zoom in on the area near the tip. You will notice a few things:

- There appear to be three tips. The left tip is white, the center tip is white, the right tip is dark.
- The dark tip is part of a dark line that is most easily explained as if the dark line is the shadow of the center tip with respect to a light source that is to the left of center of the image.
- If the left (white) tip and center (white) tip were at equal heights, then both would have shadows, but there is only one shadow
- The details of the edge crossings of the left and center tips strongly suggest the interpretation that the left triangle is on top of the right triangle, with the left triangle ending a small bit to the right of the right triangle.
- the bottom line of the triangles show that the triangles continue to be overlapped all the way down

Together these make clear that this is **not** one bi-colored triangle: this is **two** triangles with no vertices in common.

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129957

**I need 1 triangular image with height of 200 pixels and the base of 200 pixels colored with any two colors.** Please read the description. Do not focus on the picture I sent. You either can solve it or not.

Walter Roberson
on 16 Feb 2013

- 22 questions
- 17,925 answers
- 6,330 accepted answers
- Reputation: 33,506

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129959

*twice*, with different parameters each time, might accomplish the entire task.

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_129962

*not* solve it (supposedly I don't know how). I did way too much already, and it's such a trivial chore to finish it. Like Walter and I said, just do it twice. You said that half the triangle was one color and the other half was a different color. Now, ask yourself "What shape is half a triangle?" But ultimately it's your problem, not ours, and we *should not* do 100% of it for you, and I won't. There needs to be some part of it that *you* own, that *you write* yourself. You have all the tools, so, good luck with that.

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_130333

I did solve it. This is how it should look: https://www.dropbox.com/s/gsgcsb8rqwlg55z/done.doc

Link

**Direct link to this comment:**

http://www.mathworks.com/matlabcentral/answers/63312#comment_130340

Using fill(X,Y,C). The fill function creates colored polygons.

Opportunities for recent engineering grads.

## 0 Comments