- Home
- Search
- Contributors
- Recent Activity
- Flagged Content
- Log in to ask and answer questions.
- Ask a Question
- About MATLAB Answers

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Cesar
on 13 Feb 2013

Create a triangular image with height of 200 pixels and the base of 200 pixels and color the image in any two colors.

Answer by Image Analyst
on 13 Feb 2013

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

Cesar
on 13 Feb 2013

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.

Image Analyst
on 13 Feb 2013

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.

Cesar
on 13 Feb 2013

Link

**Direct link to this comment:**

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

You are right. I still need more hints.

Image Analyst
on 13 Feb 2013

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);

Cesar
on 14 Feb 2013

Link

**Direct link to this comment:**

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

I use the following vertices. triangleX = [0 100 200]; triangleY = [0 200 0]; rgbImage is showing as 2 triangles instead of a single one

Image Analyst
on 15 Feb 2013

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."

Cesar
on 15 Feb 2013

Link

**Direct link to this comment:**

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

I ran it and the triangle image is colored in 1 color...purple but not 2. The triangle must be colored in 2 colors.

Cesar
on 15 Feb 2013

Link

**Direct link to this comment:**

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

The goal is to create 1 triangle (height 200, base 200) with 2 colors, not 2 triangles with 2 different colors.

Image Analyst
on 15 Feb 2013

Link

**Direct link to this comment:**

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

That was not the original question. It didn't mention two triangles, only two colors, assumed to be triangle and background colors. But the modification is really obvious. Just run the lines of code again, with different values of triangleX and triangleY.

Cesar
on 15 Feb 2013

Link

**Direct link to this comment:**

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

I do not want 2 triangles. I need 1 triangle with 2 colors. If you run patch([0 100 200], [0 200 0], [1 0 0]) it shows 1 triangle with 1 color. I need that same size triangle with 2 colors.

Walter Roberson
### Walter Roberson (view profile)

on 15 Feb 2013

- 19 questions
- 15,551 answers
- 5,488 accepted answers
- Reputation: 29,258

Link

**Direct link to this comment:**

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

My reading of the question is one triangle in one color, plus the background in a different color, total two colors.

Cesar
on 15 Feb 2013

Link

**Direct link to this comment:**

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

Sorry. The triangle image must be in 2 colors. Each side of the triangle must be in 1 color....by example 1 side red and 1 side green.

Image Analyst
on 15 Feb 2013

Link

**Direct link to this comment:**

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

I agree with Walter. Though if you need one triangle with two colors, and the background being a third color, then that can really be two triangles, each with a different color, stuck together, right? Is that what you want? Imagine a triangle, then draw a line from any vertex to a size, bisecting the triangle into two triangles. Ok, so now **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.

Cesar
on 15 Feb 2013

Link

**Direct link to this comment:**

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

My question is simple. Half of the triangle in 1 color, the other half in another color. Since the base must be 200 pixels, 0 to 100 pixels in 1 color and 101 to 200 in a different color.

Cesar
on 15 Feb 2013

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?

Image Analyst
on 15 Feb 2013

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?

Cesar
on 15 Feb 2013

Link

**Direct link to this comment:**

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

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

Image Analyst
on 15 Feb 2013

Link

**Direct link to this comment:**

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

Fine. You can do it in one image. Show me the image you'd like to get. Post your URL with your desired image.

Cesar
on 15 Feb 2013

Link

**Direct link to this comment:**

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

Image Analyst
on 15 Feb 2013

Link

**Direct link to this comment:**

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

Yep. Looks like two tirangles to me. What's your opinion Walter? Could this be made by running my single triangle code twice with different vertex coordinates? Cesar, I can't ethically do your complete homework assignment for you. As it is, I practically did it for you. I don't want you to be caught plagiarizing. So exactly what are you asking of me? What code were you, or are you, expecting me to provide to you?

Cesar
on 15 Feb 2013

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.

Image Analyst
on 15 Feb 2013

Link

**Direct link to this comment:**

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

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

Walter Roberson
### Walter Roberson (view profile)

on 15 Feb 2013

- 19 questions
- 15,551 answers
- 5,488 accepted answers
- Reputation: 29,258

Link

**Direct link to this comment:**

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

Definitely looks like two triangles to me. Look at the shadows at the bottom: the one on the left is overlaying the one on the right, and one of the two is tilted with respect to the other.

Cesar
on 15 Feb 2013

Link

**Direct link to this comment:**

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

You didnt solve it after all.

Walter Roberson
### Walter Roberson (view profile)

on 15 Feb 2013

- 19 questions
- 15,551 answers
- 5,488 accepted answers
- Reputation: 29,258

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.

Cesar
on 16 Feb 2013

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
### Walter Roberson (view profile)

on 16 Feb 2013

- 19 questions
- 15,551 answers
- 5,488 accepted answers
- Reputation: 29,258

Link

**Direct link to this comment:**

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

If doing step (A) accomplishes half of a task, then doing step (A) *twice*, with different parameters each time, might accomplish the entire task.

Image Analyst
on 16 Feb 2013

Link

**Direct link to this comment:**

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

Well I think I will *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.

Cesar
on 18 Feb 2013

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

Cesar
on 18 Feb 2013

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.

- © 1994-2015 The MathWorks, Inc.
- Site Help
- Patents
- Trademarks
- Privacy Policy
- Preventing Piracy
- Terms of Use
- RSS
- Google+

- Featured MathWorks.com Topics:
- New Products
- Support
- Documentation
- Training
- Webinars
- Newsletters
- MATLAB Trials
- Careers

## 0 Comments