You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Create a triangular image with height of 200 pixels and the base of 200 pixels and color the image in any two colors.
5 views (last 30 days)
Show older comments
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.
Answers (1)
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;
30 Comments
Image Analyst
on 13 Feb 2013
Edited: Image Analyst
on 13 Feb 2013
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.
Image Analyst
on 13 Feb 2013
Edited: Image Analyst
on 13 Feb 2013
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
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
Edited: Image Analyst
on 15 Feb 2013
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
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
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
Edited: Image Analyst
on 15 Feb 2013
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
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
on 15 Feb 2013
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
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
Edited: Image Analyst
on 15 Feb 2013
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
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.
Image Analyst
on 15 Feb 2013
Fine. You can do it in one image. Show me the image you'd like to get. Post your URL with your desired image.
Image Analyst
on 15 Feb 2013
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
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
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
on 15 Feb 2013
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.
Walter Roberson
on 15 Feb 2013
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
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
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
Edited: Image Analyst
on 16 Feb 2013
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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)