Wednesday, 26 March 2014

Detecting fish on Raspberry Pi

After simulation on computer, I need to detect the fish on Raspberry Pi. In the case, the instruction for compiling C++ code with OpenCV library and WiringPi library is shown as following,

$g++ main.cpp -o main `pkg-config --cflags --libs opencv` -lwiringPi
And run it using following,
$sudo ./main
The results are shown as following video, I use the fish picture which is taken from fish lab,

Video 1: Detect fish on Raspberry Pi

As the video shown, it can detect the fish in the figure and then fish feeder will be rotating. Yeah, the screen also shows the status of feeder and if any fish is detected. 

This is an important step for project, and I nearly finish it, I think that I can consider the web control parts. 


Tuesday, 25 March 2014

Simulation for detecting fish on computer

Before detecting fish using Raspberry Pi, I need to detect fish on computer. In this case, I choose Windows 8.1 platform and Visual Studio for running the C++ code for detecting fish. The original image is taken from the fish lab in university, I choose one for detection,
Figure 1: Original image for fish

After processing, the picture is shown as follows:
Figure 2: Processed image


As shown in Figure 2, the color of background has been changed to the black and the color of noise changed to gray, the edge of fish has been changed to white, so that I need to count the number of white pixels which is shown in the window behind of processed image. In this case, the number of white pixels is 110. 

Then process another image which does not have fish as shown in Figure 3,
Figure 3: No fish in image

The processed image is shown as following,
Figure 4: Processed image

There are only noise on the processed image and the number of white pixels is 4. After statistics, the threshold value can be set 10. If the the number of white pixels are larger than 10, the feeder will be rotating because fish is detected, otherwise, the feeder does not operate. 

Install OpenCV on Raspberry Pi

If I want to process image, I need to install OpenCV library on the Raspberry Pi. I found a website which introduces the step teach how to install the OpenCV, it is shown as follows,



Download, Install and config, It costs long time about several hours. You can follow every steps in above website.

A small C++ program to test library and GPIO Pins

I wrote a C++ program to control pins turning on or off, the program is shown as following:

Figure 1: C++ code for driving the fish feeder

A infinite loop is used to feed fish all time, and first output a high pulse from pin 11 to drive the feeder rotating for about 9.325s, which is the time for rotating one cycle. Then set the pin is low so that the feeder is idle. After that, set the pin being high again and so on. 

Then put it in the Raspberry Pi and compile it using the following command,
$g++ Blink.cpp -o Blink -lwiringPi
And then run it using the following command,

$sudo ./Blink

The results can be shown in following video:

Video 1: Video for controlling feeder using C++

It shows that the feeder rotates successfully, so this can be used to control GPIO pins using C++.


Programming language for second part

I used Python in the first part which is control fish feeder using timer, however, I think I need to change C++ for feeding fish automatically, because there are much more sources for image processing in C++. 

Then I need the C++ library to control the Raspberry Pi, I choose "WiringPi" which is a special C++ library for controlling Pi, I want to use it to control GPIO pins. The website for "WiringPi" is shown as


The first thing I need to do is install this library on the Raspberry Pi, the steps is shown in the following website


After that, I think I can write a small program to test it.