0

Im fairly new to MatLab and am taking an elective course which required MatLab while doing my MBA (yes, totally different field). As part of the assignment, we have to use MatLab colormapping to convert the yellow object in the image below into green:

avocado_image

I've used the "imread" function and imported it into Matlab. I also have some experience with colormap with Matlab, but quite basic. In the hints provided, it is said that I should use the IMTOOL function to determine the range of r,g,b values for the yellow pixels and the background pixels. I'm supposed to design a color mapping algorithm that transforms the yellow object to green, but not touch any of the background at all.

In my previous exercise, we converted the 255 values to be just 1s and 0s by dividing by 255. We then created a new colormap and applied it which converted the colors. But I still have no idea how to bring this all together. I've tried some code I found from the Matlab help, but I'm not getting anywhere. My approach has been to import image into Matlab, read image matrix. For all pixels that are close to the pixels of yellow, I replace them with a generic green pixel. Is there a better way to do this? If not, how would I do this with simple MatLab code?

Also, I've checked the code in this function, and I'm 10000000% sure what they are asking me doesn't have to be so complicated: https://www.mathworks.com/matlabcentral/fileexchange/26420-simplecolordetection

As requested, this is the code I have so far. It is a mix of C and MatLab as I understand what needs to be done and I made a quick and dirty C for loop to do it, and now am trying to convert it to MatLab:

for i = 1: image.columnlength
for j = 1: image.rowlenght

    rgb =  image(i, j);
    red = rgb(:,1); %rgb(1,:);
    green = rgb(:,2);%rgb(2,:);
    blue = rgb(:,3);%rgb(3,:);
    if( red > 200 && red < 250 && green > 200 && green < 250 && blue > 200 && blue < 250 
            //image(i, j ) = green

    end
 end
end

Thanks, Steve

  • 2
    Could you include the code you currently have and the result you get? Also, it might help to take a look at [this post](https://stackoverflow.com/q/4063965/52738) regarding color selection. – gnovice Sep 27 '18 at 19:29
  • Sure. This is my code, but it is combination of C and MatLab (I understand C somewhat well, but the issue is trying and converting it to MatLab). I am actually using an approach where I am trying to get a range of yellows. I have added the code to the main post. – whatchamacallit Sep 27 '18 at 21:00

1 Answers1

0

I feel like a lot of what you want is already built-in. Unfortunately I dont understand your if condition. If every color value is between 200 and 250 you want the pixel to be green right? I just tried that but there is no pixel that would fit that conditions. I tried some other values and then it starts working, but you have to check wichi area you exactly want:

imdata = imread('pic.jpg');

for i=1:size(imdata,1)
    for j=1:size(imdata,2)
        %'...' is just a code-linebreak in matlab for readability
        if  imdata(i,j,1)>150 && imdata(i,j,1)<250 && imdata(i,j,1)>200 && ...
            imdata(i,j,1)<250 && imdata(i,j,3)>100 && imdata(i,j,3)<250
            %lets say your green is [50,200,0]
            imdata(i,j,1)=50;
            imdata(i,j,2)=200;
            imdata(i,j,3)=0;
        end
    end
end

imshow(imdata)

changed colors You can go for other logical conditions like (imdata(i,j,1)<50 || imdata(i,j,1)>150) or imdata(i,j,1)==150 or pretty much any other condition you can come up with. Also you can take a look at the values to find the right thresholds for you

figure
subplot(2,2,1)
mesh(imdata(:,:,1)) %red
subplot(2,2,2)
mesh(imdata(:,:,2)) %green
subplot(2,2,3)
mesh(imdata(:,:,3)) %blue
subplot(2,2,4)  
imshow(imdata)

In Matlab I would try to go for matrix operations rather than looping through every pixel, but this solution is much easier to understand and reconstruct.

Finn
  • 2,333
  • 1
  • 10
  • 21