青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

USING OPENCV FOR SIMPLE OBJECT DETECTION

https://solderspot.wordpress.com/2014/10/18/using-opencv-for-simple-object-detection/

My current project is to build a bot for the “Blue Block Challenge”. The goal is to create an autonomous robot that can move about a table, without falling off, find a blue colored block and move it onto a red colored disc, which is somewhere else on the table.

My very unoriginal plan is to have a video camera equipped Raspberry Pi be the eyes and brains of the robot. The Pi’s logic grabs individual frames of video from the camera and processes them using OpenCV to detect regions of a particular color and directs the robot accordingly.

However, my first goal is to learn how to use OpenCV to perform the object detection, which is the topic of this post.

Also, because this code will be running on the Raspberry Pi, which does not have a lot of processing power, it is extremely important that the detection method be as simple and efficient as possible. At this point I’m not even sure if the Raspberry Pi can achieve the required level of processing needed to get the bot to work well.

What is OpenCV

OpenCV is a very popular and powerful image processing library written in C/C++. It has lots of code for doing all sorts of image analysis and manipulation. You can visit opencv.org to learn more and there is a special page (opencv.org/books.html) that lists lots of books available for the library.

1406903806practical_opencv

I bought “Practical OpenCV” myself but it is way overpriced for what it is so I cannot recommend it. Still, it did get me up and running quickly.

cv::Mat

OpenCV is typically used for image processing but it is a more general purpose library than that. It actually works with matrices using the class cv::Mat.

However, to do any image processing we first need to create a cv::Mat instance for our image.

For example, if we have the typical 8 bit RGBA image we can create the cv::Mat for it using the constructor:

Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)

#include <opencv2/opencv.hpp>
 
void *first_pixel = <pointer to start of 8 bit RGBA image data>;
int rows = <height of image>;
int cols = <width of image>;
cv::Mat frame = cv::Mat(rows, cols, CV_8UC4, first_pixel);

CV_8UC4 tells OpenCV that each entry in the matrix is made up of 4 consecutive unsigned 8 bit values (i.e. 0 – 255).

Converting Color Space to HSV

Our image format is in 24 bit RGBA for each pixel, i.e. an unsigned byte (0-255) for red, green, blue and alpha components – in this case we ignore alpha as the image is coming from a camera.

The first step is to convert the input frame from RGB format to HSV thus:

cv::Mat rgb_frame = get_frame_from_video();
cv::Mat hsv_frame;
cv::cvtColor(rgb_frame, hsv_frame, CV_RGB2HSV);

The HSV format is much more useful for doing based color processing than using RGB because we get a single channel (H) that holds the color value of the pixel and two other channels (S and V) that hold the saturation level and brightness level of the pixel.

What is HSV?

HSV_color_solid_cylinder_alpha_lowgammaHSV is a three value format for describing a color with the properties “hue”, “saturation” and “value”.

The first property “Hue” is given as an angle from 0° to 360° of a color wheel where 0° is pure red, 120° is pure green and 240° is pure blue. For example, purple would be half way between blue and red, i.e. 300°

The other two properties are a little harder to describe but we can think of “saturation” as saying how strong or pale the color is, and “value” says how bright or dark the color is. You can get the technical low-down here: http://en.wikipedia.org/wiki/HSL_and_HSV

In OpenCV the HSV format is stored as 3 unsigned 8 bit values. For saturation and value the ranges 0-255 are used. For the hue component we also have a maximum range of 0 to 255 but hue is a value from 0 to 360 so OpenCV stores the hue as half the angle, i.e. range 0 to 180.

Color Thresholding

Thresholding is a fundamental image processing technique whereby we replace each pixel in an image with a “yes” or “no” value depending on whether that pixel meets some criteria. We effectively create a black and white version of the original image where “white”, i.e. value 255, means “yes” and “black” (0) means “no”.

In our case we are going to use a function called cv::inRange()

cv::Scalar   min(hueMinValue, satMinValue, volMinValue);
cv::Scalar   max(hueMaxValue, satMaxValue, volMaxValue)
cv::Mat threshold_frame;
cv::inRange( hsv_frame, min, max, threshold_frame);

The cv::inRange() operation simply compares each HSV value in the frame and replaces it with the value 0 if it is outside the min/max values or with 255 if it is inside the range.

Here is an example:

cv::Scalar   min(220/2, 0, 0);
cv::Scalar   max(260/2, 255, 255)
cv::Mat threshold_frame;
cv::inRange( hsv_frame, min, max, threshold_frame);

threshold1

In this case we are using a Hue range of 220 to 260 which is a range of blues, and saturation and volume range of 0 – 255, which basically means any saturation and any volume.

In the black and white image the white areas are pixels that fall into the hue range we set. You can see that the blue brick shows up but so does a lot of other blueish surfaces.

In this case our blue brick has a very strong color so we can adjust the saturation min range higher so that we can remove paler blues from the threshold. If we set the saturation range to be 190 to 255:

cv::Scalar   min(220/2, 190, 0);
cv::Scalar   max(260/2, 255, 255)
cv::Mat threshold_frame;
cv::inRange( hsv_frame, min, max, threshold_frame);

We get the following threshold image:
threshold2

As you can see we’ve isolated the blue brick more. Our brick is also fairly well lit so we can remove the darker areas from the threshold image by increase the minimum threshold for value to say 80, so we have a range of 80 to 255:

cv::Scalar   min(220/2, 190, 80);
cv::Scalar   max(260/2, 255, 255)
cv::Mat threshold_frame;
cv::inRange( hsv_frame, min, max, threshold_frame);

threshold3

We’ve pretty much isolated the brick now. There are still some noise in the image but we can remove them by doing the following trick:

cv::Mat str_el = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
morphologyEx(threshold_frame, threshold_frame, cv::MORPH_OPEN, str_el);
morphologyEx(threshold_frame, threshold_frame, cv::MORPH_CLOSE, str_el);

And we get the final result:

threshold4

Finding the Objects

As is the threshold image is not much use to our robot. We need to somehow find the positions of the white areas and their size. We can do this by finding the circles that enclose each of the white areas. This would provide us with an array of positions and radii which our robot can actually use to track objects.

OpenCV has the function cv::findContours() that finds all the “contours” or edges of an image as an array of points. In fact it creates an array of these arrays of points, each set of points represents a distinct region in the image. Using another function called cv::minEnclosingCircle() we can convert these arrays of points into arrays of center points and radii:

cv::vector<cv::vector<cv::Point> > contours;
cv::vector<cv::Vec4i> heirarchy;
cv::vector<cv::Point2i> center;
cv::vector<int> radius;
 
cv::findContours( threshold_frame.clone(), contours, heirarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
 
size_t count = contours.size();
 
for( int i=0; i<count; i++)
{
    cv::Point2f c;
    float r;
    cv::minEnclosingCircle( contours[i], c, r);
 
    if (!enableRadiusCulling || r >= minTargetRadius)
    {
        center.push_back(c);
        radius.push_back(r);
    }
}

In the code I’ve also put in logic to reject circles with radii less than some minimum value. This way I can cull out any remaining noise or small objects. In my particular case the objects I’m detecting are going to be largish compared to the field of view of the camera.

To verify the code is working correctly we can then use the cv:circle() function to draw in the possible targets as a red circle:

size_t count = center.size();
cv::Scalar red(255,0,0);
 
for( int i = 0; i < count; i++)
{
    cv::circle(threshold_frame, center[i], radius[i], red, 3);
}

threshold6

So now we have a pipeline that takes images from the camera and outputs possible object targets as an array of x and y image positions and radii.

Resizing the Source Image

Because I’m going to be dealing with largish areas of color, the resolution of the image does not need to be very high. In fact, as all this image processing will be happening on the Raspberry Pi it is doubly important to minimize the amount of work it has to do.

A very simple method is to just reduce the amount of data by either setting the camera up to capture low resolution images and/or resizing the image before we process it.

To do the latter we simply use the cv::resize() function:

float s = 0.5;
cv::resize( frame, frame, cv::Size(), s, s, cv::INTER_NEAREST);

This will scale the x and y axis by 50%.

OpenCVDetect App

To be better able to develop the image processing code I ended up creating an app for my Mac that lets me experiment with all the settings in realtime on a live video stream.

The source for the app is on GitHub: https://github.com/solderspot/OpenCVDetect

Screen Shot 2014-10-17 at 10.26.38 PM
Note that if you want to build this app yourself you’ll need to install OpenCV V2 on your machine. I used home brew to do that.

The controls are relatively straight forward. First select a capture device and format, and then click start. The left image view is of the live video without any processing. The right image view is of the processed image.

Below the image views are two control panels. The right hand one has the following options:

Screen Shot 2014-10-17 at 8.25.50 PM

Use the checkboxes to enable and disable features and adjust sliders accordingly.

The culling option lets you specify a minimum radius size as a percentage of the screen size.

The left hand panel has the controls for setting the color thresholding values:

Screen Shot 2014-10-17 at 8.25.18 PM

One of the limitations with the thresholding range for hue is that we can’t have ranges that span 0° without adding more logic and doing two thresholding passes. For now I have found that having hue clipped at 0 and 360 is not a problem.

I also made a quick video showing the app in action.

Conclusion

I’ve have a lot of fun playing with OpenCV on my Mac. Next step is to get it and camera capture working on the Raspberry Pi, which is proving to be laborious as this is my first time dealing with the Pi and I’m all thumbs at present.

I should clarify that what I’m doing here is not true object detection. I’m relying on the fact that the objects I’m dealing with in the challenge are the only things of their color.

Also, the color thresholding values are very dependent on ambient lighting conditions so I’ll need to calibrate the values specifically for the conditions at the time of the challenge. Basically it’s all a hack to do the minimum amount of processing possible on the Pi and it might not turn out too well.

posted on 2017-09-14 16:07 zmj 閱讀(747) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲精品免费一二三区| 老司机午夜精品视频在线观看| 香蕉久久夜色精品国产使用方法| 亚洲精品无人区| 国产欧美日韩视频一区二区三区| 午夜一区二区三视频在线观看| 久久精品日韩欧美| 99国产精品| 亚洲第一黄网| 国产一区二区三区精品欧美日韩一区二区三区 | 欧美激情一区二区三区全黄| 亚洲欧美日韩国产精品| 99精品国产福利在线观看免费| 欧美色综合网| 欧美金8天国| 久久免费偷拍视频| 久久国内精品视频| 久久成人免费网| 午夜亚洲伦理| 亚洲欧美日韩精品久久久久| 亚洲一区二区三区乱码aⅴ| 亚洲日本va在线观看| 欧美高清视频一区二区三区在线观看| 一本到高清视频免费精品| 亚洲国产乱码最新视频| 国产在线一区二区三区四区| 国产区亚洲区欧美区| 国产日韩高清一区二区三区在线| 麻豆91精品| 欧美成人一区在线| 欧美成人a视频| 欧美激情在线观看| 欧美国产先锋| 欧美日韩中文字幕| 国产精品qvod| 国产精品久久久久久久久久ktv| 久久精品国产欧美亚洲人人爽| 亚洲电影毛片| 亚洲韩国青草视频| 亚洲激情av在线| 亚洲日本欧美| avtt综合网| 亚洲欧美激情诱惑| 久久精品国产亚洲aⅴ| 久久艳片www.17c.com| 欧美成人免费小视频| 欧美日韩成人在线播放| 国产精品久久午夜| 国产一区导航| 亚洲激精日韩激精欧美精品| 亚洲美女色禁图| 亚洲一区二区三区四区五区午夜| 亚洲国产一区二区三区青草影视| 国产精品人成在线观看免费 | 篠田优中文在线播放第一区| 午夜在线一区二区| 久久久爽爽爽美女图片| 欧美激情性爽国产精品17p| 欧美日韩视频在线观看一区二区三区 | 亚洲国产精品久久久久秋霞影院| 国产精品久久一区二区三区| 国产日产高清欧美一区二区三区| 欧美日韩精品免费| 国产精品综合久久久| 激情成人中文字幕| 99pao成人国产永久免费视频| 亚洲人成久久| 亚洲男人第一网站| 免费亚洲视频| 亚洲最新在线| 久久精品中文字幕一区| 欧美精品福利视频| 国产午夜精品理论片a级大结局| 国产精品va在线播放我和闺蜜| 欧美黑人在线观看| 国产精品一二三四| 亚洲国产成人av好男人在线观看| 一区二区三区在线高清| 一区二区免费看| 欧美中文字幕精品| 亚洲国产一区视频| 欧美亚洲网站| 欧美日韩在线播放三区四区| 狠狠干综合网| 午夜免费久久久久| 亚洲成色999久久网站| 午夜亚洲福利| 欧美日韩一区二区高清| 亚洲大胆女人| 久久国产精品黑丝| 亚洲日韩欧美视频| 久久中文精品| 国产日韩欧美视频| 亚洲香蕉成视频在线观看| 欧美高清你懂得| 欧美一区二区三区的| 国产精品国产精品| 亚洲免费高清视频| 欧美.日韩.国产.一区.二区| 午夜精品理论片| 欧美日韩综合网| 日韩午夜在线播放| 欧美成人自拍视频| 久久精品一区二区三区不卡| 国产精品成人国产乱一区| 亚洲三级免费观看| 欧美www视频| 久久久久一区| 国产日韩精品入口| 香蕉成人久久| 日韩视频在线观看免费| 欧美91大片| 亚洲激情视频在线| 毛片精品免费在线观看| 午夜视频久久久| 国产日本亚洲高清| 欧美一区二区三区免费在线看| 欧美成人国产va精品日本一级| 91久久精品日日躁夜夜躁欧美 | 亚洲视频一二三| 亚洲激情第一区| 欧美高清在线观看| 亚洲国产精品第一区二区| 久久综合中文| 久久久久久久成人| 一区二区视频欧美| 欧美成人午夜激情视频| 久久夜色精品一区| 91久久在线观看| 亚洲国产1区| 欧美精品综合| 亚洲性夜色噜噜噜7777| 亚洲色图自拍| 国产日产欧美一区| 久久青草欧美一区二区三区| 久久精品国产精品亚洲| 1000部国产精品成人观看| 欧美成人精品在线播放| 美女视频黄 久久| 99热精品在线| 99re6这里只有精品视频在线观看| 欧美一区二区成人| 欧美少妇一区| 欧美一区二区三区婷婷月色| 欧美一区二区视频97| 精品盗摄一区二区三区| 亚洲第一在线| 欧美日韩理论| 欧美一区免费视频| 欧美一区二区三区日韩视频| 国内精品免费午夜毛片| 欧美激情一区二区三区不卡| 欧美日韩1234| 久久av红桃一区二区小说| 久久久噜噜噜久噜久久| 日韩亚洲一区二区| 亚洲嫩草精品久久| 亚洲国产精品va在线观看黑人| 久久精品在线视频| 媚黑女一区二区| 中文欧美在线视频| 久久都是精品| 亚洲靠逼com| 亚洲欧美网站| 亚洲精品国产系列| 亚洲欧美另类综合偷拍| 亚洲国产精品黑人久久久| 夜夜嗨av一区二区三区| 国产一区二区三区免费在线观看 | 久久av一区二区三区漫画| 91久久精品一区二区三区| 亚洲另类视频| 国产在线视频欧美一区二区三区| 欧美中文字幕在线播放| 麻豆乱码国产一区二区三区| 亚洲影院污污.| 老色鬼久久亚洲一区二区| 亚洲伊人色欲综合网| 久久夜色精品国产| 亚洲一区影院| 美玉足脚交一区二区三区图片| 亚洲国产精品一区二区www在线| 久久香蕉国产线看观看av| 欧美国产视频在线| 久久久天天操| 欧美性大战久久久久| 欧美电影美腿模特1979在线看| 免费成人av在线| 久久国产欧美精品| 欧美精品v日韩精品v韩国精品v | 亚洲一区高清| 亚洲日本va在线观看| 亚洲欧美怡红院| 亚洲视频在线观看三级| 久久久爽爽爽美女图片| 性欧美办公室18xxxxhd| 欧美日韩国产色综合一二三四| 亚洲永久免费视频| 欧美成人在线免费观看| 久久久久久夜精品精品免费|