int Vision::assign_color(Mat image, int x, int y) { Rect myROI( x - 3, y - 3, 7, 3); Mat imageIn = image(myROI); //imshow("Rect", imageIn); //waitKey(); // split the BGR image into individual channels Mat img_b(imageIn.rows, imageIn.cols, CV_8UC1); Mat img_g(imageIn.rows, imageIn.cols, CV_8UC1); Mat img_r(imageIn.rows, imageIn.cols, CV_8UC1); Mat channels[] = {img_b, img_g, img_r}; split(imageIn, channels); // compute the overall intensity for each pixel as (b + g + r) Mat intensity(imageIn.rows, imageIn.cols, CV_32F); add(img_b, img_g, intensity); add(intensity, img_r, intensity); intensity = intensity / 3.0; // compute the normalized color values for each channel Mat norm_b = img_b / intensity; Mat norm_g = img_g / intensity; Mat norm_r = img_r / intensity; // compute the average normalized color value of each channel double avg_b = cv::mean(norm_b)[0]; double avg_g = cv::mean(norm_g)[0]; double avg_r = cv::mean(norm_r)[0]; // print the color values to console std::printf("B: %f G: %f R: %f \n", avg_b, avg_g, avg_r); // define the reference color values double RED[] = {0.0, 0.5, 3.0}; double GREEN[] = {.5, 2.5, .0}; double BLUE[] = {2.5, 0.7, 0.0}; double YELLOW[] = {0.0, 2, 1}; double ORANGE[] = {0.0, 1.0, 2.0}; double WHITE[] = {2.0, 2.0, 2.0}; double YELLOW2[] = {0.0, 2.0, 2.0}; double ORANGE2[] = {0.0, 2.0, 3.0}; /* double RED[][3] = {{0.0, 1.0, 2.0},{0.0, 1.0, 2.0},{0.0, 0.3, 3.0},{0.0, 1.0, 2.0}}; double GREEN[][3] = {{.5, 2.5, .0},{.5, 2.5, .0} ,{.5, 2.5, .0},{.5, 2.5, .0}}; double BLUE[][3] = {{2.0, 1.0, 0.0}, {2.0, 1.0, 0.0}, {2.0, 1.0, 0.0}, {2.0, 1.0, 0.0}}; double YELLOW[][3] = {{0.0, 2.0, 1.0}, {0.0, 2.0, 1.0}, {0.0, 3.0, 3.0}, {0.0, 2.0, 1.0}}; double ORANGE[][3] = {{0.0, 2.0, 3.0}, {0.0, 1.0, 2.5}, {0.0, 1.0, 3.0}, {0.0, 1.0, 3.0}}; double WHITE[][3] = {{2.0, 2.0, 2.0}, {1.0, 1.0, 1.0}, {2.0, 2.0, 2.0}, {1.0, 1.0, 1.0}}; */ // compute the squerror relative to the reference color values double minError = 10.0; double errorSqr; char bestFit = 'x'; // int bestFit = 9999; // check RED fitness errorSqr = normSqr(RED[0], RED[1], RED[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_RED; } // check GREEN fitness errorSqr = normSqr(GREEN[0], GREEN[1], GREEN[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_GREEN; } // check BLUE fitness errorSqr = normSqr(BLUE[0], BLUE[1], BLUE[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_BLUE; } // check YELLOW fitness errorSqr = normSqr(YELLOW[0], YELLOW[2], YELLOW[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_YELLOW; } errorSqr = normSqr(YELLOW2[0], YELLOW2[2], YELLOW2[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_YELLOW; } // check ORANGE fitness errorSqr = normSqr(ORANGE[0], ORANGE[1], ORANGE[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_ORANGE; } errorSqr = normSqr(ORANGE2[0], ORANGE2[1], ORANGE2[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_ORANGE; } // check WHITE fitness errorSqr = normSqr(WHITE[0], WHITE[1], WHITE[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_WHITE; } errorSqr = normSqr(1.0, 1.0, 1.0, avg_b, avg_g, avg_r); if(errorSqr < minError && abs(avg_b - avg_r) < .3 && abs(avg_g - avg_r < .3) && abs(avg_g - avg_b < .3)) { minError = errorSqr; bestFit = COLOR_WHITE; } // return the best fit color label return bestFit; }
/*********************************************************************************************************************** * @BRIEF Process a single image frame * @PARAM[in] imageIn the input image region of interest * @RETURN the label char describing the ROI color * @AUTHOR Christopher D. McMurrough **********************************************************************************************************************/ char labelColor(const cv::Mat &imageIn) { // split the BGR image into individual channels cv::Mat img_b(imageIn.rows, imageIn.cols, CV_8UC1); cv::Mat img_g(imageIn.rows, imageIn.cols, CV_8UC1); cv::Mat img_r(imageIn.rows, imageIn.cols, CV_8UC1); cv::Mat channels[] = {img_b, img_g, img_r}; cv::split(imageIn, channels); // compute the overall intensity for each pixel as (b + g + r) cv::Mat intensity(imageIn.rows, imageIn.cols, CV_32F); cv::add(img_b, img_g, intensity); cv::add(intensity, img_r, intensity); intensity = intensity / 3.0; // compute the normalized color values for each channel cv::Mat norm_b = img_b / intensity; cv::Mat norm_g = img_g / intensity; cv::Mat norm_r = img_r / intensity; // compute the average normalized color value of each channel double avg_b = cv::mean(norm_b)[0]; double avg_g = cv::mean(norm_g)[0]; double avg_r = cv::mean(norm_r)[0]; // print the color values to console std::printf("B: %f G: %f R: %f \n", avg_b, avg_g, avg_r); // define the reference color values double RED[] = {0.4, 0.5, 1.8}; double GREEN[] = {1.0, 1.2, 1.0}; double BLUE[] = {1.75, 1.0, 0.5}; double YELLOW[] = {0.82, 1.7, 1.7}; double ORANGE[] = {0.2, 1.0, 2.0}; double WHITE[] = {2.0, 1.7, 1.7}; // compute the squerror relative to the reference color values double minError = 3.0; double errorSqr; char bestFit = 'x'; // check RED fitness errorSqr = normSqr(RED[0], RED[1], RED[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_RED; } // check GREEN fitness errorSqr = normSqr(GREEN[0], GREEN[1], GREEN[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_GREEN; } // check BLUE fitness errorSqr = normSqr(BLUE[0], BLUE[1], BLUE[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_BLUE; } // check YELLOW fitness errorSqr = normSqr(YELLOW[0], YELLOW[1], YELLOW[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_YELLOW; } // check ORANGE fitness errorSqr = normSqr(ORANGE[0], ORANGE[1], ORANGE[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_ORANGE; } // check WHITE fitness errorSqr = normSqr(WHITE[0], WHITE[1], WHITE[2], avg_b, avg_g, avg_r); if(errorSqr < minError) { minError = errorSqr; bestFit = COLOR_WHITE; } // return the best fit color label return bestFit; }