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. 

Monday, 17 February 2014

Arithmetic for the second section

I have finished the second section last year, but I have not update yet, sorry for that, I am busy in that period, interim report, C++ assignment, preparing for final exam and so on. So I am plan to update the achievement in a few days, I hope that you like it. Furthermore, I am developing the third and fourth section now, it is related to the Internet of things. The user can use the website to feed and monitor their fish. 

The first thing I need to introduce is the image processing. The objective of second part is to feed fish automatically when the webcam detects the fish. So the webcam needs to take photo for fish tank at fixed periods. Then the Raspberry Pi is used to process images which is taken just now and detects the fish. Therefore, I decide to use edge detection to find the fish. 

The basic arithmetic is shown as follows,
  1. Smooth: Suppress the noise which exists during the period of taking photos as much as possible, however, the real edges cannot be destroyed after reducing noise. This step is significant to improve accuracy for detecting fish. 
  2. Enhance the quality of image: Use the filter to sharpening the edge of the image, the possibility of detecting fish will increase after this step. 
  3. Detect the fish: In this step, program should determine what kinds of pixels should be regarded as noise and which can be kept for detecting fish, therefore, a threshold value can be set for filter the noise and keeping the original edge of fish. 
  4. Find the location of fish: The program will determine the detailed location for fish in terms of detected the edges. Furthermore, edge linking and thinning will be used in this step.

It may be boring, however, it is very important, because this theory supports feasibility of practical C++ code. Ok, some interesting things are coming.



Thursday, 14 November 2013

Start the second level

I have started my second level of project from last week, and this is much more advanced that previous one. After being finished, the fish feeder will feed fish automatically if fish comes here, the feeder will rotate to feed fish, so I think it can be called artificial intelligence project.

In this level, the webcam board (2302279) needs to be added in the raspberry pi as shown in Figure 1. 
Figure 1: Webcam board for Raspberry Pi

Furthermore, the rough algorithm is shown as following:

1.      Take photos for fish tank to detect if is there exists fish.
2.      Process the image to analysis if there is any fish in photos.
3.      If there is no fish in photos, take photos continually.
4.      If there exists some fish, drive feeder to feed fish.
5.      After some time, take photo continually.

So, the most important step is that analysis the photos to detect fish. The image processing will be involving in the second level. OK, how to process image? I will use OpenCV (Open Source Computer Vision Library). OpenCV will provide basic structure for computer vision and image processing so that we can use some functions to analysis images, you can use Matlab, C, C++, Python and so on to program. Furthermore, it can be installed in Windows, Mac, Linux so it can be used in main system platforms. 

Figure 2: OpenCV Logo