Create a triangular image with height of 200 pixels and the base of 200 pixels and color the image in any two colors.
Show older comments
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.
Cesar
on 13 Feb 2013
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
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
Cesar
on 15 Feb 2013
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
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
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
Cesar
on 15 Feb 2013
Image Analyst
on 15 Feb 2013
That's just two triangles stuck together. Can you tell me it isn't?
Cesar
on 15 Feb 2013
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.
Cesar
on 15 Feb 2013
Edited: Walter Roberson
on 15 Feb 2013
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
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.
Cesar
on 15 Feb 2013
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
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.
Cesar
on 18 Feb 2013
Edited: Walter Roberson
on 18 Feb 2013
Cesar
on 18 Feb 2013
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!