• <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>

            Detect red circles in an image using OpenCV

            https://solarianprogrammer.com/2015/05/08/detect-red-circles-image-using-opencv/

            The code for this post is on GitHub: https://github.com/sol-prog/OpenCV-red-circle-detection.

            A few days ago someone asked me, in an email, if it is possible to detect all red circles in an image that contains circles and rectangles of various colors. I thought this problem could be of certain interest to the readers of this blog, hence the present article.

            From the many possible approaches to the problem of red circles detection, two seem straightforward:

            • Detect all circles from the input image and keep only the ones that are filled with red.
            • Threshold the input image in order to keep only the red pixels, search for circles in the result.

            I found the second approach to be slightly better than the first one (less false positives), so I am going to present it in this post.

            I will use the OpenCV library and C++, but you can easily follow along with any of the other OpenCV bindings (C, Python, Java).

            Lets start by thresholding the input image for anything that is not red. Instead of the usual RGB color space we are going to use the HSV space, which has the desirable property that allows us to identify a particular color using a single value, the hue, instead of three values. As a side note, in OpenCV H has values from 0 to 180, S and V from 0 to 255. The red color, in OpenCV, has the hue values approximately in the range of 0 to 10 and 160 to 180.

            Next piece of code converts a color image from BGR (internally, OpenCV stores a color image in the BGR format rather than RGB) to HSV and thresholds the HSV image for anything that is not red:

             1 	...  2 	// Convert input image to HSV  3 	cv::Mat hsv_image;  4 	cv::cvtColor(bgr_image, hsv_image, cv::COLOR_BGR2HSV);  5   6 	// Threshold the HSV image, keep only the red pixels  7 	cv::Mat lower_red_hue_range;  8 	cv::Mat upper_red_hue_range;  9 	cv::inRange(hsv_image, cv::Scalar(0, 100, 100), cv::Scalar(10, 255, 255), lower_red_hue_range); 10 	cv::inRange(hsv_image, cv::Scalar(160, 100, 100), cv::Scalar(179, 255, 255), upper_red_hue_range); 11 	... 

            Take the next input image as an example:

            Five colored circles

            if we use the above piece of code, this is what we get:

            Lower red hue range

            Upper red hue range

            As you can see, the first threshold image captured the big red circle from the input image, while the second threshold image has captured the smaller red circle. Typically, you won’t see such a clear separation between the two red ranges. I’ve slightly cheated when I filled the circles in GIMP and used hue values from both intervals, in order to show you that a similar situation can arrive in practice.

            Next step is to combine the above threshold images and slightly blur the result, in order to avoid false positives:

            1 	... 2 	// Combine the above two images 3 	cv::Mat red_hue_image; 4 	cv::addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image); 5  6 	cv::GaussianBlur(red_hue_image, red_hue_image, cv::Size(9, 9), 2, 2); 7 	... 

            Combined red hue range

            Once we have the threshold image that contains only the red pixels from the original image, we can use the circle Hough Transform to detect the circles. In OpenCV this is implemented as HoughCircles:

            1 	... 2 	// Use the Hough transform to detect circles in the combined threshold image 3 	std::vector<cv::Vec3f> circles; 4 	cv::HoughCircles(red_hue_image, circles, CV_HOUGH_GRADIENT, 1, red_hue_image.rows/8, 100, 20, 0, 0); 5 	... 

            As a side note, parameters 6 and 7 from the HoughCircles must be usually tuned from case to case in order to detect circles. All found circles are stored in the circles vector from the above piece of code, using this information we can outline the detected circles on the original image:

            1 	// Loop over all detected circles and outline them on the original image 2 	if(circles.size() == 0) std::exit(-1); 3 	for(size_t current_circle = 0; current_circle < circles.size(); ++current_circle) { 4 		cv::Point center(std::round(circles[current_circle][0]), std::round(circles[current_circle][1])); 5 		int radius = std::round(circles[current_circle][2]); 6  7 		cv::circle(orig_image, center, radius, cv::Scalar(0, 255, 0), 5); 8 	} 

            Outline of the detected circles

            Lets try the code on a slightly more complex image:

            Circles and rectangles input image

            and the result:

            Circles and rectangles detected red circles

            Adding some noise to the same input image as above:

            Circles and rectangles input image with noise

            and the incredible result:

            Circles and rectangles with noise detected red circles

            Ouch! Apparently the noise from the input image fooled the Hough detector and now we have more circles than we’ve expected. A simple cure is to filter the input image before the BGR to HSV conversion, for this kind of noise usually a median filter works best:

            1 	... 2 	cv::medianBlur(bgr_image, bgr_image, 3); 3  4 	// Convert input image to HSV 5 	cv::Mat hsv_image; 6 	cv::cvtColor(bgr_image, hsv_image, cv::COLOR_BGR2HSV); 7 	... 

            and now the result is much improved:

            Circles and rectangles with noise median filter detected red circles

            posted on 2017-08-29 10:52 zmj 閱讀(615) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            久久久久香蕉视频| 亚洲AV日韩AV永久无码久久| 精品熟女少妇a∨免费久久| 久久精品国产亚洲AV大全| 国产精品一区二区久久精品| 99久久www免费人成精品| 久久性生大片免费观看性| 久久久久国产精品嫩草影院| 久久精品中文騷妇女内射| 久久国产精品久久| 一本一本久久a久久精品综合麻豆| 精品久久久久久国产| 久久久久久综合一区中文字幕 | 日本精品久久久久影院日本| 综合久久给合久久狠狠狠97色| 久久综合狠狠综合久久| 久久影院午夜理论片无码 | 精品久久无码中文字幕| 国内精品久久久久久久影视麻豆| 亚洲欧美日韩精品久久亚洲区| 国内精品九九久久久精品| 伊人久久大香线蕉成人| 亚洲国产精品人久久| 婷婷五月深深久久精品| 久久影院午夜理论片无码 | 色婷婷久久综合中文久久一本| www.久久热.com| 久久综合狠狠综合久久综合88 | 国产精品九九久久精品女同亚洲欧美日韩综合区 | 无码8090精品久久一区| 青青青青久久精品国产| 久久久久亚洲av无码专区喷水| 亚洲&#228;v永久无码精品天堂久久 | 久久亚洲高清综合| 国产亚州精品女人久久久久久| 久久青青草原亚洲av无码app| 久久久久se色偷偷亚洲精品av| 亚洲国产日韩欧美久久| 人人狠狠综合88综合久久| 午夜视频久久久久一区| 精品久久久久国产免费|