简单图像中 监测图像物品位置

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<br>function [] = test()   
 
clc;
clear;
% Path="C:\Users\Administrator\Desktop\塑料瓶\0.jpg";%图片路径
% Path="C:\Users\Administrator\Desktop\塑料瓶\纯透明塑料瓶.jpg";%图片路径
% Path="C:\Users\Administrator\Desktop\塑料瓶\深色塑料瓶.jpg";%图片路径
% Path="C:\Users\Administrator\Desktop\塑料瓶\透明蓝色塑料瓶.jpg";%图片路径
Path="C:\Users\Administrator\Desktop\塑料瓶\透明绿色塑料瓶.jpg";%图片路径
Source_img=imread(Path);%获取图片信息
% figure(6);imshow(Source_img);
 
[r,lie]=size(Source_img(:,:,2));%获取行列信息
bottle_bottom_XY=[-1,-1];
bottle_top=-1;
bottle_bottom=-1;
bottle_left=-1;
bottle_right=-1;
num=9;
!算出指定位点与标志行列的差别  判断该点是否在瓶子图片内
X_D=round(r/num);%round四舍五入取整函数 算出行取值的等差
Y_D=round(lie/num);%round四舍五入取整函数  算出列取值的等差
 
X_label=(1:X_D:r);
Y_label=(1:Y_D:lie);
 
!RGB->LAB
S_lab=zeros(r,lie,3);
cform = makecform('srgb2lab');
lab = applycform(Source_img, cform);
l=lab(:,:,1);
a=lab(:,:,2);
b=lab(:,:,3);
 
% figure(1),imshow(l);
% figure(1),imshow(a);
% figure(1),imshow(b);
for i=X_label
    for j=Y_label
        ti=round(i/X_D)+1;
        tj=round(j/Y_D)+1;
%         ii=lab(i,j,1)*lab(i,j,1);
%         jj=lab(i,j,2)*lab(i,j,2);
%         kk=lab(i,j,3)*lab(i,j,3);
%        if(ii+jj+kk>100)
       if(lab(i,j,1)>100)
%             S_lab(i,j,1)=lab(i,j,1);
%             S_lab(i,j,2)=lab(i,j,2)
%             S_lab(i,j,3)=lab(i,j,3);
              [i,j]
              lab(i,j,1)
       end
    end
end
% figure(5);imshow(S_lab);
A_R=zeros(num,num);
A_G=zeros(num,num);
A_B=zeros(num,num);
for i=X_label
    for j=Y_label
        ti=round(i/X_D)+1;
        tj=round(j/Y_D)+1;
        A_R(ti,tj)=Source_img(i,j,1);
        A_G(ti,tj)=Source_img(i,j,2);
        A_B(ti,tj)=Source_img(i,j,3);
    end
end
 
S_R=zeros(num,num);
S_G=zeros(num,num);
S_B=zeros(num,num);
 
for i=X_label
    for j=Y_label
        ti=round(i/X_D)+1;
        tj=round(j/Y_D)+1;
        S_R(ti,tj)=(A_R(1,tj)-A_R(ti,tj))*(A_R(1,tj)-A_R(ti,tj)) + (A_R(ti,1)-A_R(ti,tj))*(A_R(ti,1)-A_R(ti,tj));
        S_G(ti,tj)=(A_G(1,tj)-A_G(ti,tj))*(A_G(1,tj)-A_G(ti,tj)) + (A_G(ti,1)-A_G(ti,tj))*(A_G(ti,1)-A_G(ti,tj));
        S_B(ti,tj)=(A_B(1,tj)-A_B(ti,tj))*(A_B(1,tj)-A_B(ti,tj)) + (A_B(ti,1)-A_B(ti,tj))*(A_B(ti,1)-A_B(ti,tj));
    end
end
SS=zeros(num,num);
for i=X_label
    for j=Y_label
        ti=round(i/X_D)+1;
        tj=round(j/Y_D)+1;
       SS(ti,tj)=S_R(ti,tj)+S_G(ti,tj)+S_B(ti,tj);
    end
end
 
S_end=zeros(num,num);
for i=X_label
    for j=Y_label
        ti=round(i/X_D)+1;
        tj=round(j/Y_D)+1;
       if( SS(ti,tj)>250)
           S_end(ti,tj)=SS(ti,tj);
       else
           S_end(ti,tj)=0;
       end
    end
end
!判断瓶子顶部
flag=0;
for i=X_label
    if(flag==1)
        break;
    end
    for j=Y_label
        ti=round(i/X_D)+1;
        tj=round(j/Y_D)+1;
       if(S_end(ti,tj)>0)
           flag=1;
           bottle_top=ti;
           break;
       end
    end
end
% length(X_label)
% X_label(end:-1:1)
!判断瓶子底部
flag=0;
for i=X_label(end:-1:1)
    if(flag==1)
        break;
    end
    for j=Y_label
        ti=round(i/X_D)+1;
        tj=round(j/Y_D)+1;
       if(S_end(ti,tj)>0)
           flag=1;
%            ti
%            S_end(ti-1,tj)
%            S_end(ti+1,tj)
           bottle_bottom=length(X_label)-ti+1;
           bottle_color=[Source_img(i,j,1),Source_img(i,j,2),Source_img(i,j,3)];
           break;
       end
    end
end
!判断瓶子左边界部
flag=0;
for j=Y_label
    if(flag==1)
        break;
    end
    for i=X_label
       tj=round(j/Y_D)+1;
       ti=round(i/X_D)+1;
       if(S_end(ti,tj)>0)
           flag=1;
           bottle_left=tj;
           break;
       end
    end
end
!判断瓶子右边界部
flag=0;
for j=Y_label(end:-1:1)
    if(flag==1)
        break;
    end
    for i=X_label
        ti=round(i/X_D)+1;
        tj=round(j/Y_D)+1;
       if(S_end(ti,tj)>0)
           flag=1;
           bottle_right=tj;
           break;
       end
    end
end
 
% S_end
% bottle_color;
% bottle_top%11
% bottle_bottom%11
% bottle_left%41
% bottle_right%62

  

posted @   古路无行客  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示