pcbway

color detection in matlab,matlab color detection,color detection matlab,detect color matlab,detect color in matlabHello friends, hope you all are fine and having fun with your lives. In today’s tutorial, we are gonna see Color Detection in Images using MATLAB. In the previous tutorial, I have posted about How to Detect Circles in Images using MATLAB in which we have detected objects based on their geometrical figure means either they are circle or not but today we are gonna distinguish objects based on their color i.e. whether they are red colored or green colored etc. Its a quite simple tutorial and comes in the basic category. We will first detect the color and then will create a boundary around that object and will also show its XY coordinates as well.

Image processing is an important tool of MATLAB. We can quite easily do the image processing in it using Image Processing toolbox so you need to make sure that your MATLAB must have Image processing toolbox before running this code. So, let’s start with the project.

Color Detection in Images using MATLAB

  • In order to do the Color Detection in Images using MATLAB, first thing we are gonna need is the image itself. 😛
  • So, I designed an image in paint which has different shapes in different colors as shown in below figure:

color detection in matlab,matlab color detection,color detection matlab,detect color matlab,detect color in matlab

  • As you can see in the above figure, there are different shapes in different colors so now we are gonna detect these objects on the basis of their color.
  • Now use the below code and add it in your MATLAB m file:

  • Now, run your m file and if everything goes fine then you will get an image as shown in below figure:

color detection in matlab,matlab color detection,color detection matlab,detect color matlab,detect color in matlab

  • You can see in the above figure, we have only detected the shapes with green color.
  • The + sign indicates the center of each detected shape.
  • X and Y are the x,y coordinates of the center point of each shape which are shown in black for each detected shape.
  • Now, let’s detect the red color in above figure, so in order to do so what I need to do is to simply change the third value in imsubtract function from 2 to 1.
  • The complete code for red color detection in MATLAB is shown below:

  • Now, when you run this code in MATLAB, you will get the output as shown in below figure:

color detection in matlab,matlab color detection,color detection matlab,detect color matlab,detect color in matlab

  • Now instead of detecting the green shapes, it has now detected the red shapes in MATLAB.
  • And have also shown their x,y coordinates in black color.
  • Now, finally let’s detect the blue color, in order to do, you now just need to change the third parameter to 3 and it will detect the blue color as shown in below figure:

color detection in matlab,matlab color detection,color detection matlab,detect color matlab,detect color in matlab

  • That’s all for today, and I think its quite an easy one as I mentioned earlier this one is quite basic tutorials. Thanks for reading and I hope it will help you in some ways. 🙂

8 Comments

  1. Great article. Learned a lot. Thank you for spending the time making this article. You don’t know how much it has helped me. Just one question. How would we detect the shape of any other color besides red, green and blue. How would we detect shapes that have a yellow color. And how would we be able to implement it for any other color.

  2. Thanks for the great example! My only question is why the Y axis is inverted when the centroid is found? Is there an easy way to fix this?

    Thanks,
    Nick

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment