matlab初学者

matlab识别色块

azaz

    clc;
Source_img=imread("111.jpg");
[r,lie]=size(Source_img(:,:,2));
read_l=20;
x=1:lie;
Rvalue=Source_img(read_l,:,1);
Gvalue=Source_img(read_l,:,2);
Bvalue=Source_img(read_l,:,3);
Gray_img=zeros(1,lie);
for j=1:lie
    Gray_img(read_l,j)=round(Source_img(read_l,j,1)*0.3+Source_img(read_l,j,2)*0.6+Source_img(read_l,j,3)*0.1);
end
figure(1);
subplot(2,2,1);
plot(x,Rvalue,"r--");
title("R");
subplot(2,2,2);
plot(x,Gvalue,"g--");
title("G");
subplot(2,2,3);
plot(x,Bvalue,"b--");
title("B");
subplot(2,2,4);
plot(x,Gray_img,"k-");
title("Gray");
figure
imshow(Source_img);
Sourcehsv=rgb2hsv(Source_img);
imshow(Sourcehsv)
Sourcenew=255*ones(size(Source_img));
Sourcenewhsv=rgb2hsv(Sourcenew);
imshow(Sourcenew)
[row,col]=ind2sub(size(Sourcehsv),find(Sourcehsv(:,:,3)<0.3));
for i=1:length(row)
    Sourcenewhsv(row(i),col(i),:)=Sourcehsv(row(i),col(i),:);
end
result=hsv2rgb(Sourcenewhsv);
figure
gray=rgb2gray(result);
con=imadjust(gray);
con1=medfilt2(con,[5,5]);
pro1=graythresh(con1);
pro2=im2bw(con1,pro1);
imshow(pro2)
pro3=edge(pro2,'sobel',0.3);
imshow(pro3)
se90=strel('line',7,90);
se0=strel('line',7,0);
pro4=imdilate(pro3,[se90,se0]);
figure,
imshow(pro4);
pro5=imfill(pro4,'holes');
figure,imshow(pro5);
pro6=imclearborder(pro5,4);
figure,imshow(pro6);
se=strel("octagon",6);
pro7=imerode(pro6,se);

imshow(pro7)
[pro8 num]=bwlabel(pro7,4);
num
location=zeros(num,2);

for i=1:num
[r, c] = find(pro8==i);
rs=0;
cs=0;
for j=1:size(r)
    rs=r(j)+rs;
    cs=c(j)+cs;
end
ra=rs/numel(r);
ca=cs/numel(c);
location(i,1)=ra;
location(i,2)=ca;
end
size1=size(Source_img);
draw=zeros(size1(1,1),size1(1,2));
for i=1:num
draw(round(location(i,1)),round(location(i,2)))=1;
t=deg2rad(0:360);
y=round(location(i,1))+50*cos(t);
x=round(location(i,2))+50*sin(t);
plot(x,y);hold on;
fill(x,y,'r')
end
joint=sortrows(location,1);
joint2=sortrows(joint(2:7,:),2);
joint3=sortrows(joint(8:9,:),2);
joint4=sortrows(joint(10:11,:),2);
joint5=sortrows(joint(12:13,:),2);
joint(2:13,:)=[joint2;joint3;joint4;joint5];
x1 = joint(3,2);y1 = joint(3,1);
x2 = joint(4,2);y2 =joint(4,1);
x3 = joint(8,2);y3 = joint(8,1);
a2 = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
b2 = (x3-x2)*(x3-x2)+(y3-y2)*(y3-y2);
c2 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
a = sqrt(a2);
b = sqrt(b2);
cc = sqrt(c2);
pos = (a2+b2-c2)/(2*a*b);   
angle = acos(pos);       
realangle = angle*180/pi;   
disp(realangle);%算角度
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值