One of the widely used algorithms in image processing is image classification, which can solve various problems. In this article, we will teach you fruit classification programming using the HALCON image processing software. HALCON is considered the best software for machine vision and image processing, developed by an image processing company in Germany. You can download and install it for free here.
For registration in the “Machine Vision Training Course with HALCON Software,” click here.
Download Fruit Classification Program with Image Processing
We recommend downloading the source code for fruit classification with image processing through the link below before continuing with the article. (Note: HALCON software must be installed before running the code.) Watch the instructional video while running the program to maximize the learning experience. (You need to extract the file from the compressed state before running it.)
Programming Image Processing for Fruit Classification
To classify lemons and oranges, you need to go through four stages:
1- Read and Display Images
In this stage, images are read from the specified path and displayed in the software.
2- Find Fruits
In this stage, the region of each fruit is extracted.
3- Extract Fruit Features
Using the results from the previous stage (the region of each fruit), the roundness of the fruits is calculated, and the roundness percentage is written on each fruit.
4- Classification Based on Roundness
In this stage, the class is selected based on the roundness percentage obtained in the previous stage.
Reading and Displaying Images in Image Processing Software
To start the image processing program, you need to enter your desired images into the program. As shown in the figure, there are several images in the Image folder. The read_image code is used to read images. By running this code, the image is read from the specified Image folder path and entered into the HALCON program (named Image). There are 14 images in this folder, so the for loop in the program runs 14 times. With each run, the image corresponding to the loop index is read from the Image folder path. After reading the image, the dev_display code is used to display it in a window.
Finding Fruits
To find fruits, the find_fruits function is used. This function takes the previously read image (named Image) as input and shows us the region corresponding to the fruits in the output.
find_fruits (Image, fruits)
The operation of this function is as follows: thresholding is performed on the red channel of the color image. Then, with morphological operations, the fruits are selected.
- Select the red channel of the color image.
- Using the thresholding method, find the fruits.
- Fill the fruits using morphological operations.
- Show the boundaries of the obtained regions on the image.
In the image below, the process of finding fruits is illustrated.
Calculating Features in Image Processing Program
In the previous stage, the region corresponding to each individual fruit was extracted. In this stage, the fruits_circularity function is written to calculate the roundness of each fruit. This function takes the fruit regions (output of the previous stage) as input. Then, for each fruit, it calculates the roundness as a percentage and writes it on the image. As seen in the images below, the roundness of oranges is generally more than 90%, while the roundness of lemons is less than 70%.
Fruit Classification with Image Processing
In this stage, fruit classification is performed using the classify_fruits function. This function takes the region corresponding to the fruits as input and writes the class name corresponding to them on the image.
classify_fruits (fruits)
The classify_fruits function assigns one of the classes, lemon or orange, to the fruits based on the roundness feature. If the roundness percentage is between 80 and 100, the fruit is assigned to the orange class. If this percentage is less than 80, the fruit is assigned to the lemon class.
You can consult with our experts for your projects by clicking here.