目錄
一,整體框架
指定圖像或視頻構(gòu)建索引表Byrgbgetindex
通過(guò)構(gòu)建的索引表進(jìn)行偽彩色轉(zhuǎn)換byindexgetrgb
通過(guò)函數(shù)映射進(jìn)行偽彩色轉(zhuǎn)換gray_differup2rgb
通過(guò)密度分割將灰度進(jìn)行映射midufenge
GUI界面 last
二,處理方法介紹
函數(shù)結(jié)構(gòu)
Byrgbgetindex
輸入內(nèi)容
Rgb_img
Bili
Junyuncha
Xianyancha
輸出內(nèi)容
Gray_Index
MissingNumbers
junhendijige
Xianyandijige
方法與流程
輸入?yún)?shù)規(guī)范化
構(gòu)建源顏色表
根據(jù)輸入的判定參數(shù)遍歷順序遍歷整個(gè)源顏色表?編輯?編輯?編輯
缺失灰度值用距離最近的灰度代替?編輯?編輯?編輯
將最終索引表按照灰度值升序排序?編輯?編輯?編輯
Byindexgetrgb
輸入內(nèi)容
輸入內(nèi)容
處理過(guò)程
Gray_differup2rgb
輸入內(nèi)容
輸出內(nèi)容
流程
Miudfenge
輸入內(nèi)容
輸出內(nèi)容
過(guò)程
三,GUI介紹
區(qū)域劃分
效果展示
四,源代碼
byrgbgetindex
byindexgetrgb
gray_differup2rgb
midufenge
last
一,整體框架
指定圖像或視頻構(gòu)建索引表Byrgbgetindex
通過(guò)構(gòu)建的索引表進(jìn)行偽彩色轉(zhuǎn)換byindexgetrgb
通過(guò)函數(shù)映射進(jìn)行偽彩色轉(zhuǎn)換gray_differup2rgb
通過(guò)密度分割將灰度進(jìn)行映射midufenge
GUI界面 last
-
二,處理方法介紹
-
函數(shù)結(jié)構(gòu)
- 輸入?yún)?shù)格式調(diào)整(將字符串轉(zhuǎn)化為數(shù)字,將彩色圖像轉(zhuǎn)化為灰度圖像)
- 處理參數(shù)返回結(jié)果
-
Byrgbgetindex
-
輸入內(nèi)容
-
Rgb_img
- 一張RGB圖像(兼容了灰度圖像
-
Bili
- 根據(jù)自定義標(biāo)準(zhǔn),篩選出最終顏色的鮮艷與均勻占比
- 1/(bili+1)為鮮艷顏色
-
Junyuncha
- 篩選顏色用到的自定義評(píng)判標(biāo)準(zhǔn),GValue-GrayValue小于junyuncha的認(rèn)為此顏色滿足均勻標(biāo)準(zhǔn)。
- 經(jīng)過(guò)實(shí)踐junyuncha設(shè)置為10-20效果更好
-
Xianyancha
- 按-R-G-B-R-的順序,挑選已挑選數(shù)量非最大的顏色,保證偏R,偏G,偏B的顏色相對(duì)均勻。差值比例大于鮮艷差的認(rèn)為滿足鮮艷標(biāo)準(zhǔn)
- 經(jīng)過(guò)實(shí)踐鮮艷差設(shè)置為0.5-0.8效果更好
-
-
輸出內(nèi)容
-
Gray_Index
- 根據(jù)輸入圖像構(gòu)建出的索引表
-
MissingNumbers
- 完成篩選后,沒(méi)有對(duì)應(yīng)RGB組合的灰度值
- 篩選條件越苛刻Missing_Gray的數(shù)量越多
-
junhendijige
- 最后被認(rèn)定為均衡并加入索引表的顏色在源顏色表的序號(hào)
- 這個(gè)序號(hào)越接近源顏色表的總長(zhǎng),認(rèn)為篩選條件設(shè)置越合理
-
Xianyandijige
- 最后被認(rèn)定為鮮艷并加入索引表的顏色在源顏色表的序號(hào)
- 這個(gè)序號(hào)越接近顏色表的總長(zhǎng),認(rèn)為篩選條件設(shè)置的越合理
-
-
方法與流程
-
輸入?yún)?shù)規(guī)范化
- 格式轉(zhuǎn)換
-
構(gòu)建源顏色表
- 將RGB通道顏色聚合為128個(gè)等級(jí)值
- 統(tǒng)計(jì)顏色組合及其出現(xiàn)次數(shù)
- 統(tǒng)計(jì)chayis(單組rgb的最大值與最小值的差異)
- 構(gòu)建表最終結(jié)果按照差異降序,出現(xiàn)次數(shù)升序排序
- 將RGB通道顏色聚合為128個(gè)等級(jí)值
-
根據(jù)輸入的判定參數(shù)遍歷順序遍歷整個(gè)源顏色表
-
缺失灰度值用距離最近的灰度代替
-
將最終索引表按照灰度值升序排序
-
-
-
Byindexgetrgb
-
輸入內(nèi)容
- Gray_img
- 灰度圖像數(shù)據(jù)
- Index
- 顏色索引表
- Gray_img
-
輸入內(nèi)容
- Rgb_img
- 根據(jù)灰度圖像與索引表恢復(fù)的RGB圖像
- Rgb_img
-
處理過(guò)程
- 輸入格式規(guī)范
- 遍歷圖像內(nèi)容,根據(jù)灰度值查找索引表對(duì)應(yīng)的RGB組合填充RGB
- 對(duì)填充后的RGB通道分別進(jìn)行中值濾波
-
-
Gray_differup2rgb
-
輸入內(nèi)容
- GrayImg
- 灰度圖像(兼容彩色圖像)
- Mode
- 模式,1將高亮映射為暖色,0低亮映射為暖色
- GrayImg
-
輸出內(nèi)容
- Result_rgbImg
- 處理結(jié)果
- Result_rgbImg
-
流程
- 參數(shù)規(guī)范
- 按模式分別對(duì)灰度值進(jìn)行函數(shù)映射
- 這里我們選用了線性的函數(shù)映射
- 這里我們選用了線性的函數(shù)映射
-
-
Miudfenge
-
輸入內(nèi)容
- Image
- 圖片信息,兼容彩色圖像
- Image
-
輸出內(nèi)容
- Colored_image
- 分割結(jié)果
- Colored_image
-
過(guò)程
- 參數(shù)規(guī)范
- 計(jì)算每一段像素寬度
- 定義每一段色彩值
- 執(zhí)行密度分割
-
-
-
三,GUI介紹
-
區(qū)域劃分
- 輸入?yún)^(qū)域
- 處理區(qū)域
- 輸出區(qū)域
- 輸入?yún)^(qū)域
-
效果展示
- 輸入?yún)^(qū)域
- 點(diǎn)擊上傳圖片或視頻
圖像會(huì)直接顯示,視頻會(huì)顯示第一幀
- 處理區(qū)域
- 列表選擇處理方法
- 選擇后點(diǎn)擊處理
- 跳出彈窗指定參數(shù)
這里我們默認(rèn)了一組適用性比較廣泛的參數(shù)
- 點(diǎn)擊確定即可進(jìn)行處理
- 列表選擇處理方法
- 輸出區(qū)域
- 對(duì)于有結(jié)果需要展示的處理方法,會(huì)直接顯示原圖像與處理后的圖像
- 圖像與視頻均可處理
- 點(diǎn)擊上傳圖片或視頻
- 輸入?yún)^(qū)域
-
-
四,源代碼
-
byrgbgetindex
-
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); % 獲取顏色組合和出現(xiàn)次數(shù) [colors, ~, index] = unique(b, 'rows'); counts = accumarray(index, 1); % 計(jì)算灰度值 grayValues = 0.2989 * colors(:, 1) + 0.5870*colors(:, 2) + 0.1140*colors(:, 3); %% 調(diào)整創(chuàng)建初始索引表, % 根據(jù)出現(xiàn)次數(shù)進(jìn)行排序 [sortedCounts, idx] = sort(counts, 'descend'); sortedColors = colors(idx, :); sortedGrayValues = grayValues(idx); % 由于單憑出現(xiàn)次數(shù)建立的索引表得到的圖像效果很差,所以為了使色彩更加鮮艷我以色彩差異(算是半個(gè)飽和度吧)為主建立了新的灰度預(yù)選表 maxValues = max(sortedColors, [], 2); minValues = min(sortedColors, [], 2); chayis = maxValues-minValues; %計(jì)算差異大小 [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個(gè)等級(jí) 此時(shí) % 將RGB聚合(奇數(shù)減一,偶數(shù)不變) % 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; %% 篩選最終索引表 % 初始化結(jié)果列表和已選灰度值列表 selectedRGB = []; selectedGray = []; usedGrays = []; % 選擇灰度值 % 優(yōu)化,均勻選擇,一半G均衡為主為主,剩下的一半,RGB鮮艷各占一份 % 標(biāo)記 santi ={0,取均衡,1,取鮮艷} % diqiu ={0,紅,1,綠,2藍(lán)} santi = 0; santinum = 0; junhendijige = 0; xianyandijige = 0; rnum = 0; gnum = 0; bnum = 0; lenlong = height(newTable); for i = 1:1:lenlong % 取出當(dāng)前 rgb 對(duì)應(yīng)的灰度值 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 添加到結(jié)果列表中 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 添加到結(jié)果列表中 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 添加到結(jié)果列表中 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 添加到結(jié)果列表中 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 % 創(chuàng)建包含 0 到 255 的數(shù)組 allNumbers = 0:255; % 找出 selectedRGB 中存在的數(shù)字 presentNumbers = unique(selectedGray); % 找出缺失的數(shù)字 missingNumbers = setdiff(allNumbers, presentNumbers); % 用距離最近的映射關(guān)系代替缺失的灰度值 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
????????
-
-
byindexgetrgb
-
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
????????
-
-
gray_differup2rgb
-
%% % 適用于醫(yī)學(xué)便于觀察——差異最大化的灰度圖像轉(zhuǎn)換 % 關(guān)注低亮度區(qū)域時(shí):低亮度映射為紅色,高亮度映射為藍(lán)色;mode = 0 % 關(guān)注高亮度區(qū)域時(shí):高亮度映射為紅色,低亮度映射為藍(lán)色;mode - 1 % 分別用三個(gè)矩陣保存,三個(gè)通道的灰度值。每個(gè)通道的灰度值,有不同的映射函數(shù)。 % 輸入灰度級(jí) 輸出彩色 % 0~50 1/5 藍(lán)色 紅色 % 51~102 2/5 藍(lán)色+綠色 綠色+紅色 % 103~153 3/5 綠色 綠色 % 154~204 4/5 綠色+紅色 藍(lán)色+綠色 % 205~255 5/5 紅色 藍(lán)色 %% % % 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 %% 改進(jìn)版本,速度有所提升 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
????????文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-827451.html
-
-
midufenge
-
function [colored_image] = midufenge(image) %PINLUCHENGFEN 輸入灰度圖,選擇成分:傅里葉變換濾除目標(biāo)成分 [~,~,k] = size(image); if k == 3 image = rgb2gray(image); end gray_image_double = double(image)/255; [row, col] = size(gray_image_double); % 定義分割段數(shù) N = 5; % 計(jì)算灰度值的最大值和最小值 max_value = max(max(gray_image_double)); min_value = min(min(gray_image_double)); % 計(jì)算每一段的像素值域?qū)挾?step = (max_value - min_value) / N; % 定義每一段的彩色值 colors = [0 0 1; 0 1 1; 0 1 0; 1 1 0; 1 0 0]; % 藍(lán)->青->綠->黃->紅 % 執(zhí)行密度分割法 colored_image = zeros(row, col, 3); for i = 1:N start_gray = min_value + (i - 1) * step; end_gray = min_value + i * step; % 找到屬于當(dāng)前段的像素值 mask = (gray_image_double >= start_gray) & (gray_image_double < end_gray); % 將彩色圖像賦值給對(duì)應(yīng)的像素 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
????????文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-827451.html
-
-
last
-
myGUIApp(); function myGUIApp() % 創(chuàng)建主窗口 mainFig = figure('Name', 'My GUI App', 'Position', [200,200,800,600]); %% % 定義handles結(jié)構(gòu)體,用于不同回調(diào)函數(shù)之間的變量共享 handles = struct(); handles.inputimg = []; % 輸入圖片 handles.inputvid = []; % 輸入視頻 handles.outimg = []; % 處理結(jié)果圖片 handles.outvid = []; % 處理結(jié)果視頻 handles.pathname = []; handles.outindex = []; handles.missingNumbers = []; handles.junhendijige = []; handles.xianyandijige = []; handles.lenlong = []; %% % 創(chuàng)建輸入?yún)^(qū)域 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); % 創(chuàng)建處理區(qū)域 processPanel = uipanel('Parent', mainFig, 'Title', '處理方法選擇', 'Position', [0.55, 0.6, 0.4, 0.35]); methodPopup = uicontrol('Parent', processPanel, 'Style', 'popupmenu', 'String', {'生成索引表', '通過(guò)索引表構(gòu)建RGB圖像', '亮度冷暖圖','密度分割法'}, 'Position', [20, 20, 150, 30]); processButton = uicontrol('Parent', processPanel, 'Style', 'pushbutton', 'String', '處理', 'Position', [180, 20, 100, 30], 'Callback', @processImageCallback); % 創(chuàng)建輸出區(qū)域 outputPanel = uipanel('Parent', mainFig, 'Title', '結(jié)果展示', '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]); %% % 圖像讀取回調(diào)函數(shù) function loadImageCallback(~, ~) % 獲取當(dāng)前的handles結(jié)構(gòu)體 handles = guidata(gcf); % 打開(kāi)文件選擇對(duì)話框 [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)); % 將讀取到的變量存儲(chǔ)到handles結(jié)構(gòu)體中 handles.inputvid = []; imshow(handles.inputimg, 'Parent', imageAxes); % 存儲(chǔ)更新后的handles結(jié)構(gòu)體 guidata(gcf, handles); end %% % 視頻讀取回調(diào)函數(shù) function loadVideoCallback(~, ~) % 獲取當(dāng)前的handles結(jié)構(gòu)體 handles = guidata(gcf); % 打開(kāi)文件選擇對(duì)話框 [FileName,handles.pathname] = uigetfile({'*.avi;*.mp4;*.mov','Video files (*.avi, *.mp4, *.mov)'},'選擇一個(gè)視頻'); % 如果用戶取消選擇視頻,則返回 if isequal(FileName,0) return; end % 讀取視頻并存儲(chǔ)為一個(gè)VideoReader對(duì)象 handles.inputvid = VideoReader(fullfile(handles.pathname, FileName)); handles.outvid = VideoWriter(fullfile(handles.pathname, 'output.avi')); % 創(chuàng)建用于保存處理后幀的新視頻 % 將讀取到的變量存儲(chǔ)到handles結(jié)構(gòu)體中 handles.inputimg = []; % 在axes中顯示第一幀圖像 frame = read(handles.inputvid, 1); imshow(frame, 'Parent', imageAxes); % 存儲(chǔ)更新后的handles結(jié)構(gòu)體 guidata(gcf, handles); end %% function processImageCallback(~, ~) % 獲取當(dāng)前的handles結(jié)構(gòu)體 handles = guidata(gcf); % 獲取選擇的處理方法 methodIndex = get(methodPopup, 'Value'); methodList = {'生成索引表', '通過(guò)索引表構(gòu)建RGB圖像', '亮度冷暖圖','密度分割法'}; selectedMethod = methodList{methodIndex}; %% if size(handles.inputvid) == 0 % 根據(jù)選擇的方法調(diào)用相應(yīng)的處理函數(shù) if strcmp(selectedMethod, '生成索引表') handles.outindex = []; handles.missingNumbers = []; handles.junhendijige = []; handles.xianyandijige = []; handles.lenlong = []; % 彈出對(duì)話框獲取方法1的參數(shù)值 prompt = {'非飽和占比:', '非飽和指標(biāo):', '飽和指標(biāo):'}; dlgtitle = '輸入?yún)?shù)'; dims = [1 35]; definput = {'15', '2','0.5'}; params = inputdlg(prompt, dlgtitle, dims, definput); % 調(diào)用方法1的處理函數(shù),并傳遞用戶輸入的參數(shù)值 [handles.outindex,handles.missingNumbers,handles.junhendijige,handles.xianyandijige,handles.lenlong] = byrgbgetindex(handles.inputimg, params{:}); elseif strcmp(selectedMethod, '通過(guò)索引表構(gòu)建RGB圖像') % 調(diào)用方法2的處理函數(shù),并傳遞用戶輸入的參數(shù)值 handles.outimg = []; % 處理結(jié)果圖片 handles.outimg = byindexgetrgb(handles.inputimg, handles.outindex); % 在結(jié)果區(qū)域顯示處理結(jié)果 [~,~,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 = []; % 處理結(jié)果圖片 % 彈出對(duì)話框獲取方法3的參數(shù)值 prompt = {'關(guān)注成分選擇:'}; dlgtitle = '輸入?yún)?shù)'; dims = [1 35]; definput = {'1'}; params = inputdlg(prompt, dlgtitle, dims, definput); % 調(diào)用方法3的處理函數(shù),并傳遞用戶輸入的參數(shù)值 handles.outimg = gray_differup2rgb(handles.inputimg , params{:}); % 在結(jié)果區(qū)域顯示處理結(jié)果 [~,~,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 = []; % 處理結(jié)果圖片 % 調(diào)用方法4的處理函數(shù),并傳遞用戶輸入的參數(shù)值 handles.outimg = midufenge(handles.inputimg); % 在結(jié)果區(qū)域顯示處理結(jié)果 [~,~,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 % 根據(jù)選擇的方法調(diào)用相應(yīng)的處理函數(shù) if strcmp(selectedMethod, '生成索引表') handles.outindex = []; handles.missingNumbers = []; handles.junhendijige = []; handles.xianyandijige = []; handles.lenlong = []; % 彈出對(duì)話框獲取方法1的參數(shù)值 prompt = {'非飽和占比:', '非飽和指標(biāo):', '飽和指標(biāo):'}; dlgtitle = '輸入?yún)?shù)'; dims = [1 35]; definput = {'15', '2','0.5'}; params = inputdlg(prompt, dlgtitle, dims, definput); % 調(diào)用方法1的處理函數(shù),并傳遞用戶輸入的參數(shù)值 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, '通過(guò)索引表構(gòu)建RGB圖像') % 調(diào)用方法2的處理函數(shù),并傳遞用戶輸入的參數(shù)值 open(handles.outvid); % 打開(kāi)新視頻文件 while hasFrame(handles.inputvid) frame = readFrame(handles.inputvid); [~,~,k] = size(frame); % 對(duì)當(dāng)前幀進(jìn)行處理 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); % 關(guān)閉新視頻文件 elseif strcmp(selectedMethod, '亮度冷暖圖') % 彈出對(duì)話框獲取方法3的參數(shù)值 prompt = {'關(guān)注成分選擇:'}; dlgtitle = '輸入?yún)?shù)'; dims = [1 35]; definput = {'1'}; params = inputdlg(prompt, dlgtitle, dims, definput); % 調(diào)用方法3的處理函數(shù),并傳遞用戶輸入的參數(shù)值 open(handles.outvid); % 打開(kāi)新視頻文件 while hasFrame(handles.inputvid) frame = readFrame(handles.inputvid); [~,~,k] = size(frame); % 對(duì)當(dāng)前幀進(jìn)行處理 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); % 關(guān)閉新視頻文件 elseif strcmp(selectedMethod, '密度分割法') % 調(diào)用方法4的處理函數(shù),并傳遞用戶輸入的參數(shù)值 open(handles.outvid); % 打開(kāi)新視頻文件 while hasFrame(handles.inputvid) frame = readFrame(handles.inputvid); [~,~,k] = size(frame); % 對(duì)當(dāng)前幀進(jìn)行處理 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); % 關(guān)閉新視頻文件 end end % 將handles變量存儲(chǔ)到主窗口中 guidata(gcf, handles); end end
-
-
到了這里,關(guān)于數(shù)字圖像處理——灰度圖像(視頻)轉(zhuǎn)化偽彩色圖像(視頻)(含源代碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!