drexel_darwin_opencv
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
drexel_darwin_opencv [2016/11/01 15:07] – created dwallace | drexel_darwin_opencv [2016/11/06 19:20] (current) – dwallace | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== OpenCV ====== | ====== OpenCV ====== | ||
- | ==Motivation== | + | ===== Motivation ===== |
Since I will be working on the vision aspect of this Darwin project, I will be engaging OpenCV to process the images received from the camera. | Since I will be working on the vision aspect of this Darwin project, I will be engaging OpenCV to process the images received from the camera. | ||
- | OpenCV are libraries of files that languages like C/C++ and Python can use as long as their source codes have the link to the directory of OpenCV. Hence, compare to Matlab which is a standalone software that can also process images, OpenCV is more attractive a choice to outsource the image processing to from Darwin because it will take up less computation memory. | + | OpenCV are libraries of files that languages like C/Cpp and Python can use as long as their source codes have the link to the directory of OpenCV. Hence, compare to Matlab which is a standalone software that can also process images, OpenCV is more attractive a choice to outsource the image processing to from Darwin because it will take up less computation memory. |
I will want to be able to obtain the coordinates of each rung of the ladder which Darwin can use to project its next step in its ascend of the ladder. | I will want to be able to obtain the coordinates of each rung of the ladder which Darwin can use to project its next step in its ascend of the ladder. | ||
- | ==Setting up OpenCV== | + | ===== Setting up OpenCL |
- | I will be using Visual | + | |
+ | I will be using Visual | ||
- | I refer to here[https:// | + | I refer to [[https:// |
There were some error with using OpenCV2.1 and OpenCV2.2 in this case. | There were some error with using OpenCV2.1 and OpenCV2.2 in this case. | ||
- | *Here[http:// | + | |
- | *Here[http:// | + | * [[http:// |
- | I have changed to use OpenCV2.4.3 instead. I followed the tutorial | + | I have changed to use OpenCV2.4.3 instead. I followed the tutorial |
- | [[File: | + | {{dylanw: |
- | ''' | + | **NOTE**: The OpenCV functions used in this sample code are for C programming, |
I have also prepared a MS Power Point presentation for a step by step installation of OpenCV. | I have also prepared a MS Power Point presentation for a step by step installation of OpenCV. | ||
- | [[: | ||
- | ==Rectangle Recognition Code== | + | {{dylanw:opencv_tutorial.pptx}}\\ |
- | This[https:// | + | |
- | I will explain the main functions that are critical to make this possible. Refer to here[http:// | + | ==== Rectangle Recognition Code ==== |
- | You may need to go here[http:// | + | [[https:// |
+ | |||
+ | I will explain the main functions that are critical to make this possible. Refer to [[http:// | ||
+ | |||
+ | You may need to go [[http:// | ||
Here are some of the critical parts of the codes: | Here are some of the critical parts of the codes: | ||
- | <syntaxhighlight lang=" | + | |
+ | <code c++> | ||
VideoCapture cap(0); // open the default camera | VideoCapture cap(0); // open the default camera | ||
if(!cap.isOpened()) | if(!cap.isOpened()) | ||
Line 41: | Line 46: | ||
while(1){ | while(1){ | ||
cap >> original; | cap >> original; | ||
- | </ | + | |
- | < | + | |
Mat original, pyr, timg, gray;// | Mat original, pyr, timg, gray;// | ||
... | ... | ||
Line 48: | Line 52: | ||
pyrDown(original, | pyrDown(original, | ||
pyrUp(pyr, timg, original.size()); | pyrUp(pyr, timg, original.size()); | ||
- | </syntaxhighlight> | + | </code> |
- | ''' | + | **Note**: **pyrDown** and **pyrUp** can operate on both gray(single channel) and colored(3 channels of Blue Green and Red in the standard BGR order used in OpenCV) images. |
- | <syntaxhighlight lang=" | + | |
+ | <code c++> | ||
inRange(timg,// | inRange(timg,// | ||
Scalar(0, | Scalar(0, | ||
Scalar(90, | Scalar(90, | ||
inrange);// | inrange);// | ||
- | </syntaxhighlight> | + | </code> |
- | ''' | + | **Note**: Resultant filtered image will be a single channel of a matrix of pixels of value of wither 255(white) or 0 (black). |
- | Inside | + | Inside |
- | <syntaxhighlight lang=" | + | |
+ | <code c++> | ||
Canny(image, | Canny(image, | ||
//Detect edges on the input " | //Detect edges on the input " | ||
Line 65: | Line 71: | ||
//with lower threshold of 50 and higher threshold to 200 | //with lower threshold of 50 and higher threshold to 200 | ||
//with the aperture size of the kernel at 5 | //with the aperture size of the kernel at 5 | ||
- | </syntaxhighlight> | + | </code> |
- | ''' | + | **Note***: Input image to **Canny** function must be a single channel image. It need not be grayscale. |
- | <syntaxhighlight lang=" | + | |
+ | <code c++> | ||
dilate(gray, | dilate(gray, | ||
//take input image, that is the 1st " | //take input image, that is the 1st " | ||
Line 74: | Line 81: | ||
//using the kernel of default aperture size of 3 as defined by " | //using the kernel of default aperture size of 3 as defined by " | ||
//at default start point specified by " | //at default start point specified by " | ||
- | </ | + | |
- | < | + | |
vector< | vector< | ||
// | // | ||
Line 85: | Line 91: | ||
//processed image is saved to contours | //processed image is saved to contours | ||
//in the designated mode | //in the designated mode | ||
- | </syntaxhighlight> | + | </code> |
- | ''' | + | **Note***: Input image to **findContours** function must be a single channel image. It need not be grayscale. |
- | <syntaxhighlight lang=" | + | |
+ | <code c++> | ||
for( size_t i = 0; i < contours.size(); | for( size_t i = 0; i < contours.size(); | ||
{ | { | ||
Line 126: | Line 133: | ||
} | } | ||
} | } | ||
- | </syntaxhighlight> | + | </code> |
Here is a sample run of my code to filter out black rectangles and output the X and Y coordinates of the centers of the squares in the output MS-Prompt window. | Here is a sample run of my code to filter out black rectangles and output the X and Y coordinates of the centers of the squares in the output MS-Prompt window. | ||
- | [[File:squaresCoordinates.jpg]] | + | {{dylanw:squarescoordinates.jpg}}\\ |
Some food for thought: | Some food for thought: | ||
- | *Code is supposed to outline black rectangles, but it can also encircle the " | + | |
- | *Two or more rectangles may be identified for the same rectangle, resulting in double counting. | + | * Two or more rectangles may be identified for the same rectangle, resulting in double counting. |
- | *This is after all done using Windows Visual | + | * This is after all done using Windows Visual |
- | *The speed of computating the codes and data will need to be looked into. | + | * The speed of computating the codes and data will need to be looked into. |
+ | |||
+ | ==== Real Time Trial On Ladder(1) ==== | ||
- | ==Real Time Trial On Ladder(1)== | ||
I improved on the previous code and tested it out on an improvised ladder. | I improved on the previous code and tested it out on an improvised ladder. | ||
- | Here[https:// | + | [[https:// |
- | {{#ev:youtube|R0gCfSxRoOk}} | + | {{youtube>R0gCfSxRoOk?large}}\\ |
Video explanation: | Video explanation: | ||
- | *I created a program that creates trackbars for me to use and adjust the colour to filter out easily. | + | |
- | *I shined a light to test the effect of different light intensity on the colour filtered out. | + | * I shined a light to test the effect of different light intensity on the colour filtered out. |
- | *Initially the ladder was right in front of the back ground. I shifted the ladder away from the background such that there was some space between them. | + | * Initially the ladder was right in front of the back ground. I shifted the ladder away from the background such that there was some space between them. |
- | *During the actual detection of the ladder rungs, it was not so effective and I attributed it to the sides of the ladder. They form groove edges which disfigure the rectangular shape of the spaces in between the ladder rungs which were to be detected. | + | * During the actual detection of the ladder rungs, it was not so effective and I attributed it to the sides of the ladder. They form groove edges which disfigure the rectangular shape of the spaces in between the ladder rungs which were to be detected. |
- | *I cheated and covered the sides so that smooth rectangles were formed. Detection of the spaces in between the ladder rungs improved. | + | * I cheated and covered the sides so that smooth rectangles were formed. Detection of the spaces in between the ladder rungs improved. |
- | *I went on to detect the actual ladder rungs rather than the spaces in between them. | + | * I went on to detect the actual ladder rungs rather than the spaces in between them. |
- | *However, when I removed the covers that were covering the sides of the ladder, non of the rungs can be detected. | + | * However, when I removed the covers that were covering the sides of the ladder, non of the rungs can be detected. |
In conclusion, the ladder needs to be modified to allow easier detection of the ladder rungs. This can be done by: | In conclusion, the ladder needs to be modified to allow easier detection of the ladder rungs. This can be done by: | ||
- | *Making a new ladder that has smooth sides so that the corners of each possibly detectable rectangular shape of the spaces in between the runs or the rungs themselves form a smooth rectangle. | + | |
- | *Colour the rungs a different colour and leave the sides untouched. Filter out the colour of the rungs only | + | * Colour the rungs a different colour and leave the sides untouched. Filter out the colour of the rungs only |
- | *Create a region of interest in the centre of the camera. The detection algorithm will carry out these central pixels only instead of the whole frame. | + | * Create a region of interest in the centre of the camera. The detection algorithm will carry out these central pixels only instead of the whole frame. |
+ | |||
+ | ==== Real Time Trial On Ladder(2) ==== | ||
- | ==Real Time Trial On Ladder(2)== | ||
This algorithm depends on contour points (white circles in the picture at the top right hand corner of the insert below) detected to plot rectangles. | This algorithm depends on contour points (white circles in the picture at the top right hand corner of the insert below) detected to plot rectangles. | ||
However, as can be seen circled in red, when there is a smooth straight line, the points do not appear, and hence on the ladder rungs which has pretty smooth horizontal edge features, points are hard to come by and a rectangle is hard to detect. | However, as can be seen circled in red, when there is a smooth straight line, the points do not appear, and hence on the ladder rungs which has pretty smooth horizontal edge features, points are hard to come by and a rectangle is hard to detect. | ||
- | [[File:contourPointsLadder.jpg]] | + | {{dylanw:contourpointsladder.jpg}}\\ |
This means that one of the adjustments I made during my previous trial run, which was intended to smoothen the sides of the spaces in between ladder rungs by covering up the sides, should make the ladder rungs more difficult to be detected. However the opposite occur. | This means that one of the adjustments I made during my previous trial run, which was intended to smoothen the sides of the spaces in between ladder rungs by covering up the sides, should make the ladder rungs more difficult to be detected. However the opposite occur. | ||
Line 175: | Line 185: | ||
(I made the left cover slanted to achieve more points along vertical and prove my point) | (I made the left cover slanted to achieve more points along vertical and prove my point) | ||
- | [[File:contourPointsCoveredLadder.jpg|400px]] | + | {{dylanw:contourpointscoveredladder.jpg}}\\ |
When the ladder is tilted. the detection of rectangles improved greatly (detecting the ladder rungs instead of the spaces in between ladder rungs now by using black colored covers). | When the ladder is tilted. the detection of rectangles improved greatly (detecting the ladder rungs instead of the spaces in between ladder rungs now by using black colored covers). | ||
Line 183: | Line 193: | ||
Below is a picture of it. | Below is a picture of it. | ||
- | [[File:contourPointsTiltedLadder.jpg|400px]] | + | {{dylanw:contourpointstiltedladder.jpg}}\\ |
- | This algorithm which uses contour points and the '' | + | This algorithm which uses contour points and the **approxPolyDP()** function is not effective to detect lines which are perfectly vertical or horizontal to the camera, like the ladder rungs. |
It is thus not effective to detect the ladder rungs. | It is thus not effective to detect the ladder rungs. | ||
- | ==Real Time Trial On Ladder(3)== | + | ==== Real Time Trial On Ladder(3) ==== |
- | I found out the best way is to perform canny edge detector on a grayscale image, and then the '' | + | |
- | <syntaxhighlight lang=" | + | I found out the best way is to perform canny edge detector on a grayscale image, and then the **findContours()** function, rather than filtering out the colour of interest to work on. |
+ | |||
+ | <code c++> | ||
... | ... | ||
... | ... | ||
Line 199: | Line 211: | ||
... | ... | ||
... | ... | ||
- | </syntaxhighlight> | + | </code> |
Here are some pictures. | Here are some pictures. | ||
- | [[File:ladderRungCanny.jpg|400px]][[File:ladderRungSpaceCanny.jpg|400px]] | + | {{dylanw:ladderrungcanny.jpg}}\\ |
+ | {{dylanw:ladderrungspacecanny.jpg}}\\ |
drexel_darwin_opencv.1478038036.txt.gz · Last modified: by dwallace