## A matlab code for detection of tiles on production line

As our tutor is interested in cooperating with company, we are working on a project to detect tiles on the production line. Image processing with matlab, we can determine the position and rotation of the tile correctly.

Codes are below:

```tic

%预设值
bwg=0.3;%二值化阈值
siz=[10700,11400];%瓷砖面积最大最小值

%统计各区域大小
Ibw=im2bw(I,bwg);
I2=zeros(size(Ibw));
[L,num]=bwlabel(Ibw);
s=zeros(num,1);
for i=1:num
[r,c]=find(L==i);
[r1,c1]=size(r);
s(i,1)=r1;
end

%保留符合条件的瓷砖
Itile=zeros(size(Ibw));
for i=1:num
[r,c]=find(L==i);
[r1,c1]=size(r);
if r1>siz(1) & r1<siz(2)
for j=1:r1
Itile(r(j),c(j))=1;
end
end
end

%计算中心点和角度
[L,num]=bwlabel(Itile);
loc=zeros(num,3);			//center_x,center_y
for i=1:num
[r,c]=find(L==i);
point1=[r(1),c(1)];		//min node
point2=[r(1),c(1)];		//max node
point3=[r(1),c(1)];		//bottomest node
[r1,c1]=size(r);
for j=1:r1
if r(j)+c(j)<point1(1)+point1(2)
point1(1)=r(j);
point1(2)=c(j);
end
if r(j)+c(j)>point2(1)+point2(2)
point2(1)=r(j);
point2(2)=c(j);
end
if r(j)>point3(1)
point3(1)=r(j);
point3(2)=c(j);
end
end
loc(i,1)=round((point1(1)+point2(1))*0.5);
loc(i,2)=round((point1(2)+point2(2))*0.5);
if point3(2)<loc(i,2)
loc(i,3)=-180*atan((point2(1)-point3(1))/(point2(2)-point3(2)));
else
point3(1)=r(1);
point3(2)=c(1);
for j=1:r1
if r(j)>point3(1)& c(j)==c(1)
point3(1)=r(j);
end
end
loc(i,3)=-180*atan((point2(1)-point3(1))/(point2(2)-point3(2)));
end
end
loc

%效果显示
I2=I;
[high,wide]=size(I);
for i=1:high
for j=1:wide
if Itile(i,j)
I2(i,j)=255;
end
end
end
for i=1:num
for j=-i:i
I2(loc(i,1)+j,loc(i,2))=0;
I2(loc(i,1),loc(i,2)+j)=0;
end
end
imshow(I);
figure,imshow(I2);

toc
t=toc```

Sample pictures:

Target tiles are marked with little plus in their centers.

This is the preliminary version of codes. Later, we realize the algorithm in various embedded system using opencv.

• hokurikustr

• refrain