首页 > 编程学习 > matlab nlfilter 填充方式,MatLab实验步骤(超杰版)

图像变换--灰度调整、滤波增强---二值化(阈值分割)--形态学处理--特征提取

一:色彩变换,从颜色上控制思考怎样应用灰度的变换、滤波变换减少地物

直方图均衡化histeq自适应均衡化adapthisteq灰度调节(灰度映射)imadjust【直接变换、非线性变换、灰度映射】图像相减imsubtract(一张求减可以调节灰度值、两张相减可以消除一些地物)组合型:直方图均衡自适应调整图像相减

背景色剔除:缨帽滤波变换:调整背景色,增大对象间隙

图像补色:照亮谷底

滤波变换:消除物体filter2()、imfilter、medfilt2不常用:抖动

二:二值化,中介

然后二值化:im2bw组合型:自动灰度阈值分割二值化:J = im2bw(I,graythresh(I));

im2bw(I,100/255)二值化修饰imshow(bw,[1 0 0;0 1 0])求反色求补色

三、形态学处理,消除不必要的地物然后形态学处理,再减去些东西,{消除物体的方法}(膨胀、腐蚀、开运算、闭运算、与运算、加减运算、空洞填充、移除边界连通的目标、缨帽组合变换)

四特征提取:进行最终的提取最后通过特征提取进行提取(骨架化、边缘像素测定、绘制轮廓线、边缘提取、等值线提取)

下面是一些处理的函数:

显示:hist(I);

whos

imfinfo('')

imwrite(I,'XX.tif');

纹理映射方式观察[x,y,z]= cylinder;warp(x,y,z,I);

指定输出图像像素的大小:J =

imresize(I,1.25);【tif】

图像的旋转:J =

imrotate(I,90,'bilinear');【tif】

图像的裁剪:J = imcrop(I,[60 40 100

90]);【tif】

区域属性度量 stats =

regionprops(bwlabel(I),'all');stats(23)【png】【也不用imshow】

图像转换

索引图像转换为RGB显示:imshow(ind2rgb(X,map))灰度图像转换为RGB图像:imshow(cat(3,I,I,I))将数据矩阵转换成灰度图像imshow(mat2gray(I))

mat2gray(fitsread('solarspectra.fts'))【fts格式的处理】imshow(fitsread('solarspectra.fts'),[]);【解释:通过算子对图像滤波后的图像即是数据矩阵,当然数据矩阵也能直接显示当然如果数据矩阵不想直接转化成灰度图像时,另一种简单的方法是调节灰度值imshow(J,[])(其中J为滤波后的数据矩阵)】将灰度图像转化为索引图像grayslice(I,16);将RGB图像转换为灰度图像rgb2gray(RGB)将真彩png图像转化为索引图像[X,map] =

rgb2ind(I,128);将真彩、索引色、灰度图转化为二值图:imshow(im2bw(X,map,0.4));imshow(im2bw(I,0.4))将索引图转化为灰度图load trees imshow(ind2gray(X,map))

[X,map] = imread('forest.tif');

I = ind2gray(X,map);

灰度调整:增强(只改变灰度和颜色,不消除物体,不改变图像的原有物质)imshow(I,[]);

histeq(I)

adapthisteq(I)

imsubtract(I,50)(图像相减)

灰度调节imadjust(I,stretchlim(I),[]);{提高对比度}

imadjust(I,stretchlim(I),[0 1]);

直方图均衡自适应调整bw1 =

imadjust(adapthisteq(I,'NumTiles',[10 10]));

图像的二值化level = graythresh(I),J =

im2bw(I,level);

J = im2bw(I,graythresh(I));【自动灰度阈值分割】

自定义阈值分割figure,imhist(I);J=im2bw(I,100/255);【通过灰度来进行二值化】

二值图像的调节色彩imshow(bw,[1 0 0;0 1

0])

求反色imshow(~I)求补色imshow(imcomplement(I));

通过乘法来改变亮度和对比度(乘以数值改变亮度,乘以本身图像改变对比度)J= immultiply(I,0.2);

I16 = uint16(bw8);bw9 = immultiply(I16,I16);

直接灰度变换J = imadjust(I,[0 1],[1

0],1.5);[PNG]【灰度倒置线性变换】灰度的非线性变换J

= 45*log(double(I)+1);figure,imshow(J,[])

直方图灰度变换J = imadjust(I,[0.15

0.9],[0

1]);figure;imshow(J);figure,imhist(J,64)【区域映射】【增强减弱图像的对比度】灰度范围的映射J = imadjust(I,[0 0.2],[0.5

1]);【此方法有利于消除背景区域,然后方便边界的提取】索引图的灰度变换[X,map] = imread('forest.tif');I = ind2gray(X,map);J =

imadjust(I,[],[],0.5);

改变图像以调节对比度抖动 imshow(dither(I));

滤波和灰度调整是为了消除图像中的一些东西目的相同,原理不同J= filter2([1 2;-1 -2],I)

J = filter2([1 2 1;0 0 0;-1 -2 -1],I);

J = filter2(fspecial('sobel'),I);

I2 = imfilter(I,ones(5,5)/25);

中值滤波K =

medfilt2(j);【把杂色过滤掉】

灰度调整与消除背景后的图像相加有异曲同工之妙,都能够进行亮更亮,暗更暗的效果。

处理方法,先进行背影的去除,然后进行(图像相加)增强(第一次增加对比度),然后进行第二次的效果增强。对比度调节,到了这个时候明暗已经很突出了,不过还有些微小的灰度变化,然进行一下彻底地图像二值化,进行完全的黑白分明,这样就能进行边缘提取,要是感觉还不行的话,那就进行形态学的膨胀或腐蚀的操作,消除微小物,然后在进行分割操作

图像的提取

1 背景色的处理

图像消除背景色,先提取背影图,然后再进行图像相减(背景色不一致的可以用这个方法)

背景图的提取(开运算)se = strel('disk',25);

background = imopen(I,se);

figure,imshow(background);

blocks = blkproc(I,[32 32],'min(x(:))');

background = imresize(blocks,[256 256],'bilinear');

�ckground = imresize(blkproc(I,[32 32],'min(x(:))'),[256

256],'bilinear');

减去背景色bw3 = imsubtract(I,background);

图像相减imsubtract(I,J);

图像的开运算imopen(I,se);se =

strel('disk',25);

查看背景色是否正常的方法figure,surf(double(I(1:8:end,1:8:end))),zlim([0 255]);

set(gca,'ydir','reverse');

对一副图像的各种处理中的图像可进行相减来消除一些东西imsubtract(I,J);

两幅同样的图像可以进行背景去除后的相加操作是特征更加明显imadd(I,J);这点和直接加一个数值的区别是后者是整体相加,而前者是亮亮相加

经过灰度变换后使图像的灰度对比很大时就可以进行边缘提取了

边缘提取edge(I);

BW2 = edge(I,'canny');

BW = edge(I,'sobel')

BW = edge(I,'prewitt')

BW = edge(I,'roberts')

BW = edge(I,'log')

bws = edge(I,'sobel',(graythresh(topI)));

先进行灰度划分然后进行边缘检测bws =

edge(I,'sobel',(graythresh(I)*.1));

图像块边缘检测J = nlfilter(I,[3

3],inline('max(x(:))'));【tif】【此方法进行边缘的扩充】【邻域检测】

等高线提取imcontour(I),imcontour(I,3);【过程提取比较慢】

形态学的膨胀imdilate(I,se);【可通过膨胀填补空隙】bw = imdilate(K,[strel('line',3,90) strel('line',3,0)]);

bwsdil =

imdilate(bws,[strel('line',3,90)]);可参照帮助文档strel 和imdilate

空洞的填充J = imfill(I,'holes');J =

imfill(I);

移除与边界连通的目标:J=

imclearborder(I,4);|J= imclearborder(I);

腐蚀操作:se = strel('diamond',1);J = imerode(I,se);J =

imerode(J,se);figure,imshow(J);【此方法可以对边缘进行平滑】

绘制轮廓线J = bwperim(I);

两幅图像叠加的显示segout =

I;segout(bwoutline)=

255;figure,imshow(segout);【一般与绘制轮廓线同时使用,是图像分割的方法】

开运算:bwco =

imopen(bwc,strel('disk',6));

se = strel('rectangle',[40 30]);

bw2 = imopen(bw1,se);【此方法用来删除图像中较小的物体】

闭运算:bwc =

imclose(bw,strel('disk',6));【抽取原始图像较大的物体】

图像“与”操作imshow(bw

& bwco)【包含小对象和包含大对象的图像进行“与”操作,从而得到原始图像中所有的小对象】

骨架化b2 =

bwmorph(b1,'skel',Inf);【骨架提取适用于道路的提取】b3 =

bwmorph(b1,'remove');【骨骼提取、边缘检测最好先将图像转化为二进制图像】

边缘像素测定bw2 = bwperim(bw1);|BW2 =

bwperim(BW1,8);

缨帽变换(高帽滤波变换):J =

imtophat(gI,strel('disk',10));【消除背景中那些亮度不一致的背景】

低帽滤波变换Ibot =

imbothat(afm,se);figure,imshow(Ibot,[]);

增大对象间的间隙【原图像加上高帽滤波变换的图像,然后再减去低帽滤波的图像】

Ienhance =

imsubtract(imadd(Itop,afm),Ibot);

谷点图像的增强,照亮谷点图像Iec = imcomplement(Ienhance);

下面试一下实验的小代码可以作为参考

在每幅图像处理前都可以用下面的方法进行检测《《《《《《《《《《《《《《clear,close all

I = imread('f:\s.png');

%figure,imshow(I);

%I = imread('rice.png');

imshow(I);

figure,imhist(I);

bw = im2bw(I,230/255);

figure;imshow(bw);阈值分割********************边缘检测检测灰度梯度相差很大的区域

clear,close all

%I = imread('f:\s.png');

%figure,imshow(I);

I = imread('rice.png');

figure;

imshow(I);

bw1 = edge(I,'Roberts');

figure,imshow(bw1);

bw2 = edge(I,'sobel');

figure,imshow(bw2);

bw3 = edge(I,'prewitt');

figure,imshow(bw3);

bw4 =edge(I,'canny');

figure,imshow(bw4);

******************************通过图像分割检测细胞

P217

clear,close all

%I = imread('f:\s.png');

%figure,imshow(I);

I = imread('cell.tif');

figure,imshow(I),title('yuanshitu');

bws = edge(I,'sobel',(graythresh(I)*.1));

figure,imshow(bws),title('tiqu');

se90 = strel('line',3,90);

se0 = strel('line',3,0);

bwsdil = imdilate(bws,[se90 se0]);

figure,imshow(bwsdil),title('dilated gradient mask');

bwdfill = imfill(bwsdil,'holes');

figure,imshow(bwdfill);

title('binary image with filled holes');

bwnobord = imclearborder(bwdfill,4);

figure,imshow(bwnobord),title('cleared border image ')

seD = strel('diamond',1);

bwfinal = imerode(bwnobord,seD);

bwfinal = imerode(bwfinal,seD);

figure,imshow(bwfinal),title('segmented image');

bwoutline = bwperim(bwfinal);

Seout = I;

Segout(bwoutline) = 255;

figure,imshow(Seout),title('outlined original image');

****************************************利用图像分割结合形态学操作测试图像中的微小结构

P219

clear,close all

%I = imread('f:\s.png');

%figure,imshow(I);

I = imread('f:\sy.tif');

figure,imshow(I),title('original image');

ic = imcomplement(I);%求反色bw = im2bw(ic,graythresh(ic));%阈值分割figure,imshow(ic),title('complement of image');

figure,imshow(bw);

title('thresholding the image to show small

structures');

se = strel('disk',6);

bwc = imclose(bw,se);

bwco = imopen(bwc,se);

figure,imshow(bwc),title('closing the thresholded image');

figure,imshow(bwco),

title('open the image to show large objects');

mask = bw & bwco;

figure,imshow(mask),title('the "and" of these two

images');

*******************************图像粒度测定

P221

clear,close all

%I = imread('f:\s.png');

%figure,imshow(I);

I = imread('f:\s.png');

figure,imshow(I),title('original image');

ic = imcomplement(I);%求反色figure,imshow(ic);

%I = imread('snowflakes.png');

%figure,imshow(I);

claheI = adapthisteq(I,'NumTiles',[10 10]);

claheI = imadjust(claheI);

figure;

imshow(claheI);

gI = imadjust(im2double(I),[],[0 1]);

figure,imshow(gI),title('adjusted grayscale iamge');

se = strel('disk',10);

topI = imtophat(gI,se);

figure,imshow(topI),title('top-hat iamge');

bws = edge(I,'sobel',(graythresh(topI)));

figure,imshow(bws);

*************************************

%腐蚀膨胀联合操作

P236

clear,close all

%I = imread('f:\s.png');

%figure,imshow(I);

I = imread('f:\s.png');

figure,imshow(I),title('original image');

bw1 = imread('circbw.tif');

figure,imshow(bw1);

se = strel('rectangle',[40 30]);

bw2 = imopen(bw1,se);

figure,imshow(bw2);

bw3 = imerode(I,se);

figure,imshow(bw3);

bw4 = imdilate(I,se);

figure,

imshow(bw4);

*************************************

%骨架化

P237

clear,close all

%I = imread('f:\s.png');

%figure,imshow(I);

b1 = imread('circbw.tif');

b2 = bwmorph(b1,'skel',Inf);

figure,

imshow(b1);

figure,imshow(b2);

b3 = bwmorph(b1,'remove');

imview(b3)

BW = imread('circles.png');

figure,

imshow(BW);

*****************************边界像素检测

P238

bw1 = imread('circbw.tif');

bw2 = bwperim(bw1);

imshow(bw1);

figure,imshow(bw2);

BW1 = imread('circbw.tif');

BW2 = bwperim(BW1,8);

imview(BW1)

imview(BW2)

BW2 = bwmorph(BW,'remove');

imview(BW2)

BW3 = bwmorph(BW,'skel',Inf);

imview(BW3)

********************************基于分水岭的图像

clear,close all

%I = imread('f:\s.png');

%figure,imshow(I);

afm = imread('spine.tif');

figure;

imshow(afm);

se = strel('disk',15);

Itop = imtophat(afm,se);

Ibot = imbothat(afm,se);

figure,imshow(Itop,[]);

figure,imshow(Ibot,[]);

Ienhance = imsubtract(imadd(Itop,afm),Ibot);

figure,imshow(Ienhance),title('original of enhanced

image');

Iec = imcomplement(Ienhance);

figure,imshow(Iec),title('complement of enhanced

image');

Iemin = imextendedmin(Iec,22);

%Iompose = imimposemin(Iec,Iemin);

figure,imshow(Iemin);

%figure,imshow(Iimpose);

I = imread('rice.png');

figure, imshow(I), title('original image')

BW = im2bw(I, graythresh(I));

L = bwlabel(BW);

RGB = label2rgb(L);

RGB2 = label2rgb(L, 'spring', 'c', 'shuffle');

imview(RGB), imshow(RGB2)

rgb = label2rgb(Iemin);

figure,imshow(rgb);

***********************************************************************************************************

I = imread('pout.tif');

imshow(I);

figure,

imhist(I);

I2 = histeq(I);

figure,imshow(I2);

figure,imhist(I2);

imwrite(I2,'pout2.png');

imfinfo('pout2.png');

***************************************

clear,close all

I = imread('rice.png');

figure;

imshow(I)

se = strel('disk',15)

background = imopen(I,se);

figure,

imshow(background);

figure,surf(double(background(1:8:end,1:8:end))),zlim([0

255]);

set(gca,'ydir','reverse');

I2 = imsubtract(I,background);

figure,

imshow(I2)

I3 = imadjust(I2,stretchlim(I2),[0 1]);

figure,

imshow(I3)

level = graythresh(I3);

bw = im2bw(I3,level);

figure,

imshow(bw)

whos

**********************

clear,close all

I = imread('rice.png')

figure,imshow(I),colormap(I)

J= filter2([1 2;-1 -2],I)

imshow(I)

figure,

imshow(J,[])

*******************

clear,close all

moonfig = imread('moon.tif');

imshow(moonfig)

imview(moonfig)

imview close all

****************

clear,close all

I= imread('testpat1.png');

figure,

imshow(I);

J= filter2([1 2;-1 -2],I);

figure,

imshow(J,[]);

clear ,close all

%显示灰度图像load clown

clims = [10 60];

imagesc(X,clims)

colormap(bone)

%colormap('default')

%colormap(gray)

%colormap(winter)

% See also hsv, gray, hot, bone, copper, pink, flag,

colormap,

rgbplot.

clear ,close all

mri = uint8(zeros(128,128,1,27));

for frame= 1: 27

[mri(:,:,:,frame),map]= imread('mri.tif',frame);

end

imshow(mri(:,:,:,3),map);

*****************************************

clear ,close all

mri = uint8(zeros(128,128,1,27));

for frame= 1: 27

[mri(:,:,:,frame),map]= imread('mri.tif',frame);

end

figure;

imshow(mri(:,:,:,1),map);

figure;

montage(mri,map);

figure

mov = immovie(mri,map);

movie(mov);

******************************

clear ,close all

[X1,map1] = imread('forest.tif');

[X2,map2] = imread('trees.tif');

Imshow(X1,map1),

figure,

imshow(X2,map2);

figure;

subplot(1,2,1),imshow(X1,map1);

subplot(1,2,2),imshow(X2,map2)

figure;

subplot(1,2,1),subimage(X1,map1);

subplot(1,2,2),subimage(X2,map2);

figure;

[x,y,z]= cylinder;

I = imread('testpat1.png');

subplot(1,2,1),imshow(I);

subplot(1,2,2),

warp(x,y,z,I);

******************************************

clear ,close all

I = imread('rice.png');

J = filter2(fspecial('sobel'),I);

K = mat2gray(J);

imshow(I)

figure;

imshow(K);

figure,

imshow(J)

******************************

clear ,close all

imview close all

I = imread('snowflakes.png');

X = grayslice(I,16);

imview(I)

imview(X,jet(16))

**********************

clear ,close all

imview close all

RGB = imread('peppers.png');

[X,map] = rgb2ind(RGB,128);

figure;

imshow(X,map);

*************

clear ,close all

imview close all

load trees

bw = im2bw(X,map,0.4);

imshow(X,map);

figure,imshow(bw);

*************

clear ,close all

imview close all

load trees

bw = ind2gray(X,map);

imshow(X,map);

figure,imshow(bw);

*****************

clear ,close all

imview close all

I = imread('cameraman.tif');

BW = dither(I);

imview(BW)

***************

clear ,close all

imview close all

I = imread('rice.png');

J = imread('cameraman.tif');

K = imadd(I,J,'uint16');

subplot(1,3,1),imshow('rice.png');

subplot(1,3,2),imshow('cameraman.tif');

subplot(1,3,3),imshow(K,[])

*************

clear ,close all

imview close all

I = imread('rice.png');

J = imread('cameraman.tif');

K = imadd(I,J,'uint16');

subplot(1,3,1),imshow('rice.png');

subplot(1,3,2),imshow('cameraman.tif');

subplot(1,3,3),imshow(K,[])

figure;

I = imread('rice.png');

J = imadd(I,50);

subplot(1,2,1),imshow(I);

subplot(1,2,2),imshow(J)

******************

clear ,close all

I = imread('rice.png');

%blocks = blkproc(I,[32 32],'min(x(:))');

background = imresize(blkproc(I,[32

32],'min(x(:))'),[256

256],'bilinear');

bw = imsubtract(I,background);

figure,imshow(I);

figure;

imshow(bw,[]);

*******************

I = fitsread('solarspectra.fts');

I1 = mat2gray(I);%数据矩阵转化为灰度图像bw = edge(I1);

figure,

imshow(I);

figure;

imshow(I1),figure,imshow(bw);

*************

clear ,close all

I = imread('rice.png');

figure;

imshow(I);

figure;

imcontour(I);

figure;

I = imread('circuit.tif');

imcontour(I,3)

*********

clear ,close all

I = imread('peppers.png');colormap

imshow(I);

j = imadjust(I,[0 1],[1 0],1.5);

figure;

subimage(j)

clear ,close all

[X,map] = imread('forest.tif');

I = ind2gray(X,map);

J = imadjust(I,[],[],0.5);

imshow(I)

figure,imshow(J);

clear ,close all

[X,map] = imread('forest.tif');

I = ind2gray(X,map);

J = imadjust(I,[0.15 0.5],[0 1],1.5);

%J = imadjust(I,[],[],0.5);

imshow(I)

figure,imshow(J,[]);

J1 = edge(J);

figure;imshow(J1);

************************

I = imread('cameraman.tif');

imshow(I)

figure,imhist(I)

clear ,close all

I = imread('cameraman.tif');

j = imadjust(I,[0 0.2],[0.5 1]);

imshow(j);

figure,imhist(64)

***************

clear ,close all

I = imread('tire.tif');

j = histeq(I);

subplot(1,2,1),imshow(I);

subplot(1,2,2),imshow(j)

figure

subplot(1,2,1);

imhist(I,64);

subplot(1,2,2);

imhist(j,64)

************************

clear ,close all

I = imread('tire.tif');

j = histeq(I);

subplot(1,2,1),imshow(I);

subplot(1,2,2),imshow(j)

figure

subplot(1,2,1);

imhist(I,64);

subplot(1,2,2);

imhist(j,64)

*******************

clear ,close all

%对比度自适应直方图均衡化I = imread('pout.tif');

j= adapthisteq(I);

imshow(I)

figure,

imshow(j)

figure

imhist(j,64)

******************

clear ,close all

%去相关拉伸[I col] = imread('forest.tif');

S = decorrstretch(ind2rgb(I,col));

subplot(2,1,1), imshow(I,col)

subplot(2,1,2), imshow(S)

*****************

clear ,close all

%中值滤波I = imread('eight.tif');

j = imnoise(I,'salt & pepper',0.02);

figure,imshow(I);

figure;

K = medfilt2(j);

subplot(1,2,1),imshow(j)

subplot(1,2,2),imshow(K)

************************

clear,close all

%边缘提取I = imread('circuit.tif');

BW1 = edge(I,'prewitt');

BW2 = edge(I,'canny');

figure;

imshow(I)

figure;

imshow(BW1);

figure, imshow(BW2)

***************************

clear,close all

%边缘提取I = imread('circuit.tif');

BW1 = edge(I,'prewitt');

BW2 = edge(I,'canny');

figure;

imshow(I)

figure;

imshow(BW1);

figure, imshow(BW2)

*********************

clear ,close all

%Otsu自动阈值分割方法

I=imread('coins.png');

figure,imshow(I);

level=graythresh(I); %确定灰度阈值J=im2bw(I,level);

figure,imshow(J)

************************

clear,close all

bw = imread('text.png');

se = strel('line',11,90);%生成线形的结构元素bw2 = imdilate(bw,se);%对图像进行膨胀subplot(121), imshow(bw), title('原始图像')

subplot(122), imshow(bw2), title('膨胀后的图像')

**********************

clear,close all

I = imread('cameraman.tif');

se = strel('ball',5,5);%生成球形的结构元素I2 = imdilate(I,se);%对图像进行膨胀subplot(121), imshow(I), title('原始图像')

subplot(122), imshow(I2), title('膨胀后的图像')

*****************

clear,close all

I = imread('cameraman.tif');

se = strel('ball',5,5);%生成球形的结构元素I2 = imdilate(I,se);%对图像进行膨胀subplot(121), imshow(I), title('原始图像')

subplot(122), imshow(I2), title('膨胀后的图像')

clear,close all

I = imread('cameraman.tif');

I1=256-I;%对图像进行取反操作se = strel('ball',5,5);%生成球形的结构元素I2 = imdilate(I1,se);%对图像进行膨胀I3=256-I2;%对膨胀后的图像取反subplot(121), imshow(I), title('原始图像')

subplot(122), imshow(I3), title('膨胀后的图像')

clear,close all

originalBW = imread('circles.png');

se = strel('disk',11);%生成圆盘形的结构元素

erodedBW = imerode(originalBW,se);%对图像进行腐蚀subplot(121), imshow(originalBW), title('原始图像');

subplot(122), imshow(erodedBW); title('腐蚀后的图像');

clear,close all

I = imread('cameraman.tif');

se = strel('ball',5,5);%生成球形的结构元素I2 = imerode(I,se);%对图像进行腐蚀subplot(121), imshow(I), title('原始图像')

subplot(122), imshow(I2), title('腐蚀后的图像')

clear,close all

BW1 = imread('circbw.tif');%读取图像subplot(131), imshow(BW1); title('原始图像');

SE = strel('rectangle',[40 30]);%生成矩形结构元素BW2 = imerode(BW1,SE);%对图像进行腐蚀subplot(132), imshow(BW2); title('腐蚀后的图像');

BW3 = imdilate(BW2,SE);%对图像进行膨胀subplot(133), imshow(BW3); title('先腐蚀后膨胀的图像');

clear,close all

BW1 = imread('circbw.tif'); %读取图像subplot(121), imshow(BW1); title('????????');

SE = strel('rectangle',[40 30]); %生成矩形结构元素BW2 = imopen(BW1,SE);%对图像直接进行开运算subplot(122), imshow(BW2); title('开运算后的图像');

clear,close all

originalBW = imread('circles.png');

subplot(121), imshow(originalBW); title('原始的图像');

se = strel('disk',10);

closeBW = imclose(originalBW,se);

subplot(122), imshow(closeBW); title('关运算后的图像')

clear,close all

BW1 = imread('circbw.tif');

BW2 = bwmorph(BW1,'skel',Inf);

subplot(121), imshow(BW1) ; title('原始图像');

subplot(122), imshow(BW2) ; title('骨架提取后的图像')

***********************************

BW1 = imread('circbw.tif');

BW2 = bwperim(BW1,8);

subplot(121), imshow(BW1) ; title('原始图像');

subplot(122), imshow(BW2) ; title('边缘检测后的图像');

************************

I = imread('rice.png');

subplot(121), imshow(I) ; title('原始图像');

se = strel('disk',12); %生成圆盘形状的结构元素J = imtophat(I,se); %使用top-hat对图像进行滤波subplot(122), imshow(J) ; title('top-hat滤波后的图像');

**********************

clear,close all

I = imread('pout.tif');

subplot(121), imshow(I) ; title('原图像');

se = strel('disk',3);%生成圆形的结构元素I1=imtophat(I,se);%top-hat滤波I2=imadd(I,I1);%原图像加上top-hat滤波后的图像I3=imbothat(I,se);%bottom-hat滤波J = imsubtract(I2, I3);%再减去bottom-hat滤波后的图像subplot(122), imshow(J) ; title('增强后的图像');

************************

BW = im2bw(imread('coins.png'));%读取图像BW1 = imfill(BW,'holes');%对图像进行填充subplot(121), imshow(BW), title('原始图像')

subplot(122), imshow(BW1); title('填充后的图像')

********************************

I = imread('glass.png');

BW = imextendedmin(I,50);%求取极小值的二值图像,阈值为50

subplot(121), imshow(I), subplot(122), imshow(BW)

*********************************

bw = zeros(5,5);

bw(2,2) = 1; bw(4,4) = 1; %设定像素值为1

[D,L] = bwdist(bw)%计算欧氏距离

*****************

bw = zeros(200,200);

bw(50,50) = 1; bw(50,150) = 1; bw(150,100) = 1;

%设定像素值为1

D1 = bwdist(bw,'euclidean');%使用欧氏距离D2 = bwdist(bw,'cityblock');%使用曼哈顿距离D3 = bwdist(bw,'chessboard');%使用棋盘距离D4 = bwdist(bw,'quasi-euclidean');%使用准欧氏距离figure%依次显示四种距离矩阵并显示等值线subplot(2,2,1), subimage(mat2gray(D1)), %欧氏距离title('Euclidean')

hold on, imcontour(D1)%绘制等值线subplot(2,2,2), subimage(mat2gray(D2)),%曼哈顿距离 title('City block')

hold on, imcontour(D2)%绘制等值线subplot(2,2,3), subimage(mat2gray(D3)),%棋盘距离 title('Chessboard')

hold on, imcontour(D3)%绘制等值线subplot(2,2,4), subimage(mat2gray(D4)),%准欧氏距离 title('Quasi-Euclidean')

hold on, imcontour(D4)%绘制等值线

****************

bw = zeros(50,50,50);

bw(25,25,25) = 1;%设定一个像素值为1

D1 = bwdist(bw,'euclidean');%欧氏距离D2 = bwdist(bw,'cityblock');%曼哈顿距离D3 = bwdist(bw,'chessboard');%棋盘距离D4 = bwdist(bw,'quasi-euclidean');%准欧氏距离figure; subplot(2,2,1), isosurface(D1,15),

%显示欧氏距离axis equal, view(3); camlight, lighting gouraud,

%光照处理title('Euclidean')

subplot(2,2,2), isosurface(D2,15), %显示曼哈顿距离axis equal, view(3); camlight, lighting gouraud,

%光照处理title('City block')

subplot(2,2,3), isosurface(D3,15), %显示棋盘距离axis equal, view(3); camlight, lighting gouraud,

%光照处理title('Chessboard')

subplot(2,2,4), isosurface(D4,15), %显示准欧氏距离axis equal, view(3); camlight, lighting gouraud,

%光照处理title('Quasi-Euclidean')

**********

BW1 = imread('text.png');

c = [43 185 212];%规定了对象的列数r = [38 68 181];%规定了对象的行数BW2 = bwselect(BW1,c,r,4);%选择对象,连通性为4

subplot(121), imshow(BW1), subplot(122),

imshow(BW2),%显示选择的对象

****************

clear,close all

BW1 = imread('text.png'); BW = imread('circbw.tif');

SE = ones(5);

BW2 = imdilate(BW,SE);%对图像膨胀increase = (bwarea(BW2) -

bwarea(BW))/bwarea(BW)%计算面积增加的比例******

BW = imread('circles.png'); imshow(BW);

eul=bweuler(BW)%计算欧拉数***********************************

f = @(x) sum(x(:)) >=

3;%创建查询表使用的函数lut = makelut(f,3);%创建查询表BW1 = imread('text.png');

BW2 = applylut(BW1,lut);%使用查询表操作subplot(121), imshow(BW1); subplot(122), imshow(BW2);

**************************

上面代码有些稍微重复,有些是前面的代码进一步加工处理的结果,看时请自行鉴别其意义!

下面是提取遥感图像上我的家乡的几个村庄

具体图片见相册!

代码如下:(可以借鉴下,这些代码只对原图有效,换张图就完全不一样了)

clear,close all

imview close all

I = imread('c:\家乡.jpg');

bw1 = rgb2gray(I);

surf(double(bw1(1:8:end,1:8:end))),zlim([0

255]);title('背景色观察');

set(gca,'ydir','reverse');

se = strel('disk',25);

background = imopen(bw1,se);

bw2 = imsubtract(bw1,background);

se1=strel('disk',10)

bw3 = imtophat(bw2,se1);

bw4 = imadjust(bw3,stretchlim(bw3),[0 1]);

bw5 =medfilt2(bw4);

bw6 = imadjust(bw5,stretchlim(bw5),[0 1]);

bw7=im2bw(bw4,100/255);

%图像变换以及图像增强(灰度变换、滤波变换)figure;

subplot(3,3,1),imshow(I),title('源图像');

subplot(3,3,2),imshow(bw1),title('灰度图像');

subplot(3,3,3),imshow(background),title('背景图像');

subplot(3,3,4),imshow(bw2),title('背景色消除后图像');

subplot(3,3,5),imshow(bw3),title('缨帽变换');

subplot(3,3,6),imshow(bw4),title('灰度变换1');

subplot(3,3,7),imshow(bw5),title('中值滤波');

subplot(3,3,8),imshow(bw6),title('灰度变换2');

subplot(3,3,9),imshow(bw7),title('灰度直方图二值化');

%图形学处理、特征提取se2 = strel('disk',5);

e1 = imclose(bw7,se2);

se3 = strel('rectangle',[4 3]);

bw8 = imerode(e1,se3);

e2 = imopen(bw8,se2);

se4 = strel('rectangle',[3 4]);

bw9 = imerode(bw8,se4);

bw10 = imfill(bw9,'holes');

se5 = strel('diamond',10);

bw11 = imerode(bw10,se5);

se6 = strel('rectangle',[5 4]);

bw12 = imerode(bw11,se6);

se7 = strel('rectangle',[25 20]);

bw13 = imdilate(bw12,se7);

bw14 = bwperim(bw13);

bw15 = I;

bw15(bw14)= 255;

figure,

subplot(3,3,1),imshow(e1),title('闭运算');

subplot(3,3,2),imshow(bw8),title('腐蚀1');

subplot(3,3,3),imshow(e2),title('开运算');

subplot(3,3,4),imshow(bw9),title('腐蚀2');

subplot(3,3,5),imshow(bw10),title('填补空洞');

subplot(3,3,6),imshow(bw11),title('腐蚀3');

subplot(3,3,7),imshow(bw12),title('腐蚀4');

subplot(3,3,8),imshow(bw13),title('膨胀');

subplot(3,3,9),imshow(bw14),title('边缘提取');

figure,imshow(bw15);title('叠加合成');

imview(bw15);

blog_9e65a11e01013rfv.html

blog_9e65a11e01013rfv.html

blog_9e65a11e01013rfv.html

最终结果略......

matlab终结

灰度处理和图像增强

图像提取、形态学处理、图像分割

图像格式的转化

图像的输出

显示:hist(I);

whos

imfinfo('')

imwrite(I,'XX.tif');

纹理映射方式观察[x,y,z]= cylinder;warp(x,y,z,I);

指定输出图像像素的大小:J =

imresize(I,1.25);【tif】

图像的旋转:J =

imrotate(I,90,'bilinear');【tif】

图像的裁剪:J = imcrop(I,[60 40 100

90]);【tif】

区域属性度量 stats =

regionprops(bwlabel(I),'all');stats(23)【png】【也不用imshow】

灰度调整:增强(只改变灰度和颜色,不消除物体,不改变图像的原有物质)imshow(I,[]);

histeq(I)

adapthisteq(I)

imsubtract(I,50)(图像相减)

灰度调节imadjust(I,stretchlim(I),[]);{提高对比度}

imadjust(I,stretchlim(I),[0 1]);

直方图均衡自适应调整bw1 =

imadjust(adapthisteq(I,'NumTiles',[10 10]));

图像的二值化level = graythresh(I),J =

im2bw(I,level);

J = im2bw(I,graythresh(I));【自动灰度阈值分割】

自定义阈值分割figure,imhist(I);J=im2bw(I,100/255);【通过灰度来进行二值化】

二值图像的调节色彩imshow(bw,[1 0 0;0 1

0])

求反色imshow(~I)

求补色imshow(imcomplement(I));

通过乘法来改变亮度和对比度(乘以数值改变亮度,乘以本身图像改变对比度)J= immultiply(I,0.2);

I16 = uint16(bw8);bw9 = immultiply(I16,I16);

直接灰度变换J = imadjust(I,[0 1],[1

0],1.5);[PNG]【灰度倒置线性变换】 灰度的非线性变换J

= 45*log(double(I)+1);figure,imshow(J,[])

直方图灰度变换J = imadjust(I,[0.15

0.9],[0

1]);figure;imshow(J);figure,imhist(J,64)【区域映射】【增强减弱图像的对比度】 灰度范围的映射J = imadjust(I,[0 0.2],[0.5

1]);【此方法有利于消除背景区域,然后方便边界的提取】 索引图的灰度变换[X,map] = imread('forest.tif');I = ind2gray(X,map);J =

imadjust(I,[],[],0.5);

改变图像以调节对比度 抖动

imshow(dither(I));

滤波和灰度调整是为了消除图像中的一些东西 目的相同,原理不同J= filter2([1 2;-1 -2],I)

J = filter2([1 2 1;0 0 0;-1 -2 -1],I);

J = filter2(fspecial('sobel'),I);

I2 = imfilter(I,ones(5,5)/25);

中值滤波K =

medfilt2(j);【把杂色过滤掉】

灰度调整与消除背景后的图像相加有异曲同工之妙,都能够进行亮更亮,暗更暗的效果。

处理方法,先进行背影的去除,然后进行(图像相加)增强(第一次增加对比度),然后进行第二次的效果增强。对比度调节,到了这个时候明暗已经很突出了,不过还有些微小的灰度变化,然进行一下彻底地图像二值化,进行完全的黑白分明,这样就能进行边缘提取,要是感觉还不行的话,那就进行形态学的膨胀或腐蚀的操作,消除微小物,然后在进行分割操作

图像的提取

1 背景色的处理

图像消除背景色,先提取背影图,然后再进行图像相减(背景色不一致的可以用这个方法)

背景图的提取(开运算)se = strel('disk',25);

background = imopen(I,se);

figure,imshow(background);

blocks = blkproc(I,[32 32],'min(x(:))');

background = imresize(blocks,[256 256],'bilinear');

�ckground = imresize(blkproc(I,[32 32],'min(x(:))'),[256

256],'bilinear');

减去背景色bw3 =

imsubtract(I,background);

图像相减imsubtract(I,J);

图像的开运算imopen(I,se);se =

strel('disk',25);

查看背景色是否正常的方法figure,surf(double(I(1:8:end,1:8:end))),zlim([0 255]);

set(gca,'ydir','reverse');

对一副图像的各种处理中的图像可进行相减来消除一些东西imsubtract(I,J);

两幅同样的图像可以进行背景去除后的相加操作是特征更加明显imadd(I,J); 这点和直接加一个数值的区别是后者是整体相加,而前者是亮亮相加

经过灰度变换后使图像的灰度对比很大时就可以进行边缘提取了

边缘提取edge(I);

BW2 = edge(I,'canny');

BW = edge(I,'sobel')

BW = edge(I,'prewitt')

BW = edge(I,'roberts')

BW = edge(I,'log')

bws = edge(I,'sobel',(graythresh(topI)));

先进行灰度划分然后进行边缘检测bws =

edge(I,'sobel',(graythresh(I)*.1));

图像块边缘检测J = nlfilter(I,[3

3],inline('max(x(:))'));【tif】【此方法进行边缘的扩充】【邻域检测】

等高线提取imcontour(I),imcontour(I,3);【过程提取比较慢】

形态学的膨胀imdilate(I,se); 【可通过膨胀填补空隙】bw = imdilate(K,[strel('line',3,90) strel('line',3,0)]);

bwsdil = imdilate(bws,[strel('line',3,90)]);

可参照帮助文档strel

和imdilate

空洞的填充J = imfill(I,'holes');J =

imfill(I);

移除与边界连通的目标:J=

imclearborder(I,4);|J= imclearborder(I);

腐蚀操作:se = strel('diamond',1);J

= imerode(I,se);J =

imerode(J,se);figure,imshow(J);【此方法可以对边缘进行平滑】

绘制轮廓线J =

bwperim(I);

两幅图像叠加的显示segout =

I;segout(bwoutline)=

255;figure,imshow(segout);【一般与绘制轮廓线同时使用,是图像分割的方法】

开运算:bwco =

imopen(bwc,strel('disk',6));

se = strel('rectangle',[40 30]);

bw2 = imopen(bw1,se);【此方法用来删除图像中较小的物体】

闭运算:bwc =

imclose(bw,strel('disk',6));【抽取原始图像较大的物体】

图像“与”操作imshow(bw

& bwco)【包含小对象和包含大对象的图像进行“与”操作,从而得到原始图像中所有的小对象】

骨架化b2 =

bwmorph(b1,'skel',Inf);【骨架提取适用于道路的提取】b3 =

bwmorph(b1,'remove');【骨骼提取、边缘检测最好先将图像转化为二进制图像】

边缘像素测定bw2 = bwperim(bw1);|BW2 =

bwperim(BW1,8);

缨帽变换(高帽滤波变换):J =

imtophat(gI,strel('disk',10));【消除背景中那些亮度不一致的背景】

低帽滤波变换Ibot =

imbothat(afm,se);figure,imshow(Ibot,[]);

增大对象间的间隙【原图像加上高帽滤波变换的图像,然后再减去低帽滤波的图像】

Ienhance =

imsubtract(imadd(Itop,afm),Ibot);

谷点图像的增强,照亮谷点图像Iec = imcomplement(Ienhance);

Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000