color_detecting
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | color_detecting [2019/06/21 05:58] (current) – created myeonggeonkim | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Color Detecting using OpenCV ====== | ||
+ | |||
+ | **Author:** Myong Geon Kim Email: [email protected] | ||
+ | \\ | ||
+ | **Date:** Last modified on < | ||
+ | \\ | ||
+ | **Keywords: | ||
+ | \\ | ||
+ | |||
+ | {{ mgkim: | ||
+ | \\ | ||
+ | This tutorial is for whom will continue DASL Intern Gantry project. Visual studio and OpenCV library are required for this tutorial. | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Motivation and Audience ===== | ||
+ | \\ | ||
+ | \\ | ||
+ | Readers of this tutorial assumes the reader has the following background and interests: | ||
+ | \\ | ||
+ | \\ | ||
+ | <fc blue> | ||
+ | \\ | ||
+ | * Basic background of CPP | ||
+ | * Basic background of using OpenCV library : [[open_cv_set_up|How to Use OpenCV library]]. | ||
+ | \\ | ||
+ | </fc> | ||
+ | \\ | ||
+ | The rest of this tutorial is presented as follows: | ||
+ | * [[color_detecting# | ||
+ | * [[color_detecting# | ||
+ | * Final Words | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== Code ==== | ||
+ | \\ | ||
+ | \\ | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace cv; | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int range_count = 0; | ||
+ | |||
+ | Scalar red(0, 0, 255); | ||
+ | Scalar blue(255, 0, 0); | ||
+ | Scalar yellow(0, 255, 255); | ||
+ | |||
+ | Scalar magenta(255, | ||
+ | |||
+ | |||
+ | Mat rgb_color = Mat(1, 1, CV_8UC3, red); | ||
+ | Mat hsv_color; | ||
+ | |||
+ | cvtColor(rgb_color, | ||
+ | |||
+ | |||
+ | int hue = (int)hsv_color.at< | ||
+ | int saturation = (int)hsv_color.at< | ||
+ | int value = (int)hsv_color.at< | ||
+ | |||
+ | |||
+ | cout << "hue = " << hue << endl; | ||
+ | cout << " | ||
+ | cout << "value = " << value << endl; | ||
+ | |||
+ | |||
+ | int low_hue = hue - 10; | ||
+ | int high_hue = hue + 10; | ||
+ | |||
+ | int low_hue1 = 0, low_hue2 = 0; | ||
+ | int high_hue1 = 0, high_hue2 = 0; | ||
+ | |||
+ | if (low_hue < 10) { | ||
+ | range_count = 2; | ||
+ | |||
+ | high_hue1 = 180; | ||
+ | low_hue1 = low_hue + 180; | ||
+ | high_hue2 = high_hue; | ||
+ | low_hue2 = 0; | ||
+ | } | ||
+ | else if (high_hue > 170) { | ||
+ | range_count = 2; | ||
+ | |||
+ | high_hue1 = low_hue; | ||
+ | low_hue1 = 180; | ||
+ | high_hue2 = high_hue - 180; | ||
+ | low_hue2 = 0; | ||
+ | } | ||
+ | else { | ||
+ | range_count = 1; | ||
+ | |||
+ | low_hue1 = low_hue; | ||
+ | high_hue1 = high_hue; | ||
+ | } | ||
+ | |||
+ | |||
+ | cout << low_hue1 << " | ||
+ | cout << low_hue2 << " | ||
+ | |||
+ | |||
+ | VideoCapture cap(1); | ||
+ | Mat img_frame, img_hsv; | ||
+ | |||
+ | |||
+ | if (!cap.isOpened()) { | ||
+ | cerr << " | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | |||
+ | for (;;) | ||
+ | { | ||
+ | // wait for a new frame from camera and store it into ' | ||
+ | cap.read(img_frame); | ||
+ | |||
+ | // check if we succeeded | ||
+ | if (img_frame.empty()) { | ||
+ | cerr << " | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | |||
+ | //HSV로 변환 | ||
+ | cvtColor(img_frame, | ||
+ | |||
+ | |||
+ | // | ||
+ | Mat img_mask1, img_mask2; | ||
+ | inRange(img_hsv, | ||
+ | if (range_count == 2) { | ||
+ | inRange(img_hsv, | ||
+ | img_mask1 |= img_mask2; | ||
+ | } | ||
+ | |||
+ | |||
+ | // | ||
+ | erode(img_mask1, | ||
+ | dilate(img_mask1, | ||
+ | |||
+ | |||
+ | // | ||
+ | dilate(img_mask1, | ||
+ | erode(img_mask1, | ||
+ | |||
+ | |||
+ | // | ||
+ | Mat img_labels, stats, centroids; | ||
+ | int numOfLables = connectedComponentsWithStats(img_mask1, | ||
+ | stats, centroids, 8, CV_32S); | ||
+ | |||
+ | |||
+ | // | ||
+ | int max = -1, idx = 0; | ||
+ | for (int j = 1; j < numOfLables; | ||
+ | int area = stats.at< | ||
+ | if (max < area) | ||
+ | { | ||
+ | max = area; | ||
+ | idx = j; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | int left = stats.at< | ||
+ | int top = stats.at< | ||
+ | int width = stats.at< | ||
+ | int height = stats.at< | ||
+ | |||
+ | int x = left + width / 2; | ||
+ | int y = top + height / 2; | ||
+ | |||
+ | int original_x = centroids.at< | ||
+ | int original_y = centroids.at< | ||
+ | |||
+ | |||
+ | circle(img_frame, | ||
+ | |||
+ | |||
+ | cout << "Ball position X = " << x // x position of center point of circle | ||
+ | << ",\tY = " << y // y position of center point of circle | ||
+ | << ", | ||
+ | <<" | ||
+ | <<" | ||
+ | << " | ||
+ | |||
+ | |||
+ | |||
+ | imshow(" | ||
+ | imshow(" | ||
+ | |||
+ | |||
+ | if (waitKey(5) >= 0) | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== Explanation ==== | ||
+ | \\ | ||
+ | \\ | ||
+ | Above project is made by three big parts. | ||
+ | \\ | ||
+ | First, detect color by setting up curtain hue value. | ||
+ | \\ | ||
+ | Second, clarify detected color by change the image to binary image. | ||
+ | \\ | ||
+ | Last, labeling that color. | ||
+ | \\ | ||
+ | \\ | ||
+ | Detecting and clarifying was hard for me to understand how it works. | ||
+ | \\ | ||
+ | \\ | ||
+ | In this tutorial, I will focus on drawing a circle around the color and finding its position. | ||
+ | \\ | ||
+ | \\ | ||
+ | In this part, | ||
+ | \\ | ||
+ | < | ||
+ | int left = stats.at< | ||
+ | int top = stats.at< | ||
+ | int width = stats.at< | ||
+ | int height = stats.at< | ||
+ | </ | ||
+ | \\ | ||
+ | " | ||
+ | \\ | ||
+ | which means we can find radius and center of circle. | ||
+ | \\ | ||
+ | \\ | ||
+ | < | ||
+ | int original_x = centroids.at< | ||
+ | int original_y = centroids.at< | ||
+ | </ | ||
+ | \\ | ||
+ | This function let us know the origin point of images. | ||
+ | \\ | ||
+ | < | ||
+ | circle(img_frame, | ||
+ | </ | ||
+ | \\ | ||
+ | This function draw a circle around the color. | ||
+ | \\ | ||
+ | < | ||
+ | cout << "Ball position X = " << x // x position of center point of circle | ||
+ | << ",\tY = " << y // y position of center point of circle | ||
+ | << ", | ||
+ | <<" | ||
+ | <<" | ||
+ | << " | ||
+ | |||
+ | </ | ||
+ | \\ | ||
+ | This will show current position and radius of circle constantly in the cmd window. | ||
+ | \\ | ||
+ | |||
+ | ==== Final Words ==== | ||
+ | |||
+ | |||
+ | For questions, clarifications, | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
color_detecting.txt · Last modified: 2019/06/21 05:58 by myeonggeonkim