数字图像处理——灰度图像(视频)转化伪彩色图像(视频)(含源代码)

目录

一,整体框架

指定图像或视频构建索引表Byrgbgetindex

通过构建的索引表进行伪彩色转换byindexgetrgb

通过函数映射进行伪彩色转换gray_differup2rgb

通过密度分割将灰度进行映射midufenge

GUI界面 last

二,处理方法介绍

函数结构

Byrgbgetindex

输入内容

Rgb_img

Bili

Junyuncha

Xianyancha

输出内容

Gray_Index

MissingNumbers

junhendijige

Xianyandijige

方法与流程

输入参数规范化

构建源颜色表

根据输入的判定参数遍历顺序遍历整个源颜色表​编辑​编辑​编辑

缺失灰度值用距离最近的灰度代替​编辑​编辑​编辑

将最终索引表按照灰度值升序排序​编辑​编辑​编辑

Byindexgetrgb

输入内容

输入内容

处理过程

Gray_differup2rgb

输入内容

输出内容

流程

Miudfenge

输入内容

输出内容

过程

三,GUI介绍

区域划分

效果展示

四,源代码

byrgbgetindex

byindexgetrgb

gray_differup2rgb

midufenge

last


一,整体框架

  1. 指定图像或视频构建索引表Byrgbgetindex
  2. 通过构建的索引表进行伪彩色转换byindexgetrgb
  3. 通过函数映射进行伪彩色转换gray_differup2rgb
  4. 通过密度分割将灰度进行映射midufenge
  5. GUI界面 last
  • 二,处理方法介绍

    1. 函数结构
      1. 输入参数格式调整(将字符串转化为数字,将彩色图像转化为灰度图像)
      2. 处理参数返回结果
    2. Byrgbgetindex
      1. 输入内容
        1. Rgb_img
          1. 一张RGB图像(兼容了灰度图像
        2. Bili
          1. 根据自定义标准,筛选出最终颜色的鲜艳与均匀占比
          2. 1/(bili+1)为鲜艳颜色
        3. Junyuncha
          1. 筛选颜色用到的自定义评判标准,GValue-GrayValue小于junyuncha的认为此颜色满足均匀标准。
          2. 经过实践junyuncha设置为10-20效果更好
        4. Xianyancha
          1. 按-R-G-B-R-的顺序,挑选已挑选数量非最大的颜色,保证偏R,偏G,偏B的颜色相对均匀。差值比例大于鲜艳差的认为满足鲜艳标准
          2. 经过实践鲜艳差设置为0.5-0.8效果更好
      2. 输出内容
        1. Gray_Index
          1. 根据输入图像构建出的索引表
        2. MissingNumbers
          1. 完成筛选后,没有对应RGB组合的灰度值
          2. 筛选条件越苛刻Missing_Gray的数量越多
        3. junhendijige
          1. 最后被认定为均衡并加入索引表的颜色在源颜色表的序号
          2. 这个序号越接近源颜色表的总长,认为筛选条件设置越合理
        4. Xianyandijige
          1. 最后被认定为鲜艳并加入索引表的颜色在源颜色表的序号
          2. 这个序号越接近颜色表的总长,认为筛选条件设置的越合理
      3. 方法与流程
        1. 输入参数规范化
          1. 格式转换
        2. 构建源颜色表
          1. 将RGB通道颜色聚合为128个等级值
          2. 统计颜色组合及其出现次数
          3. 统计chayis(单组rgb的最大值与最小值的差异)
          4. 构建表最终结果按照差异降序,出现次数升序排序
        3. 根据输入的判定参数遍历顺序遍历整个源颜色表
        4. 缺失灰度值用距离最近的灰度代替
        5. 将最终索引表按照灰度值升序排序
    3. Byindexgetrgb
      1. 输入内容
        1. Gray_img
          1. 灰度图像数据
        2. Index
          1. 颜色索引表
      2. 输入内容
        1. Rgb_img
          1. 根据灰度图像与索引表恢复的RGB图像
      3. 处理过程
        1. 输入格式规范
        2. 遍历图像内容,根据灰度值查找索引表对应的RGB组合填充RGB
        3. 对填充后的RGB通道分别进行中值滤波
    4. Gray_differup2rgb
      1. 输入内容
        1. GrayImg
          1. 灰度图像(兼容彩色图像)
        2. Mode
          1. 模式,1将高亮映射为暖色,0低亮映射为暖色
      2. 输出内容
        1. Result_rgbImg
          1. 处理结果
      3. 流程
        1. 参数规范
        2. 按模式分别对灰度值进行函数映射
          1. 这里我们选用了线性的函数映射
    5. Miudfenge
      1. 输入内容
        1. Image
          1. 图片信息,兼容彩色图像
      2. 输出内容
        1. Colored_image
          1. 分割结果
      3. 过程
        1. 参数规范
        2. 计算每一段像素宽度
        3. 定义每一段色彩值
        4. 执行密度分割
  • 三,GUI介绍

    1. 区域划分
      1. 输入区域
      2. 处理区域
      3. 输出区域
    2. 效果展示
      1. 输入区域
        1. 点击上传图片或视频图像会直接显示,视频会显示第一帧
        2. 处理区域
          1. 列表选择处理方法
            1. 选择后点击处理
            2. 跳出弹窗指定参数这里我们默认了一组适用性比较广泛的参数
            3. 点击确定即可进行处理
        3. 输出区域
          1. 对于有结果需要展示的处理方法,会直接显示原图像与处理后的图像
          2. 图像与视频均可处理
  • 四,源代码

    1. byrgbgetindex
      1. function [gray_index,missingNumbers,junhendijige,xianyandijige,lenlong] = byrgbgetindex(rgb_img, bili ,junyuncha ,xianyancha)
        
        [~,~,k] = size(rgb_img);
        if k == 1
            rgb_img(:,:,1)=rgb_img(:,:,1);
            rgb_img(:,:,2)=rgb_img(:,:,2);
            rgb_img(:,:,3)=rgb_img(:,:,3);
        end
        
        if ischar(bili)
            bili = str2double(bili);
        end
        if ischar(junyuncha)
            junyuncha = str2double(junyuncha);
        end
        if ischar(xianyancha)
            xianyancha = str2double(xianyancha);
        end
        b = reshape(rgb_img, [], 3);
        
        % 获取颜色组合和出现次数
        [colors, ~, index] = unique(b, 'rows');
        counts = accumarray(index, 1);
        
        % 计算灰度值
        grayValues = 0.2989 * colors(:, 1) + 0.5870*colors(:, 2) + 0.1140*colors(:, 3);
        
        %%   调整创建初始索引表,
        % 根据出现次数进行排序
        [sortedCounts, idx] = sort(counts, 'descend');
        sortedColors = colors(idx, :);
        sortedGrayValues = grayValues(idx);
        
        
        % 由于单凭出现次数建立的索引表得到的图像效果很差,所以为了使色彩更加鲜艳我以色彩差异(算是半个饱和度吧)为主建立了新的灰度预选表
        maxValues = max(sortedColors, [], 2);
        minValues = min(sortedColors, [], 2);
        chayis = maxValues-minValues; %计算差异大小
        [sortedfchayis, idxchayi] = sort(chayis, 'descend');
        chayisortedCounts = sortedCounts(idxchayi, :);
        chayisortedColors = sortedColors(idxchayi, :);
        chayisortedGrayValues = sortedGrayValues(idxchayi);
        newTable = table(sortedfchayis, chayisortedCounts, chayisortedColors(:, 1), chayisortedColors(:, 2), chayisortedColors(:, 3), chayisortedGrayValues, 'VariableNames', {'chayis', 'Counts', 'R', 'G', 'B', 'GrayValue'});
        newTable = sortrows(newTable, {'chayis', 'Counts'}, {'descend', 'descend'});
        
        
        % RGB聚合为128个等级 此时
        % 将RGB聚合(奇数减一,偶数不变)
        
        % 128分界线
        newTable.R = newTable.R - mod(newTable.R, 2);
        newTable.G = newTable.G - mod(newTable.G, 2);
        newTable.B = newTable.B - mod(newTable.B, 2);
        
        
        newTable.GrayValue = 0.2989 * newTable.R + 0.5870*newTable.G + 0.1140*newTable.B;
        
        
        
        %%  筛选最终索引表
        % 初始化结果列表和已选灰度值列表  
        selectedRGB = [];
        selectedGray = [];
        
        usedGrays = [];
        
        
        % 选择灰度值
        % 优化,均匀选择,一半G均衡为主为主,剩下的一半,RGB鲜艳各占一份
        % 标记 santi ={0,取均衡,1,取鲜艳}
        %      diqiu ={0,红,1,绿,2蓝}
        santi = 0;
        santinum = 0;
        junhendijige = 0;
        xianyandijige = 0;
        rnum = 0;
        gnum = 0;
        bnum = 0;
        lenlong = height(newTable);
        for i = 1:1:lenlong
            % 取出当前 rgb 对应的灰度值
            gray = newTable.GrayValue(i);
            
            % 检查灰度是否已被选择
            if ismember(gray, usedGrays)
                continue
            end
            
            if santi < bili
                if abs( newTable.G(i)-newTable.GrayValue(i)) <= junyuncha % 满足条件的被选中
                    % 将灰度添加到已选列表中
                    usedGrays = [usedGrays; gray];
                    % 将 RGB 添加到结果列表中
                    selectedRGB = [selectedRGB; newTable.R(i), newTable.G(i), newTable.B(i)];
                    selectedGray = [selectedGray; gray];
                    santi = santi + 1; %santi 置1 下次选鲜艳
                    santinum = santinum + 1;
                    junhendijige = i;
                    continue;
                end
            end
            if santi == bili
                if newTable.R(i)/newTable.G(i) - 1 >= xianyancha && rnum-gnum <= 0 && rnum-bnum <= 0  %满足条件被选中 
                    % 将灰度添加到已选列表中
                    usedGrays = [usedGrays; gray];
                    % 将 RGB 添加到结果列表中
                    selectedRGB = [selectedRGB; newTable.R(i), newTable.G(i), newTable.B(i)];
                    selectedGray = [selectedGray; gray];
                    santi = 0; %santi 置0 下次选均匀
                    rnum = rnum+1;
                    xianyandijige = i;
                    continue;
                elseif newTable.G(i)/newTable.B(i) - 1 >= xianyancha && gnum-bnum <= 0 && gnum-rnum <= 0  %满足条件被选中
                    % 将灰度添加到已选列表中
                    usedGrays = [usedGrays; gray];
                    % 将 RGB 添加到结果列表中
                    selectedRGB = [selectedRGB; newTable.R(i), newTable.G(i), newTable.B(i)];
                    selectedGray = [selectedGray; gray];
                    santi = 0; %santi 置0 下次选均匀
                    gnum = gnum+1;
                    xianyandijige = i;
                    continue;
                elseif newTable.B(i)/newTable.R(i) - 1 >= xianyancha && bnum-rnum <= 0 && bnum-gnum <= 0  %满足条件被选中
                    % 将灰度添加到已选列表中
                    usedGrays = [usedGrays; gray];
                    % 将 RGB 添加到结果列表中
                    selectedRGB = [selectedRGB; newTable.R(i), newTable.G(i), newTable.B(i)];
                    selectedGray = [selectedGray; gray];
                    santi = 0; %santi 置0 下次选均匀
                    bnum = bnum+1;
                    xianyandijige = i;
                    continue;
                end
            end
        
        
        end
        
        
        % 创建包含 0 到 255 的数组
        allNumbers = 0:255;
        
        % 找出 selectedRGB 中存在的数字
        presentNumbers = unique(selectedGray);
        
        % 找出缺失的数字
        missingNumbers = setdiff(allNumbers, presentNumbers);
        
        % 用距离最近的映射关系代替缺失的灰度值 
        for i = 1:length(missingNumbers)
            for j = 1:length(missingNumbers)*2
                
                if mod(j,2) == 0 
                    j = -j/2;
                else
                    j = (j+1)/2;
                end
                if missingNumbers(i)+j < 0
                    continue;
                end
                
                if ismember(missingNumbers(i)+j, selectedGray)
                    selectedGray = [selectedGray; missingNumbers(i)];
                    findout = find(selectedGray == missingNumbers(i)+j);
                    selectedRGB = [selectedRGB; selectedRGB(findout,:)];
                    break;
                end
            end
        end
        
        gray_index = table(selectedRGB(:, 1), selectedRGB(:, 2), selectedRGB(:, 3), selectedGray, 'VariableNames', {'R', 'G', 'B', 'Gray'});
        gray_index = sortrows(gray_index, 'Gray', 'ascend');
        
        
        end
        
        
        

                

    2. byindexgetrgb
      1. function [rgb_img] = byindexgetrgb(gray_img, index)
        [row, col, k] = size(gray_img);
        rgb_img = zeros(row, col, 3, 'uint8');
        if k == 3
            gray_img = rgb2gray(gray_img);
        end
        array_table = table2array(index);
        gray_img_index = gray_img + 1;
        
        linear_index = sub2ind(size(array_table), gray_img_index(:));
        rgb_values = array_table(linear_index, :);
        
        rgb_img(:,:,1) = reshape(rgb_values(:, 1), row, col);
        rgb_img(:,:,2) = reshape(rgb_values(:, 2), row, col);
        rgb_img(:,:,3) = reshape(rgb_values(:, 3), row, col);
        rgb_img(:,:,1) = medfilt2(rgb_img(:,:,1), [3,3]);
        rgb_img(:,:,2) = medfilt2(rgb_img(:,:,2), [3,3]);
        rgb_img(:,:,3) = medfilt2(rgb_img(:,:,3), [3,3]);
        
        end
        

                

    3. gray_differup2rgb
      1. %% 
        %   适用于医学便于观察——差异最大化的灰度图像转换
        %       关注低亮度区域时:低亮度映射为红色,高亮度映射为蓝色;mode = 0
        %       关注高亮度区域时:高亮度映射为红色,低亮度映射为蓝色;mode - 1
        %	分别用三个矩阵保存,三个通道的灰度值。每个通道的灰度值,有不同的映射函数。
        % 输入灰度级        输出彩色       
        % 0~50  1/5         蓝色        红色
        % 51~102 2/5        蓝色+绿色   绿色+红色
        % 103~153 3/5       绿色        绿色
        % 154~204  4/5      绿色+红色   蓝色+绿色
        % 205~255   5/5     红色        蓝色
        %% 
        % 
        % function [results_rgbImg] = gray_differup2rgb(grayImg,mode)
        % 
        % [row,col]=size(grayImg);
        % R = zeros(row,col);
        % G = zeros(row,col);
        % B = zeros(row,col);
        % 
        % 
        % if mode == 1
        %     for i = 1:row
        %         for j = 1:col
        %             if grayImg(i,j)<=50 % [0,50]
        %                 R(i,j)=0;
        %                 G(i,j)=0;
        %                 B(i,j)=255;
        %                 
        %             elseif grayImg(i,j)<=102 % (51,102] 
        %                 R(i,j)=0;
        %                 G(i,j)=5*(grayImg(i,j)-50);
        %                 B(i,j)=255-5*(grayImg(i,j)-50);
        %             elseif grayImg(i,j)<=153% (103, 153]
        %                 R(i,j)=0;
        %                 G(i,j)=255;
        %                 B(i,j)=0;
        %              
        %             elseif grayImg(i,j)<=204% (154, 204]
        %                 R(i,j)=5*(grayImg(i,j)-153);
        %                 G(i,j)=255-5*(grayImg(i,j)-153);
        %                 B(i,j)=0;
        %          
        %             else              % (205,255]
        %                 R(i,j)=255;
        %                 G(i,j)=0;
        %                 B(i,j)=0;
        %             end
        %         end
        %     end
        %     
        %     results_rgbImg = zeros(row,col);
        %     for i=1:row
        %         for j=1:col
        %               results_rgbImg(i,j,1)=R(i,j);
        %               results_rgbImg(i,j,2)=G(i,j);
        %               results_rgbImg(i,j,3)=B(i,j);
        %         end
        %     end
        %  
        %     results_rgbImg=results_rgbImg/256;
        % elseif mode == 0
        %     for i = 1:row
        %         for j = 1:col
        %             if grayImg(i,j)<=50 % [0,50]
        %                 
        %                 R(i,j)=255;
        %                 G(i,j)=0;
        %                 B(i,j)=0;
        %             elseif grayImg(i,j)<=102 % (51,102] 
        %                 
        %                 R(i,j)=5*(grayImg(i,j)-153);
        %                 G(i,j)=255-5*(grayImg(i,j)-153);
        %                 B(i,j)=0;
        %             elseif grayImg(i,j)<=153% (103, 153]
        %                 R(i,j)=0;
        %                 G(i,j)=255;
        %                 B(i,j)=0;
        %             elseif grayImg(i,j)<=204% (154, 204]
        %                 R(i,j)=0;
        %                 G(i,j)=5*(grayImg(i,j)-50);
        %                 B(i,j)=255-5*(grayImg(i,j)-50);
        % 
        %             else              % (205,255]
        %                 R(i,j)=0;
        %                 G(i,j)=0;
        %                 B(i,j)=255;
        %             end
        %         end
        %     end
        %     
        %     results_rgbImg = zeros(row,col);
        %     for i=1:row
        %         for j=1:col
        %               results_rgbImg(i,j,1)=R(i,j);
        %               results_rgbImg(i,j,2)=G(i,j);
        %               results_rgbImg(i,j,3)=B(i,j);
        %         end
        %     end
        %  
        %     results_rgbImg=results_rgbImg/256;
        %     
        % end
        % end
        %%    改进版本,速度有所提升
        function results_rgbImg = gray_differup2rgb(grayImg, mode)
        if ischar(mode)
            mode = str2double(mode);
        end
        [row, col, k] = size(grayImg);
        R = zeros(row, col);
        G = zeros(row, col);
        B = zeros(row, col);
        
        % 检查输入图像是否为灰度图像
        if k == 3
            grayImg = rgb2gray(grayImg);
        end
        
        
        
        if mode == 1
            % [0,50]
            idx = grayImg <= 50;
            R(idx) = 0;
            G(idx) = 0;
            B(idx) = 255;
            
            % (51,102]
            idx = (grayImg > 50) & (grayImg <= 102);
            G(idx) = 5 * (grayImg(idx) - 50);
            B(idx) = 255 - 5 * (grayImg(idx) - 50);
            
            % (103, 153]
            idx = (grayImg > 102) & (grayImg <= 153);
            G(idx) = 255;
            B(idx) = 0;
            
            % (154, 204]
            idx = (grayImg > 153) & (grayImg <= 204);
            R(idx) = 5 * (grayImg(idx) - 153);
            G(idx) = 255 - 5 * (grayImg(idx) - 153);
            
            % (205,255]
            idx = grayImg > 204;
            R(idx) = 255;
            G(idx) = 0;
            B(idx) = 0;
            
        elseif mode == 0
            % [0,50]
            idx = grayImg <= 50;
            R(idx) = 255;
            G(idx) = 0;
            B(idx) = 0;
            
            % (51,102]
            idx = (grayImg > 50) & (grayImg <= 102);
            R(idx) = 5 * (grayImg(idx) - 153);
            G(idx) = 255 - 5 * (grayImg(idx) - 153);
            
            % (103, 153]
            idx = (grayImg > 102) & (grayImg <= 153);
            G(idx) = 255;
            B(idx) = 0;
            
            % (154, 204]
            idx = (grayImg > 153) & (grayImg <= 204);
            G(idx) = 5 * (grayImg(idx) - 50);
            B(idx) = 255 - 5 * (grayImg(idx) - 50);
            
            % (205,255]
            idx = grayImg > 204;
            R(idx) = 0;
            G(idx) = 0;
            B(idx) = 255;
        end
        
        results_rgbImg = cat(3, R, G, B)/255;
        
        end
        

                

    4. midufenge
      1. 
        function [colored_image] = midufenge(image)
        %PINLUCHENGFEN 输入灰度图,选择成分:傅里叶变换滤除目标成分
        
        [~,~,k] = size(image);
        if k == 3
            image = rgb2gray(image);
        end
        
        
        gray_image_double = double(image)/255;
        [row, col] = size(gray_image_double);
        
        % 定义分割段数
        N = 5;
        % 计算灰度值的最大值和最小值
        max_value = max(max(gray_image_double));
        min_value = min(min(gray_image_double));
        % 计算每一段的像素值域宽度
        step = (max_value - min_value) / N;
        % 定义每一段的彩色值
        colors = [0 0 1; 0 1 1; 0 1 0; 1 1 0; 1 0 0]; % 蓝->青->绿->黄->红
        
        % 执行密度分割法
        colored_image = zeros(row, col, 3);
        for i = 1:N
            start_gray = min_value + (i - 1) * step;
            end_gray = min_value + i * step;
            
            % 找到属于当前段的像素值
            mask = (gray_image_double >= start_gray) & (gray_image_double < end_gray);
            
            
            % 将彩色图像赋值给对应的像素
            for j = 1:row
                for k = 1:col
                    if mask(j,k) == 1
                        colored_image(j,k,1) = colors(i,1);
                        colored_image(j,k,2) = colors(i,2);
                        colored_image(j,k,3) = colors(i,3);
                    end
                end
            end
            
        end
        
        end
        

                

    5. last
      1. myGUIApp();
        
        function myGUIApp()
            % 创建主窗口
            mainFig = figure('Name', 'My GUI App', 'Position', [200,200,800,600]);
            %%
            % 定义handles结构体,用于不同回调函数之间的变量共享
            handles = struct();
            handles.inputimg = []; % 输入图片
            handles.inputvid = []; % 输入视频
            handles.outimg = []; % 处理结果图片
            handles.outvid = []; % 处理结果视频
            handles.pathname = [];
            handles.outindex = [];
            handles.missingNumbers = [];
            handles.junhendijige = [];
            handles.xianyandijige = [];
            handles.lenlong = [];
            %%
            % 创建输入区域
            inputPanel = uipanel('Parent', mainFig, 'Title', '待处理图像或视频', 'Position', [0.05, 0.6, 0.4, 0.35]);
            imageAxes = axes('Parent', inputPanel, 'Position', [0.05, 0.15, 0.9, 0.8]);
            loadImageButton = uicontrol('Parent', inputPanel, 'Style', 'pushbutton', 'String', '读取图像', 'Position', [20, 20, 100, 30], 'Callback', @loadImageCallback);
            loadVideoButton = uicontrol('Parent', inputPanel, 'Style', 'pushbutton', 'String', '读取视频', 'Position', [140, 20, 100, 30], 'Callback', @loadVideoCallback);
            
            % 创建处理区域
            processPanel = uipanel('Parent', mainFig, 'Title', '处理方法选择', 'Position', [0.55, 0.6, 0.4, 0.35]);
            methodPopup = uicontrol('Parent', processPanel, 'Style', 'popupmenu', 'String', {'生成索引表', '通过索引表构建RGB图像', '亮度冷暖图','密度分割法'}, 'Position', [20, 20, 150, 30]);
            processButton = uicontrol('Parent', processPanel, 'Style', 'pushbutton', 'String', '处理', 'Position', [180, 20, 100, 30], 'Callback', @processImageCallback);
        
            % 创建输出区域
            outputPanel = uipanel('Parent', mainFig, 'Title', '结果展示', 'Position', [0.05, 0.1, 0.9, 0.45]);
            resultAxes = axes('Parent', outputPanel, 'Position', [0.05, 0.15, 0.4, 0.7]);
            resultAxes1 = axes('Parent', outputPanel, 'Position', [0.55, 0.15, 0.4, 0.7]);
            infoEdit = uicontrol('Parent', outputPanel, 'Style', 'edit', 'Max', 2, 'HorizontalAlignment', 'left', 'Position', [0.5, 0.15, 0.45, 0.7]);
            %%
            % 图像读取回调函数
            function loadImageCallback(~, ~)
                % 获取当前的handles结构体
                handles = guidata(gcf);
        
                % 打开文件选择对话框
                [FileName,handles.pathname] = uigetfile({'*.jpg;*.jpeg;*.png;*.bmp','Image files (*.jpg, *.jpeg, *.png, *.bmp)'},'选择一张图像');
        
                % 如果用户取消选择图像,则返回
                if isequal(FileName,0)
                    return;
                end
        
                % 读取图像并在axes中显示
                handles.inputimg = imread(fullfile(handles.pathname, FileName));
            
                % 将读取到的变量存储到handles结构体中
                
                handles.inputvid = [];
            
                imshow(handles.inputimg, 'Parent', imageAxes);
        
                % 存储更新后的handles结构体
                guidata(gcf, handles);
            end
            %%
            % 视频读取回调函数
            function loadVideoCallback(~, ~)
                % 获取当前的handles结构体
                handles = guidata(gcf);
                
                % 打开文件选择对话框
                [FileName,handles.pathname] = uigetfile({'*.avi;*.mp4;*.mov','Video files (*.avi, *.mp4, *.mov)'},'选择一个视频');
            
                % 如果用户取消选择视频,则返回
                if isequal(FileName,0)
                    return;
                end
            
                % 读取视频并存储为一个VideoReader对象
                handles.inputvid = VideoReader(fullfile(handles.pathname, FileName));
                handles.outvid = VideoWriter(fullfile(handles.pathname, 'output.avi')); % 创建用于保存处理后帧的新视频
                
                % 将读取到的变量存储到handles结构体中
                handles.inputimg = [];
                % 在axes中显示第一帧图像
                frame = read(handles.inputvid, 1);
                imshow(frame, 'Parent', imageAxes);
                
                % 存储更新后的handles结构体
                guidata(gcf, handles);
            end
        
            %%
            function processImageCallback(~, ~)
                % 获取当前的handles结构体
                handles = guidata(gcf);
                
                % 获取选择的处理方法
                methodIndex = get(methodPopup, 'Value');
                methodList = {'生成索引表', '通过索引表构建RGB图像', '亮度冷暖图','密度分割法'};
                selectedMethod = methodList{methodIndex};
                %%
                if size(handles.inputvid) == 0
                    % 根据选择的方法调用相应的处理函数
                    if strcmp(selectedMethod, '生成索引表')
                        handles.outindex = [];
                        handles.missingNumbers = [];
                        handles.junhendijige = [];
                        handles.xianyandijige = [];
                        handles.lenlong = [];
                        % 弹出对话框获取方法1的参数值
                        prompt = {'非饱和占比:', '非饱和指标:', '饱和指标:'};
                        dlgtitle = '输入参数';
                        dims = [1 35];
                        definput = {'15', '2','0.5'};
                        params = inputdlg(prompt, dlgtitle, dims, definput);
        
                        % 调用方法1的处理函数,并传递用户输入的参数值
                        [handles.outindex,handles.missingNumbers,handles.junhendijige,handles.xianyandijige,handles.lenlong] = byrgbgetindex(handles.inputimg, params{:});
                    elseif strcmp(selectedMethod, '通过索引表构建RGB图像')
                        % 调用方法2的处理函数,并传递用户输入的参数值
                        handles.outimg = []; % 处理结果图片
                        handles.outimg = byindexgetrgb(handles.inputimg, handles.outindex);
                    
                        % 在结果区域显示处理结果
                        [~,~,k] = size(handles.inputimg);
                        if k == 3 
                            gray_img = rgb2gray(handles.inputimg);
                        else
                            gray_img = handles.inputimg;
                        end
                        
                        imshow(gray_img, 'Parent', resultAxes);
                        imshow(handles.outimg, 'Parent', resultAxes1);
                    elseif strcmp(selectedMethod, '亮度冷暖图')
                        handles.outimg = []; % 处理结果图片
                        % 弹出对话框获取方法3的参数值
                        
                        prompt = {'关注成分选择:'};
                        dlgtitle = '输入参数';
                        dims = [1 35];
                        definput = {'1'};
                        params = inputdlg(prompt, dlgtitle, dims, definput);
        
                        % 调用方法3的处理函数,并传递用户输入的参数值
                        handles.outimg = gray_differup2rgb(handles.inputimg , params{:});
                        % 在结果区域显示处理结果
                        [~,~,k] = size(handles.inputimg);
                        if k == 3 
                            gray_img = rgb2gray(handles.inputimg);
                        else
                            gray_img = handles.inputimg;
                        end
                        imshow(gray_img, 'Parent', resultAxes);
                
                        imshow(handles.outimg, 'Parent', resultAxes1);
             
                    elseif strcmp(selectedMethod, '密度分割法')
                        handles.outimg = []; % 处理结果图片
                        
        
                        % 调用方法4的处理函数,并传递用户输入的参数值
                        handles.outimg = midufenge(handles.inputimg);
                        % 在结果区域显示处理结果
                        [~,~,k] = size(handles.inputimg);
                        if k == 3 
                            gray_img = rgb2gray(handles.inputimg);
                        else
                            gray_img = handles.inputimg;
                        end
                        imshow(gray_img, 'Parent', resultAxes);
                        imshow(handles.outimg, 'Parent', resultAxes1);
                    end
                end
                %%
                if size(handles.inputimg) == 0
                    % 根据选择的方法调用相应的处理函数
                    if strcmp(selectedMethod, '生成索引表')
                        handles.outindex = [];
                        handles.missingNumbers = [];
                        handles.junhendijige = [];
                        handles.xianyandijige = [];
                        handles.lenlong = [];
                        % 弹出对话框获取方法1的参数值
                        prompt = {'非饱和占比:', '非饱和指标:', '饱和指标:'};
                        dlgtitle = '输入参数';
                        dims = [1 35];
                        definput = {'15', '2','0.5'};
                        params = inputdlg(prompt, dlgtitle, dims, definput);
        
                        % 调用方法1的处理函数,并传递用户输入的参数值
                        frameSequence = cell(1, 5);
                        if handles.inputvid.NumFrames < 5
                            concatenatedImage = readFrame(handles.inputvid);
                        else
                            for i = 1:5
                                frame = readFrame(handles.inputvid);
                                frameSequence{i} = frame;
                            end
                            concatenatedImage = cat(2, frameSequence{:});
                        end
                        
                        [handles.outindex,handles.missingNumbers,handles.junhendijige,handles.xianyandijige,handles.lenlong] = byrgbgetindex(concatenatedImage, params{:});
                    elseif strcmp(selectedMethod, '通过索引表构建RGB图像')
                        % 调用方法2的处理函数,并传递用户输入的参数值
        
                        
                        open(handles.outvid); % 打开新视频文件
                        while hasFrame(handles.inputvid)
                            frame = readFrame(handles.inputvid);
                            [~,~,k] = size(frame);
                            % 对当前帧进行处理
                            if k == 3 
                                frame = rgb2gray(frame);
                            end
                            imshow(frame, 'Parent', resultAxes);
                            frame = byindexgetrgb(frame, handles.outindex);
                            imshow(frame, 'Parent', resultAxes1);
                            % 将处理后的帧写入新视频
                            writeVideo(handles.outvid, frame);
                            pause(0.0333); % 控制帧率为30fps
                                
                        end
                        close(handles.outvid); % 关闭新视频文件
                    
                    elseif strcmp(selectedMethod, '亮度冷暖图')
                        % 弹出对话框获取方法3的参数值
                        prompt = {'关注成分选择:'};
                        dlgtitle = '输入参数';
                        dims = [1 35];
                        definput = {'1'};
                        params = inputdlg(prompt, dlgtitle, dims, definput);
        
                        % 调用方法3的处理函数,并传递用户输入的参数值
                        open(handles.outvid); % 打开新视频文件
                        while hasFrame(handles.inputvid)
                            frame = readFrame(handles.inputvid);
                            [~,~,k] = size(frame);
                            % 对当前帧进行处理
                            if k == 3 
                                frame = rgb2gray(frame);
                            end
                            imshow(frame, 'Parent', resultAxes);
                            frame = gray_differup2rgb(frame , params{:});
                            imshow(frame, 'Parent', resultAxes1);
                            % 将处理后的帧写入新视频
                            writeVideo(handles.outvid, frame);
                            pause(0.0333); % 控制帧率为30fps
                                
                        end
                        close(handles.outvid); % 关闭新视频文件
             
                    elseif strcmp(selectedMethod, '密度分割法')
                        
        
                        % 调用方法4的处理函数,并传递用户输入的参数值
                        open(handles.outvid); % 打开新视频文件
                        while hasFrame(handles.inputvid)
                            frame = readFrame(handles.inputvid);
                            [~,~,k] = size(frame);
                            % 对当前帧进行处理
                            if k == 3 
                                frame = rgb2gray(frame);
                            end
                            imshow(frame, 'Parent', resultAxes);
                            frame = midufenge(frame);
                            imshow(frame, 'Parent', resultAxes1);
                            % 将处理后的帧写入新视频
                            writeVideo(handles.outvid, frame);
                            pause(0.0333); % 控制帧率为30fps
                                
                        end
                        close(handles.outvid); % 关闭新视频文件
                    end
                end
        
                % 将handles变量存储到主窗口中
                guidata(gcf, handles);
            end
        end

  • 22
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值