Clear Filters
Clear Filters

Replacing elements in a matrix in an if-else statement?

5 views (last 30 days)
I have an assignment where I am supposed to make a function that will take a photo of myself, with a sharply contrasting background, pick out the image of me, and place it into another image (mine is of Pluto) like a fake tourist photo. This is my code so far:
stub=pluto(151:end,52:111,:);
for i=1:numel(stub)
if me(i) >= 229;
stub(i)=stub(i);
else
stub(i)=me(i);
end
end
The "stub" is a piece of the Pluto image that is the same size as my photo, so I was attempting to pick out the threshold of my photo with the if statement and replace its values with my photo. The background of my photo is a bright green, hence the 229 value. I have also tried this code with a for loop through every row, column, and 3rd dimension, and tried to pick out only the value in the green layer, but for that attempt and the code above I keep getting strange results that are not what I need. Which is the better approach, and what is it that I'm doing wrong?
Thanks!
  2 Comments
Chad Greene
Chad Greene on 2 Mar 2016
This sounds like a fun project. I have a feeling the loop and the if statement are both unnecessary, but just to check, I need to ask,
1. It looks like your Pluto image is RGB because it has a third dimension. Is that true?
2. Is the photo of you RGB or grayscale?
3. Assuming the photo of you is RGB, what are the RGB values of the background?
Janell Lopez
Janell Lopez on 3 Mar 2016
Both are RGB. It's a beginner's class, so I think the for loop is implied, but if there's another way I'm all ears! The value of the background is a bright green, with R=181, G=229, B=29.

Sign in to comment.

Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!