可以有opencv 使用C++补全下面的函数/*** (1). implementing histogram equalization for image enhancement ***/ bool HistogramEqualization( IplImage* pSrcImg, //@pSrcImg : input gray image IplImage*& pDstImg, //@pDstImg : output histogram equalized gray image IplImage*& pSrcHistImg,//@pSrcHistImg: original histogram of the input IplImage*& pDstHistImg)//@pDstHistImg: equalized histogram of the output { if (!pSrcImg) return false; //create two histogram images pSrcHistImg = cvCreateImage(cvSize(256, 256), 8, 1); pDstHistImg = cvCreateImage(cvSize(256, 256), 8, 1); /*** YOUR CODE HERE ***/ //[50pts] return true; }
时间: 2023-05-29 22:04:38 浏览: 66
pDstImg //@pDstImg : output gray image with equalized histogram ) {
// check if input image is grayscale
if (pSrcImg->nChannels != 1) {
return false;
}
int hist[256] = {0};
int cum_hist[256] = {0};
int max_intensity = 255;
int total_pixels = pSrcImg->width * pSrcImg->height;
// calculate histogram of input image
for (int i = 0; i < pSrcImg->height; i++) {
uchar* row_ptr = (uchar*)(pSrcImg->imageData + i * pSrcImg->widthStep);
for (int j = 0; j < pSrcImg->width; j++) {
hist[row_ptr[j]]++;
}
}
// calculate cumulative histogram
cum_hist[0] = hist[0];
for (int i = 1; i <= max_intensity; i++) {
cum_hist[i] = cum_hist[i-1] + hist[i];
}
// calculate equalized histogram
for (int i = 0; i <= max_intensity; i++) {
cum_hist[i] = round(((float)cum_hist[i] / total_pixels) * max_intensity);
}
// apply equalized histogram to input image to get output image
for (int i = 0; i < pSrcImg->height; i++) {
uchar* src_row_ptr = (uchar*)(pSrcImg->imageData + i * pSrcImg->widthStep);
uchar* dst_row_ptr = (uchar*)(pDstImg->imageData + i * pDstImg->widthStep);
for (int j = 0; j < pSrcImg->width; j++) {
dst_row_ptr[j] = cum_hist[src_row_ptr[j]];
}
}
return true;
}