matlab程序 谁能读

matlab程序 谁能读,第1张

这函数不难啊,就是长了些

clc

clear all

%reading and showing

f1=imread('foreground.jpg') %读图到f1

%converting color image to grayimage

f2=rgb2gray(f1) %变成灰度图象

figure(1),imshow(f2)%显示

%reading and showing

f3=imread('background.jpg') %读背景图

%converting color image to grayimage

f4=rgb2gray(f3) %转成灰度图象

figure(2),imshow(f4) %显示背景图

%background subtracting

f5=abs(double(f2)-double(f4)) %第一幅图减去背景图

[M N]=size(f5)

T=20

for i=1:M

for j=1:N

if f5(i,j)>T

f5(i,j)=255

else

f5(i,j)=0

end

end

end

f5=uint8(f5)%运算后的图转为uint8型

figure(3)

imshow(f5) % 显示减去背景后的图

% imclose the image

se=strel('disk',3)

f6=imclose(f5,se) %用半径为3的圆对图进行闭 *** 作

figure(4)

imshow(f6) %显示结果

% fill the image

f8=imfill(f6) %对图象进行填充

figure(5)

imshow(f8) %显示填充结果

% smooth the image

f9=double(f8)/255

f10=medfilt2(f9,[3 3]) %中值滤波

figure(6)

imshow(f10,[]) %对图象进行平滑,并显示结果

% add label to the image

bw1=im2bw(f10)%将图形转成二值图像

[x,num]=bwlabel(bw1,4) %对4连通的连通域进行标注

% calculate the number of pixels of each labeled area

long=1:num

for i=1:num

[r,c]=find(x==i) %计算每个连通域的像素数

rc=[r c]

long(i)=size(rc,1)

end

% display the number of pixels of each labeled area

for j=1:num

disp(long(j)) %显示每个连通域的像素数

end

% calculate which area has the biggest number of pixels

max=0

for k=1:num

if(long(k)>max) %计算象素数最多的连通域

max=long(k)

m=k

end

end

% the label of the biggest area

m %最大的连通域的标号

% the number of pixels of the biggest area

max %最大的连通域的象素数

% get the image of the biggest area

[row column]=size(x)

for i=1:row

for j=1:column

if(x(i,j)==m) %取图像的最大连通域

x(i,j)=m

else x(i,j)=0

end

end

end

se=strel('disk',2)

x=imerode(x,se) %用半径为2的圆腐蚀图像

% display the image

figure(7),imshow(x) %显示结果

% get the edge of the image

y=edge(x,'canny') %用canny方法提取边界

figure(8),imshow(y) %显示提取的边界

你这个是提取原图像中最大物体的边界的程序吧!

好长阿,看得我眼睛都酸了!

给我追加一点儿辛苦分吧!

不知道你为什么要生成第一、二、三列分别表示每个像素的r、g、b值的矩阵,其实matlab自动把三个通道分开是有道理的,处理会很方便,如果你一定要生产你想要的矩阵,可尝试以下几种方法:

1.以进制的格式读图片,然后去掉图像的信息头(注意每个格式的头文件包含的字节数都不一样的,以真彩色BMP为例,其头文件有54个字节,去掉着54个字节后就是图像的RGB信息了)

2.第二种会稍微方便些,用imread读图生成一个三维矩阵,然后你新建一个你想要的二维矩阵,把前面得到的RGB信息填充进去


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8006475.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存