Optimisation de Basic Global Thresholding
Bonjour à tous,
voici une fonction basique qui permet de déterminer automatiquement une valeur de seuil pour une image... Elle s'exécute actuellement en 8ms... J'aimerais l'optimiser au maximum (que ce soit au niveau de l'algo mais aussi -et surtout- au niveau du code c++)... Pourriez-vous me conseiller ? Voici mon code (l'image de départ est stockée dans m_pImageRGB):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| void CThresholding::AutoThresholding(int type)
{
width = m_pImageRGB->width ;
height = m_pImageRGB->height ;
m_pImageThreshold = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
m_pImageGray = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
// Conversion RGB->Grayscale
cvCvtColor(m_pImageRGB, m_pImageGray, CV_BGR2GRAY);
// Get the Histogramm of the image
hist_size = 256;
m_pImageSmooth = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
cvSmooth(m_pImageGray,m_pImageSmooth,CV_GAUSSIAN,3,3);
m_pHistogram = cvCreateHist(1, &hist_size, CV_HIST_ARRAY);
cvCalcHist(&m_pImageSmooth,m_pHistogram);
int T = BasicGlobalThresholding(m_pHistogram,
cvThreshold(m_pImageGray, m_pImageThreshold, T, 255, CV_THRESH_BINARY);
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| int CThresholding::BasicGlobalThresholding(CvHistogram* pHisto,int histSize)
{
int mean = GetAverageIntensity(pHisto,0,histSize);
int T1 = mean ;
int m1 = GetAverageIntensity(pHisto, 0, T1+1) ;
int m2 = GetAverageIntensity(pHisto, T1+1, histSize) ;
int T2 = (m1+m2)/2 ;
while (T2-T1>=2)
{
T1 = T2 ;
m1 = GetAverageIntensity(pHisto, 0, T1+1) ;
m2 = GetAverageIntensity(pHisto, T1+1, histSize) ;
T2 = (m1+m2)/2 ;
}
return T2 ;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| int CThresholding::GetAverageIntensity(CvHistogram* Hist, int start, int end)
{
int mean = 0 ;
int count = 0 ;
int v ;
for (int i=start; i<end; i++)
{
v = cvQueryHistValue_1D(Hist, i);
mean += i * v ;
count += v ;
}
return (mean/count) ;
} |