matlab 输入图像 分别生成输入图像的R、G、B三通道直方图 对上一步每个通道的直方图,使用经过m值限定后的直方图分布范围来计算新的曝光值 利用曝光值计算分割点X_a,将每个直方图分割成两个子直方图,并显示
时间: 2024-05-29 18:11:37 浏览: 60
用matlab实现对图像的分割
5星 · 资源好评率100%
以下是用MATLAB实现的代码:
% 读入图像
img = imread('input.jpg');
% 分离R、G、B三个通道
R = img(:,:,1);
G = img(:,:,2);
B = img(:,:,3);
% 计算R通道直方图
r_hist = imhist(R);
r_hist = r_hist / sum(r_hist);
% 计算G通道直方图
g_hist = imhist(G);
g_hist = g_hist / sum(g_hist);
% 计算B通道直方图
b_hist = imhist(B);
b_hist = b_hist / sum(b_hist);
% 经过m值限定后的直方图分布范围
m = 0.005;
r_hist_range = find(r_hist > m);
g_hist_range = find(g_hist > m);
b_hist_range = find(b_hist > m);
% 计算新的曝光值
r_exposure = sum(r_hist_range) / length(r_hist_range);
g_exposure = sum(g_hist_range) / length(g_hist_range);
b_exposure = sum(b_hist_range) / length(b_hist_range);
% 计算分割点Xa
Xa_r = round(r_exposure * 255);
Xa_g = round(g_exposure * 255);
Xa_b = round(b_exposure * 255);
% 分割R通道直方图
r_hist_left = r_hist(1:Xa_r);
r_hist_right = r_hist(Xa_r+1:end);
% 分割G通道直方图
g_hist_left = g_hist(1:Xa_g);
g_hist_right = g_hist(Xa_g+1:end);
% 分割B通道直方图
b_hist_left = b_hist(1:Xa_b);
b_hist_right = b_hist(Xa_b+1:end);
% 显示分割后的直方图
subplot(2,3,1);imshow(img);title('Input Image');
subplot(2,3,2);imhist(R);title('R Channel Histogram');
subplot(2,3,3);imhist(G);title('G Channel Histogram');
subplot(2,3,4);imhist(B);title('B Channel Histogram');
subplot(2,3,5);plot(r_hist_left);hold on;plot(r_hist_right);title('R Channel Split Histogram');
subplot(2,3,6);plot(g_hist_left);hold on;plot(g_hist_right);title('G Channel Split Histogram');
subplot(2,3,7);plot(b_hist_left);hold on;plot(b_hist_right);title('B Channel Split Histogram');
运行上述代码,将会生成一个包含输入图像和三个通道直方图的窗口,以及三个子窗口,每个子窗口包含分割后的两个子直方图。其中,X轴表示颜色值,Y轴表示像素数量,左边的子直方图表示颜色值小于分割点的像素数量,右边的子直方图表示颜色值大于等于分割点的像素数量。
阅读全文