点击链接→【数字图像处理】简单实践汇总
1. 幂率变换
先将像素值缩放到0 ~ 255的范围,作为底数,当指数大于1的时候图片会变暗,指数小于1则会变亮!
srcImage = imread("src.jpg",0);
imshow(WIMDOW_NAME, srcImage);
Mat dImage = srcImage;
for (int i = 0; i < srcImage.rows; i++)
{
for (int j = 0; j < srcImage.cols; j++)
{
dImage.at<uchar>(i, j) = 255 * srcImage.at<uchar>(i, j) / 255
* srcImage.at<uchar>(i, j) / 255
* srcImage.at<uchar>(i, j) / 255;
}
}
imshow("幂律变换", dImage);
2. 直方图均衡
- 大多数自然图像,其灰度分布集中在较窄的区间,引起图像细节不够清晰,采用直方图修正后可使图像的灰度间距拉开或使灰度分布均匀,从而增大反差,使图像细节清晰,达到增强的目的。例如一幅过曝光的图片,其灰度级都集中在高亮度范围内,而曝光不足的图片,其灰度级集中在低亮度范围内,具有这样直方图的图片其可视效果比较差。
- 直方图均衡的步骤:
1.统计绘图级:
p r ( r k ) = n k n p_r(r_k)=\frac{n_k}{n} pr(rk)=nnk
2.计算累计分布曲线:
s k = T ( r k ) = ∑ j = 0 k p r ( r j ) = ∑ j = 0 k n j n s_k=T(r_k)=\sum ^k _{j=0} p_r(r_j)=\sum ^k _{j=0} \frac{n_j}{n} sk=T(rk)=j=0∑kpr(rj)=j=0∑knnj
3.用累积分布作为变换函数:
S ( k ) = i n t [ ( m a x ( r k ) − m i n ( r k ) ) ∙ s k + 0.5 ] S(k)=int[(max(r_k)-min(r_k))\bullet s_k + 0.5] S(k)=int[(max(rk)−min(rk))∙sk+0.5]
表中的数据是别的图的
srcImage = imread("2.jpg", 0);
imshow(WIMDOW_NAME, srcImage);
Mat dImage2 = srcImage;
int nmax = srcImage.rows * srcImage.cols;
int n[256] = { 0 };
double p[256] = { 0 };
double s[256] = { 0 };
int sk[256] = { 0 };
for (int i = 0; i < srcImage.rows; i++)
{
for (int j = 0; j < srcImage.cols; j++)
{
n[dImage2.at<uchar>(i, j)] += 1;
}
}
for (int i = 0; i < 256; i++)
{
p[i] = 1.0 * n[i] / nmax;
if(i == 0)
s[i] = p[i];
else
s[i] = s[i-1] + p[i];
sk[i] = 255 * s[i] + 0.5;
}
for (int i = 0; i < srcImage.rows; i++)
{
for (int j = 0; j < srcImage.cols; j++)
{
dImage2.at<uchar>(i, j) =sk[dImage2.at<uchar>(i, j)];
}
}
imshow("直方图均衡", dImage2);