Demo entry 6357149

1

Submitted by 1 on Apr 21, 2017 at 09:05
Language: Matlab. Code size: 1.3 kB.

```Image=imread('00.png');
imshow(Image);
R=Image(:,:,1); G=Image(:,:,2); B=Image(:,:,3);
diff_R=0; diff_G=0; diff_B=0;
XYR=~((R-G)>diff_R&(R-B)>diff_R);  % 提取红色条件是R分量与G、B分量差值大于设定
XYG=~((G-R)>diff_G&(G-B)>diff_G);  % 提取绿色条件是G分量与R、B分量差值大于设定
XYB=~((B-R)>diff_B&(B-G)>diff_B);  % 提取蓝色条件是B分量与R、G分量差值大于设定
%红色色块中心点
[y_r,x_r]=find(XYR==0);
B_r=[x_r,y_r];
[y1_r,I_r] = max(B_r(:,2));
x1_r=B_r(I_r,1);
[y2_r,S_r] = min(B_r(:,2));
x2_r=B_r(S_r,1);
[x3_r,M_r] = min(B_r(:,1));
y3_r=B_r(M_r,2);
[x4_r,N_r] = max(B_r(:,1));
y4_r=B_r(N_r,2);
x0_r=((x1_r+x2_r+x3_r+x4_r)/4);
y0_r=((y1_r+y2_r+y3_r+y4_r)/4);

%绿色色块中心点
[y_g,x_g]=find(XYG==0);
B_g=[x_g,y_g];
[y1_g,I_g] = max(B_g(:,2));
x1_g=B_g(I_g,1);
[y2_g,S_g] = min(B_g(:,2));
x2_g=B_g(S_g,1);
[x3_g,M_g] = min(B_g(:,1));
y3_g=B_g(M_g,2);
[x4_g,N_g] = max(B_g(:,1));
y4_g=B_g(N_g,2);
x0_g=((x1_g+x2_g+x3_g+x4_g)/4);
y0_g=((y1_g+y2_g+y3_g+y4_g)/4);

%蓝色色块中心点
[y_b,x_b]=find(XYB==0);
B_b=[x_b,y_b];
[y1_b,I_b] = max(B_b(:,2));
x1_b=B_b(I_b,1);
[y2_b,S_b] = min(B_b(:,2));
x2_b=B_b(S_b,1);
[x3_b,M_b] = min(B_b(:,1));
y3_b=B_b(M_b,2);
[x4_b,N_b] = max(B_b(:,1));
y4_b=B_b(N_b,2);
x0_b=((x1_b+x2_b+x3_b+x4_b)/4);
y0_b=((y1_b+y2_b+y3_b+y4_b)/4);

%连接顶点
hold on;
line([x0_r,x0_g],[y0_r,y0_g]);
hold on;
line([x0_r,x0_b],[y0_r,y0_b]);
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.