Matlab 彩色图片直方图均衡化处理 line()函数实现

好了,不说这么多了,直奔主题,本次分别做了两次实现,分别是:彩色图片转灰度图直方图均衡化处理以及彩色图片的直方图均衡化处理,大家可以按需观看。

一、程序源码
(1)彩色图片转灰度图直方图均衡化处理:

%读取图片
I=imread('test.jpg');
%将图片转化成灰度图片
I=rgb2gray(I);
%显示原图像
figure(4),imshow(I);
%获得灰度图的长和宽
[M,N]=size(I);

%做直方图均衡化处理,设置初始参数
average=0;

%做直方图均衡化处理,创建映射区域
A=zeros(1,256);

%获得每一个点的灰度值,并判断其值等于多少
for k=1:256
    count=0; 
   for i=1:M   
     for j=1:N
        value=I(i,j);
        %统计灰度值
        if value==k
            count=count+1;
        end
     end  
   end
   %画出图像灰度直方图
   figure(1),line([k,k],[0,count]);
   %直方图均衡化处理
   count=count/(M*N*1.0);
   average=average+count;
   %获得灰度映射
   A(k)=average;
   %画出均衡化后的直方图
   figure(2),line([k,k],[0,average]);
end


%累计分布取整,+0.5等于四舍五入,也就是取整
A=uint8(255.*A+0.5);
%进行直方图反映射,得到处理后的原图像
for i=1:M
    for j=1:N
        I(i,j)=A(I(i,j));
    end
end

%展示处理后图像
figure(3),imshow(I);

实验结果截图:
这里写图片描述

(2)彩色图片的直方图均衡化处理
彩色图片的直方图均衡化处理与灰度图的处理方法一样,不同的地方在于要分别把R、G、B层做一次处理,也就是处理三次,而灰度图像就只需要处理灰度值就行,这就是两者唯一的不同。因此,原理懂了,就不难实现了

%读取图片
I=imread('test.jpg');
figure(7),imshow(I);
%获取图片的长、宽和层数(其实等于三,R、G、B三层)
[M,N,G]=size(I);
result=zeros(M,N,3);
%获得每一层每一个点的RGB值,并判断其值等于多少
for g=1:3
    A=zeros(1,256);
    %每处理完一层,参数要重新初始化为0
    average=0;
    for k=1:256
        count=0;
        for i=1:M
            for j=1:N
                value=I(i,j,g);
                if value==k
                    count=count+1;
                end
            end
        end
        figure(g),line([k,k],[0,count]);
        count=count/(M*N*1.0);
        average=average+count;
        A(k)=average;
        figure(g+3),line([k,k],[0,average]);
    end
    A=uint8(255.*A+0.5);
    for i=1:M
        for j=1:N
            I(i,j,g)=A(I(i,j,g)+0.5);
        end
    end  
end
%展示处理效果
figure(8),imshow(I);

实验结果截图:
这里写图片描述

以上,就是如何运用line()函数来画直方图以及如何通过对直方图进行处理来处理图片的运用啦。个人觉得不足的地方在于for循环用的有些多,程序看起来有点臃肿,希望有大神能够指点一二。

  • 9
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的 MATLAB GUI 代码示例,用于对图像进行直方图均衡化: ```matlab function varargout = histogram_equalization_gui(varargin) % HISTOGRAM_EQUALIZATION_GUI MATLAB code for histogram_equalization_gui.fig % HISTOGRAM_EQUALIZATION_GUI, by itself, creates a new HISTOGRAM_EQUALIZATION_GUI or raises the existing % singleton*. % % H = HISTOGRAM_EQUALIZATION_GUI returns the handle to a new HISTOGRAM_EQUALIZATION_GUI or the handle to % the existing singleton*. % % HISTOGRAM_EQUALIZATION_GUI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in HISTOGRAM_EQUALIZATION_GUI.M with the given input arguments. % % HISTOGRAM_EQUALIZATION_GUI('Property','Value',...) creates a new HISTOGRAM_EQUALIZATION_GUI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before histogram_equalization_gui_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to histogram_equalization_gui_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help histogram_equalization_gui % Last Modified by GUIDE v2.5 30-Apr-2021 10:28:09 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @histogram_equalization_gui_OpeningFcn, ... 'gui_OutputFcn', @histogram_equalization_gui_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before histogram_equalization_gui is made visible. function histogram_equalization_gui_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to histogram_equalization_gui (see VARARGIN) % Choose default command line output for histogram_equalization_gui handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes histogram_equalization_gui wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = histogram_equalization_gui_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Load an image from file [filename, pathname] = uigetfile({'*.bmp;*.jpg;*.png;*.gif','All Image Files';... '*.*','All Files' },'Select an image file'); if filename ~= 0 handles.I = imread(fullfile(pathname, filename)); axes(handles.axes1); imshow(handles.I); set(handles.pushbutton2,'Enable','on'); end guidata(hObject, handles); % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Convert image to grayscale I = rgb2gray(handles.I); % Perform histogram equalization J = histeq(I); % Display results axes(handles.axes2); imshow(J); guidata(hObject, handles); ``` 此代码使用MATLAB GUI,其中包含两个按钮和两个图像框。 第一个按钮(`pushbutton1`)用于加载图像文件,第二个按钮(`pushbutton2`)用于执行直方图均衡化并显示结果。 在 `pushbutton2` 回调函数中,首先将 RGB 图像转换为灰度图像,然后使用 `histeq` 函数执行直方图均衡化,并将结果显示在第二个图像框中。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值