c++ - Finding Nearest Pixel Value with OpenCV -
i want find nearest (r, g, b) values in frame using c++ using opencv. (r, g, b) values given. there function or solution in opencv? searched on internet couldn't find. ordinary solution causes memory problems , works slow because want find 256 different colors. suggestion or code helpful?
below, find code snippet calculates l1 distance between rgb color values of each pixel , rgb value for. then, returns location of pixel minimum distance.
this code example, based on l1 distance between rgb values. distance does not reflect human perception of color difference. there choice of distances should use instead if find pixel similar human eye. check out wikipedia page on color difference list of such distances. once decide on one, easy modify code below use distance instead of l1. if application simple, might able away code below is.
// rgb values of color looking int r = 0; int g = 0; int b = 255; // load image cv::mat img = cv::imread("image.png"); // split image channels cv::mat channels[3]; cv::split(img, channels); // find absolute differences each channel cv::mat diff_r; cv::absdiff(channels[2], r, diff_r); cv::mat diff_g; cv::absdiff(channels[1], g, diff_g); cv::mat diff_b; cv::absdiff(channels[0], b, diff_b); // calculate l1 distance cv::mat dist = diff_r + diff_g + diff_b; // find location of pixel minimum color distance cv::point minloc; cv::minmaxloc(dist, 0, 0, &minloc); // color of pixel @ minloc cout << img.at<cv::vec3b>(minloc) << endl;
Comments
Post a Comment