你好,我想通过邻域像素值来增强图像的对比度。
假设图像为u0。然后我想使用以下公式来增强图像:
这里,M1是邻域像素中u0的最小值,M2是最大值,Mg是原始图像的最大灰度级值。我的操作所采用的邻域大小为9X9。uN是新生成的图像(增强对比度的图像)。
我已经尝试了以下代码,但不确定是否正确。
%Generate a contrast enhanced image
tic
clear all; close all;
I = imread('4.jpg');
I = imresize(I,[128 128]);
if size(I,3)== 3
P = rgb2gray(uint8(I));
P = double(P);
elseif size(I,3) == 2
P = 0.5.*(double(I(:,:,1))+double(I(:,:,2)));
else
P = double(I);
end
ssize=9;
mg=max(P(:));
f1 = @(x) min(x(:));
m1 = nlfilter(P,[9 9],f1);
f2 = @(x) max(x(:));
m2 = nlfilter(P,[9 9],f2);
P_op=((P-m1)./(m2-m1)).*mg;
subplot(2,1,1),imagesc(P,[0 255]);colormap(gray);axis off;
subplot(2,1,2),imagesc(P_op,[0 255]);colormap(gray);axis off;
toc
以下是我得到的一些结果:
![enter image description here](https://istack.dev59.com/FhVXA.webp)
![enter image description here](https://istack.dev59.com/WttmZ.webp)
![enter image description here](https://istack.dev59.com/Me0JL.webp)
![enter image description here](https://istack.dev59.com/eDGMT.webp)
编辑后的问题:我重新阅读了工作内容,我必须将滑动窗口函数应用于仅指定的几个像素点。
我需要应用的像素点是通过以下方法找到的。首先检测图像的初始轮廓(在红色图像中显示)。然后在轮廓周围绘制一个指定距离的带状区域。原始图像中只需在该窄带内的那些像素上应用滑动窗口函数。
我提供了图像、初始轮廓和带状图像。
![enter image description here](https://istack.dev59.com/hJDU2.webp)
![enter image description here](https://istack.dev59.com/rG8W9.webp)
![enter image description here](https://istack.dev59.com/aY3og.webp)
![enter image description here](https://istack.dev59.com/ISFEQ.webp)
imdilate
和imerode
的灰度版本来计算每个像素的局部最大值/最小值。这可能比nlfilt
稍微快一些。 - Shainlfilt
不接受掩码。你可以做的是增强所有图像(就像你所做的那样),然后根据你计算出的掩码将增强的结果与原始图像合并。 - Shai