Detect Circles in Images Using MATLAB

Hello friends, hope you all are fine and having fun with your lives. In today's post we are gonna see How to detect Circles in Images using MATLAB. It's quite a simple and basic tutorial but quite helpful as today I was working on a project in which I need to do so but all the codes available online were for detection and tracking of circles in live images. So, I thought to share this one on our site. I have also posted another tutorial Color Detection in Images using MATLAB, so I think its better if you check that one as well.

We all know about MATLAB, which is a great tool for image processing and quite easy as it has a strong Help section. I haven't posted much tutorials on MATLAB in my blog so from now on I am gonna post tutorial on MATLAB as I get many requests about it. If you have any requests then use our Contact form and send them to us and I will try my best to postrelated tutorials. I personally prefer OpenCV over MATLAB, when it comes to image processing but in OpenCV there's not much flexibility as in MATLAB. Anyways, let's start our tutorial which is How to Detect Circles in Images using MATLAB.

Detect Circles in Images Using MATLAB

  • First of all, you are gonna need an Image, in which you are gonna find circles so I used this image of a bike.
  • As we can see there are two circles in the above image, which are two tyres, so we are gonna detect them now.
  • So, copy the below code and paste it in your MATLAB m file.
ImagePath='TEP.jpg'; %Give Path of your file here
Img=imread(ImagePath);

Img=im2bw(Img(:,:,3));  
Rmin=70;
Rmax=100;  
[centersDark, radiiDark] = imfindcircles(Img, [Rmin Rmax], ...
                                        'ObjectPolarity','bright','sensitivity',0.90)
imagesc(Img);
hold on
viscircles(centersDark, radiiDark,'LineStyle','--');
hold off
  • Now, when you run this code you will get something as shown in below figure:
  • As you can see in the above figure, both the circles are now detected and are marked with red and white line.
  • The command, I have used for detecting these circles is imfindcircles , where Rmin and Rmax are the minimum and maximum radius of the circles.
  • It will detect only those circles which will lie in this range and other circles will be ignored.
  • Moreover, the last parameter is for setting the sensitivity of circle detection, which I have given is 0.90.
  • It will also give the center of these circles along with their radii in the command window as shown in below figure:
  • As, we have detected two circles so the command window is showing the X, Y coordinates of both these circles along with their radii.
  • Now test it and also change the minimum and maximum ranges and you will see you are gonna detect more circles.
It was quite easy but if you have problem in it then ask in comments and I will try my best to solve them. That's all for today, will meet in next tutorial. Till then take care !!! :)

How to Reset Arduino Programmatically

Hello friends, hope you all are fine and having fun with your lives. Today's post is about How to Reset Arduino Programmatically. Sounds a bit weird, yes it is :) but literally in some cases, this technique is the only choice you have. It recently happened to me in one of my projects, that's why I know How important it is. Before going into details, let's first have a look at the resetting feature of Arduino.

If you have worked on any Arduino board, then you must have noticed the RESET pin in Arduino and you may wonder what's the use of this pin. So, today this pin is gonna get useful. Moreover, you have also noticed that when you upload the code to your Arduino board then the Arduino resets, another way of resetting Arduino is by opening the Serial Terminal in Arduino software, while connecting your Arduino board to your computer. As you open the Serial Terminal, the Arduino automatically gets reset. The third way of resetting Arduino is by pressing the push button. When you press and release the push button, Arduino gets reset. You should also have a look at How to get Hex File from Arduino.

So till now we have seen three ways of resetting Arduino but you have noticed that all of these methods are manual, you have to manually push the button or to open the Serial Terminal or to upload the code. Now in some projects, we have to reset Arduino Programmatically, like we don't do anything and it just reset itself automatically. Now how can we do that, that's the topic of today's tutorial. So, I am gonna share two methods today using which we are gonna reset Arduino programmatically. So, let's start with them.

Where To Buy?
No.ComponentsDistributorLink To Buy
1Jumper WiresAmazonBuy Now
2Arduino UnoAmazonBuy Now

Reset Arduino Programmatically using RESET Pin

  • In the first method, we are going to reset Arduino Programmatically using the RESET Pin available on the Arduino board.
Note:
  • If you haven't bought your Arduino UNO yet, then you can buy it from this reliable source:
  • So, first of all, connect Arduino Reset Pin with any of the digital pins as I have connected it with Pin#4 shown in the below figure:
  • Now upload the below code to your Arduino board:
 
int Reset = 4;

void setup() {  
  digitalWrite(Reset, HIGH);
  delay(200); 
  pinMode(Reset, OUTPUT);     
  Serial.begin(9600);
  Serial.println("How to Reset Arduino Programmatically");
  Serial.println("www.TheEngineeringProjects.com");
  delay(200);
}
void loop() 
{
  Serial.println("A");
  delay(1000);               
  Serial.println("B");
  delay(1000);               
  Serial.println("Now we are Resetting Arduino Programmatically");
  Serial.println();
  delay(1000);
  digitalWrite(Reset, LOW);
  Serial.println("Arduino will never reach there.");

}

  • Once you have uploaded the code then and open your Arduino Serial Monitor and you will get something as shown in the below figure:
  • As you can see in the above figure, our Arduino is not displaying the line "Arduino will never reach there" and got reset and then display from start. So that's how it's going to work.
  • Now let's have a look at the second method of How to Reset Arduino Programmatically.

Reset Arduino Programmatically using reset Function

  • In this method, we are not going to use any hardware pin, instead, we will do everything in programming.
  • So, if you don't know much about Arduino Programming then you should have a look at Getting Started with Arduino Programming.
  • Arduino has a built-in function named as resetFunc() which we need to declare at address 0 and when we execute this function Arduino gets reset automatically.
  • So, no need of doing anything in hardware and simply upload the below code to your Arduino board.
void(* resetFunc) (void) = 0;
 
void setup() {     
  Serial.begin(9600);
  Serial.println("How to Reset Arduino Programmatically");
  Serial.println("www.TheEngineeringProjects.com");
  delay(200);
}

void loop() 
{
  Serial.println("A");
  delay(1000);               
  Serial.println("B");
  delay(1000);               
  Serial.println("Now we are Resetting Arduino Programmatically");
  Serial.println();
  delay(1000);
  resetFunc();
  Serial.println("Arrduino will never reach there.");
 
}
  • Now open your Arduino Serial Terminal and you will get the same output as we get in the first method and shown below:
  • In the code you have seen that we defined the function resetFunc() and then where we call that function, our Arduino gets reset at that point.
It was quite a simple tutorial, but if you have any problems then ask in the comments and I will try to resolve them. So that's all for today and will meet in the next tutorial. Till then take care !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir