我試圖做一個基於中心的直方圖,這意味着在中間的RGB圖像的值更重要。我寫了下面的代碼,但根據OpenCV自己的直方圖函數,它非常慢。有什麼辦法可以讓這個速度變快嗎?基於OpenCV中心的直方圖
void calc_histogram(cv::Mat& image, cv::Mat& histogram)
{
int bit = 8;
int max_value = pow(2, bit);
int n_blue_bin = 8;
int n_green_bin = 8;
int n_red_bin = 8;
int blue_width = max_value/n_blue_bin;
int green_width = max_value/n_green_bin;
int red_width = max_value/n_red_bin;
int mySizes[3]={n_blue_bin, n_green_bin, n_red_bin};
histogram = Mat::zeros(3,mySizes,CV_32F);
int blue, green, red;
int blue_bin, green_bin, red_bin;
float k, length;
float max_length = sqrt((image.rows/2)*(image.rows/2) + (image.cols/2)*(image.cols/2));
for(int i = 0 ; i < image.rows ; i++)
{
for(int j = 0 ; j < image.cols ; j++)
{
length = sqrt(abs(image.rows/2 - i)*abs(image.rows/2 - i) + abs(image.cols/2 - j)*abs(image.cols/2 - j))/max_length;
k = sqrt(1-length);
Vec3b intensity = image.at<Vec3b>(i, j);
blue = intensity.val[0];
green = intensity.val[1];
red = intensity.val[2];
blue_bin = blue/blue_width;
green_bin = green/green_width;
red_bin = red/red_width;
histogram.at<float>(blue_bin, green_bin, red_bin) += k;
}
}
}
你是否分析了你的代碼?這通常是一個好的開始。我懷疑在你的內部循環中對'sqrt()'的兩次調用可能會成爲你的主要嫌疑人... –
@TobySpeight他們可能是,但我認爲他們是必要的。 –