赞
踩
有时候我们需要对一张灰度范围小的 图像进行灰度归一化至[0,1]或[0,255]。
关于代码中 min(min(img))和max(max(img))的解释:
关于代码中 归一化的数学原理见博客:
===========================================================
测试结果:
原图:
第一种方法:
第二种方法:
第三种方法:
============================================================
代码:
%% 读取原图像 oriImage = imread('luoxuan.jpg'); grayImage = rgb2gray(oriImage); figure; subplot(121); imshow(grayImage); title('grayImage'); %% 计算灰度 originalMinValue = double(min(min(grayImage))); originalMaxValue = double(max(max(grayImage))); originalRange = originalMaxValue-originalMinValue; %% 归一化为[0 255] desiredMin = 0; %想要的最小灰度 desiredMax = 255; %想要的最大灰度 desiredRange=desiredMax-desiredMin; dblImageS255=desiredRange*(double(grayImage)-originalMinValue)/originalRange+desiredMin; subplot(122); imshow(uint8(dblImageS255)); imwrite(uint8(dblImageS255),'25_2.jpg') title('dblImageS255');
运行结果为:
代码:
%% 读取原图像 oriImage = imread('luoxuan.jpg'); grayImage = rgb2gray(oriImage); figure; subplot(121); imshow(grayImage); title('grayImage'); %% 计算灰度 originalMinValue = double(min(min(grayImage))); originalMaxValue = double(max(max(grayImage))); originalRange = originalMaxValue-originalMinValue; %% 归一化为[0 1] desiredMin = 0; %想要的最小灰度 desiredMax = 1; %想要的最大灰度 desiredRange=desiredMax-desiredMin; dblImageS1=desiredRange*(double(grayImage)-originalMinValue)/originalRange+desiredMin; % figure; subplot(122); imshow(dblImageS1); imwrite(uint8(dblImageS255),'25_2.jpg') title('dblImageS-01');
运行结果为:
代码:
%% 读取原图像 oriImage = imread('luoxuan.jpg'); grayImage = rgb2gray(oriImage); figure; subplot(121); imshow(grayImage); title('grayImage'); %% 计算灰度 originalMinValue = double(min(min(grayImage))); originalMaxValue = double(max(max(grayImage))); originalRange = originalMaxValue-originalMinValue; %% 调用matlab的函数实现归一化 img3 = mat2gray(oriImage); subplot(122); imshow(img3); imwrite(img3 ,'25_2.jpg') title('dblImageS-matlab');
运行结果为:
%% 读取原图像 oriImage = imread('luoxuan.jpg'); grayImage = rgb2gray(oriImage); figure; imshow(grayImage); title('grayImage'); %% 计算灰度 originalMinValue = double(min(min(grayImage))); originalMaxValue = double(max(max(grayImage))); originalRange = originalMaxValue-originalMinValue; %% 归一化为[0 255] desiredMin = 0; %想要的最小灰度 desiredMax = 255; %想要的最大灰度 desiredRange=desiredMax-desiredMin; dblImageS255=desiredRange*(double(grayImage)-originalMinValue)/originalRange+desiredMin; figure; imshow(uint8(dblImageS255)); imwrite(uint8(dblImageS255),'25_2.jpg') title('dblImageS255'); %% 归一化为[0 1] desiredMin = 0; %想要的最小灰度 desiredMax = 1; %想要的最大灰度 desiredRange=desiredMax-desiredMin; dblImageS1=desiredRange*(double(grayImage)-originalMinValue)/originalRange+desiredMin; figure; imshow(dblImageS1); imwrite(uint8(dblImageS255),'25_2.jpg') title('dblImageS-01'); %% 调用matlab的函数实现归一化 img3 = mat2gray(oriImage); figure; imshow(img3); imwrite(img3 ,'25_2.jpg') title('dblImageS-matlab');```
[1] https://blog.csdn.net/discoverer100/article/details/61426650
========END
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。