我想使用C++和OpenCV找到帧中最接近给定(R, G, B)值的像素。是否有OpenCV中的函数或解决方案可用?我在互联网上搜索了,但没有找到。普通的解决方案会导致内存问题,并且速度很慢,因为我要查找256种不同的颜色。任何建议或代码都将非常有帮助。
我想使用C++和OpenCV找到帧中最接近给定(R, G, B)值的像素。是否有OpenCV中的函数或解决方案可用?我在互联网上搜索了,但没有找到。普通的解决方案会导致内存问题,并且速度很慢,因为我要查找256种不同的颜色。任何建议或代码都将非常有帮助。
// RGB values of the color you are looking for
int r = 0;
int g = 0;
int b = 255;
// Load image
cv::Mat img = cv::imread("image.png");
// Split image into channels
cv::Mat channels[3];
cv::split(img, channels);
// Find absolute differences for 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 the location of pixel with minimum color distance
cv::Point minLoc;
cv::minMaxLoc(dist, 0, 0, &minLoc);
// Get the color of a pixel at minLoc
cout << img.at<cv::Vec3b>(minLoc) << endl;