简单的灰度图像可以通过

InbertIm('example.jpg');

实现灰度翻转,简单说就是白变黑,黑变白。

可以自动侦测图像bit数,还有灰度范围。

function [invIm]=InvertIm(im, varargin)

% InvertIm generalized image inversion (grayscale reversal)

%    invIm=InvertIm(im) inverts an image array im in the sense of reversing

%    its grayscale (NOT matrix inversion).  InvertIm effects the inversion

%    by computing new image data rather than by reversing the colormap or

%    look-up-table (LUT), which is often, but not always, a better

%    approach.  im can be an array of any dimension composed of numeric or

%    binary elements (integers [uint8, int8, uint16, int16, etc.], floating

%    point [double or single], or logical).  The result invIm is the same

%    data class as im.

%

%    For numeric arrays, the inverse is computed using

%

%       invIm = (maxVal + minVal) - im

%

%    with appropriate treatment of the different data types.  For integer

%    arrays, minVal and maxVal are the minimum and maximum allowed

%    integers.  For floating point arrays, if the image data lie within the

%    range [0,1], the inversion is performed with default minVal and maxVal

%    of 0 and 1.  If the image data lie outside [0,1], then the minimum and

%    maxmimum values of the data are used for minVal and maxVal.

%

%    Invert(im, bitsStored) returns the inverse of the image im using a

%    maxVal of 2^bitsStored.  This option is convenient when inverting

%    images with a limited dynamic range, e.g., when you have a 16-bit

%    image that only uses the first 12 bits of data.  This option is only

%    applicable to integer data.

%

%    invIm=Invert(im, [], lowerLimit) returns the inverse of the image im

%    using a minVal of lowerLimit.  This option is useful, for example,

%    when inverting signed integer image data that is only positive.

%

%    invIm=Invert(im, [], [], upperLimit) returns the inverse of the image

%    im using a maxVal of upperLimit.

%

%    All three optional arguments may be used together (e.g.,

%    invIm=Invert(im, bitsStored, lowerLimit, upperLimit)), but upperLimit

%    will supercede bitsStored.

%

%

%    Example:

%      im=imread('cameraman.tif');  % Brigg's field at MIT

%      fig=figure; colormap(gray)

%      subplot(2,2,1)

%      subimage(im)

%      axis off

%      subplot(2,2,2)

%      imhist(im)

%      ylabel('Number of Pixels')

%

%      invIm=InvertIm(im);

%      subplot(2,2,3)

%      subimage(invIm)

%      axis off

%      subplot(2,2,4)

%      imhist(invIm)

%      ylabel('Number of Pixels')

%

%

%    See also colormap

%    Written by Stead Kiger, Ph.D.

%               Department of Radiation Oncology

%               Beth Israel Deaconess Medical Center

%               Harvard Medical School

%               Boston, MA  02215

%

%    Last revised on April 11, 2007

% Initialize parameters

bitsStored=[];

lowerLimit=[];

upperLimit=[];

% Assign and check optional arguments for parameters

error(nargchk(1, 4, nargin, 'struct'))

% bitsStored

if nargin >= 2,

bitsStored=varargin{1};

if ~(isnumeric(bitsStored) || isempty(bitsStored)),

error('Optional argument bitsStored must be either numeric or empty.')

end

end

% lowerLimit

if nargin >= 3,

lowerLimit=varargin{2};

if ~(isnumeric(lowerLimit) || isempty(lowerLimit)),

error('Optional argument lowerLimit must be either numeric or empty.')

end

end

% upperLimit

if nargin == 4,

upperLimit=varargin{3};

if ~(isnumeric(upperLimit) || isempty(upperLimit)),

error('Optional argument upperLimit must be either numeric or empty.')

end

end

% Check that the image array is either numeric or logical

if ~(islogical(im) || isnumeric(im)),

error('Input array must be logical or numeric, but is %s',class(im))

end

if islogical(im),

% logical - just apply not

invIm=~im;

else

% determine the image class, i.e., data type; e.g., uint8, int16 etc.

imClass=class(im);

% create the function handle for casting the double precision image back

% into its original class

clsHdl=str2func(imClass);

if isinteger(im),

% get the minimum integer

minVal=double(intmin(imClass));

if ~isempty(lowerLimit),

minVal=double(max(minVal, lowerLimit));

end

% get the maximum integer

maxVal=double(intmax(imClass));

if ~isempty(bitsStored),

maxVal=double(min(maxVal, 2^bitsStored - 1));

end

if ~isempty(upperLimit),

maxVal=min(maxVal, upperLimit);

end

elseif isfloat(im)

imMin=min(im(:));

imMax=max(im(:));

if imMin >= 0 && imMax <= 1,

% Intensity image or RGB

minVal=0;

maxVal=1;

else

minVal=imMin;

maxVal=imMax;

end

if ~isempty(lowerLimit),

minVal=lowerLimit;

end

if ~isempty(upperLimit),

maxVal=upperLimit;

end

else

error('Input array must be logical or numeric, but is %s',class(im))

end

% Compute the inverse and cast the array back into its original class

invIm = feval(clsHdl, (maxVal + minVal) - double(im));

end

matlab 图像的灰度值翻转相关推荐

  1. 用MATLAB进行灰度图像灰度值的处理

            在处理数据集时,需要将图像的灰度值进行替换,例如将图像中灰度值为5的像素转化为灰度值为1,将所有大于0的灰度值的像素都变成灰度值为1的图像,MATLAB代码转换如下: 单张图像的处理: ...

  2. 8bit黑白图像的灰度值范围是_浅谈工业CT图像灰度值

    工业CT使用的X射线检测作为无损检测的主要方法之一,与其他方法相比具有直观.准确等优点.射线检测中垂直于射线透照方向的缺陷尺寸可精确测量,但平行于射线透照方向的缺陷尺寸无法直接测量得到,而未焊透.根部 ...

  3. 二值图填充原理 matlab,图像Ostu二值化原理及matlab实现代码

    Ostu假设图像是由前景区域和背景区域两部分组成的,通过遍历计算不同阈值(通常为[0 255]区间范围内)下分割结果中前景区域和背景区域的灰度直方图,然后比较两者之间的方差,使得方差最大化的那个灰度阈 ...

  4. MATLAB图像的中值滤波——手动滤波和medfilt2函数滤波

    本文还是书接上回,https://blog.csdn.net/weixin_44502554/article/details/126283957?spm=1001.2014.3001.5502 前期的 ...

  5. 8bit黑白图像的灰度值范围是_数字图像处理基本知识

    1.数字图像: 数字图像,又称为数码图像或数位图像,是二维图像用有限数字数值像素的表示.数字图像是由模拟图像数字化得到的.以像素为基本元素的.可以用数字计算机或数字电路存储和处理的图像. 2.数字图像 ...

  6. 8bit黑白图像的灰度值范围是_窗宽窗位对基于互信息的医学图像

    窗宽窗位对基于互信息的医学图像 摘 要:基于互信息的配准方法具有自动化程度高.配准精度高等优点,近来已成为医学图像处理领域的热点.基于互信息的配准方法实质上是一种对灰度进行统计和计算的方法,因此同一图 ...

  7. opencv读取图像的灰度值并显示出来

    通过双层循环,遍历所有的像素值,再输出灰度值即可.图片太大,所有只选择20行和20列进行输出. 昨天要用到图片的灰度值,脑子突然短路了,忘了存储灰度图的数组,存放的就是图片的灰度值,还以为要用什么函数 ...

  8. 图像转灰度值计算公式原理_图像处理--传统算法

    图像处理中平滑和锐化操作是什么? 平滑处理(smoothing)也称模糊处理(bluring),主要用于消除图像中的噪声部分,平滑处理常用的用途是用来减少图像上的噪点或失真,平滑主要使用图像滤波.在这 ...

  9. 【Matlab 图像】灰度二值化处理

    灰度运算 grayimg = rgb2gray(flag_yellow); % 灰度图像 二值化运算 BWimg = im2bw(grayimg, 0.8); 自动化阈值 T = graythresh ...

最新文章

  1. mysql 去掉复合索引_MySQL性能优化[实践篇]-复合索引实例
  2. HashMap 的 7 种遍历方式与性能分析!(强烈推荐)
  3. mysql实验6语言结构_实验六 SQL语言数据查询语言DQL.pdf
  4. 线程:等待/通知机制
  5. 从mysql高可用架构看高可用架构设计
  6. 2021-10-11 ! LeetCode226. 翻转二叉树 的前中后层序遍历写法
  7. hdu 2602 Bone Collector 解题报告
  8. C语言:替换字符串中某一段子字符串
  9. 2019 7.14学习笔记
  10. Flask中数据库的应用
  11. 批量替换_【脚本】AE照片墙模板图片批量替换脚本Multi Replacer
  12. 【Oracle】删除重复记录
  13. keras中使用ImageDataGenerator对MINST数据集数据增强
  14. EasyExcel 实现冻结行和列
  15. PDF Expert教程|七个提高效率的小技巧
  16. 全景制作教程:如何利用Pano2VR进行补天补地?
  17. pythontrun什么意思_python 新手笔记一
  18. Keil多分支工程管理
  19. 读《曾国藩》笔记2--慈不掌兵
  20. jvm 内存查看与分析工具

热门文章

  1. WordPress主题开发:主题初始化
  2. 阿里P7为了证明自己确实年入百万,晒出了他的工资
  3. 怎么找计算机里面的视频教程,win7系统快速搜索查找电脑里的视频文件的办法...
  4. Android第一天 安装Android Studio 3.5
  5. layer.tips换行问题(解决layui tips连续英文和数字不换行问题)
  6. 二分法python上机实验报告_数值分析上机实验报告..doc
  7. 在NVIDIA 官网下载驱动和Cuda库太慢或失败的解决办法
  8. 企业品牌推广做好这七点快速开启软文营销之旅
  9. 牛客网日刷30题错题解析
  10. 小米与联发科合作真相是什么