詳解基于Matlab的空心散點檢測
問題描述
有一張這樣的圖片,如何提取里面的紅色圈圈坐標,并且連接這些坐標形成兩個封閉的環(huán)路?

過程展示
圖像導入
oriPic=imread('test1.png');
subplot(2,2,1)
imshow(oriPic)依據RGB值圖像二值化
原理就是圖中顏色種類比較少,只有紅黑白,而紅色和白色都是R通道數值較大,因此我們可以利用這一點進行圖像分割
% 刪除紅色外的部分并構造二值圖 grayPic=rgb2gray(oriPic); grayPic(oriPic(:,:,1)<250)=255; grayPic(grayPic<250)=0; %subplot(2,2,2) figure imshow(grayPic)

圖像腐蝕
對于白色來說是腐蝕,對于黑色來說是膨脹,這一步是為了讓那些有缺口的小圓圈將缺口補起來
% 圖像膨脹,使未連接邊緣連接 SE=[0 1 0;1 1 1;0 1 0]; bwPic=imerode(grayPic,SE); figure imshow(bwPic)

圖像邊緣清理
就是把和邊緣連接的不被黑色包圍的區(qū)域變成黑色:
% 邊緣清理:保留圓圈聯(lián)通區(qū)域 bwPic=imclearborder(bwPic); %subplot(2,2,3) figure imshow(bwPic)

聯(lián)通區(qū)域查找與坐標均值計算
現(xiàn)在每一個白點都是一個坐標區(qū)域,我們檢測所有聯(lián)通區(qū)域并計算各個區(qū)域的重心即可:
% 獲取每一個聯(lián)通區(qū)域
[LPic,labelNum]=bwlabel(bwPic);
% 計算每一個聯(lián)通區(qū)域 坐標均值
pointSet=zeros(labelNum,2);
for i=1:labelNum
[X,Y]=find(LPic==i);
Xmean=mean(X);
Ymean=mean(Y);
pointSet(i,:)=[Xmean,Ymean];
end
% 畫個圖展示一下
%subplot(2,2,4)
figure
imshow(bwPic)
hold on
scatter(pointSet(:,2),pointSet(:,1),'r','LineWidth',1)
可以看出定位結果還是非常準確的:

圈查找
就以一個點開始不斷找最近的點唄,沒啥好說的:
n=1;
while ~isempty(pointSet)
circleSetInd=1;
for j=1:length(pointSet)
disSet=sqrt(sum((pointSet-pointSet(circleSetInd(end),:)).^2,2));
[~,ind]=sort(disSet);
ind=ind(1:5);
[~,~,t_ind]=intersect(circleSetInd,ind);
ind(t_ind)=[];
if ~isempty(ind)
circleSetInd=[circleSetInd;ind(1)];
else
circleSet{n}=pointSet(circleSetInd,:);
pointSet(circleSetInd,:)=[];
n=n+1;
break
end
end
end
figure
imshow(oriPic)
hold on
for i=1:n-1
plot(circleSet{i}(:,2),circleSet{i}(:,1),'LineWidth',2)
end
這效果就很美滋滋:

完整代碼
function redPnt
oriPic=imread('test1.png');
%subplot(2,2,1)
figure
imshow(oriPic)
% 刪除紅色外的部分并構造二值圖
grayPic=rgb2gray(oriPic);
grayPic(oriPic(:,:,1)<250)=255;
grayPic(grayPic<250)=0;
%subplot(2,2,2)
figure
imshow(grayPic)
% 圖像膨脹,使未連接邊緣連接
SE=[0 1 0;1 1 1;0 1 0];
bwPic=imerode(grayPic,SE);
figure
imshow(bwPic)
% 邊緣清理:保留圓圈聯(lián)通區(qū)域
bwPic=imclearborder(bwPic);
%subplot(2,2,3)
figure
imshow(bwPic)
% 獲取每一個聯(lián)通區(qū)域
[LPic,labelNum]=bwlabel(bwPic);
% 計算每一個聯(lián)通區(qū)域 坐標均值
pointSet=zeros(labelNum,2);
for i=1:labelNum
[X,Y]=find(LPic==i);
Xmean=mean(X);
Ymean=mean(Y);
pointSet(i,:)=[Xmean,Ymean];
end
%subplot(2,2,4)
figure
imshow(bwPic)
hold on
scatter(pointSet(:,2),pointSet(:,1),'r','LineWidth',1)
n=1;
while ~isempty(pointSet)
circleSetInd=1;
for j=1:length(pointSet)
disSet=sqrt(sum((pointSet-pointSet(circleSetInd(end),:)).^2,2));
[~,ind]=sort(disSet);
ind=ind(1:5);
[~,~,t_ind]=intersect(circleSetInd,ind);
ind(t_ind)=[];
if ~isempty(ind)
circleSetInd=[circleSetInd;ind(1)];
else
circleSet{n}=pointSet(circleSetInd,:);
pointSet(circleSetInd,:)=[];
n=n+1;
break
end
end
end
figure
imshow(oriPic)
hold on
for i=1:n-1
plot(circleSet{i}(:,2),circleSet{i}(:,1),'LineWidth',2)
end
end
其它形狀空心散點檢測
來波正方形試試:






可以看出效果還是很棒的,當然大家可以根據實際情況自行更改圖像腐蝕模板形狀,如果散點是其它顏色請自行更改第一步的圖像分割條件。
后注:
若是因為點較為密集而導致圈形路徑內部白色區(qū)域沒被清除,可能會將內部區(qū)域也算作散點造成錯誤,解決方法是計算每個聯(lián)通區(qū)域面積并剔除遠遠大于區(qū)域面積中位數的聯(lián)通區(qū)域:
問題出現(xiàn)原因的圖片描述:


如圖所示種間那一大片區(qū)域也被算作散點

更改后代碼如下:
function redPnt
oriPic=imread('test2.png');
figure
imshow(oriPic)
% 刪除紅色外的部分并構造二值圖
grayPic=rgb2gray(oriPic);
grayPic(oriPic(:,:,1)<250)=255;
grayPic(grayPic<250)=0;
figure
imshow(grayPic)
% 圖像膨脹,使未連接邊緣連接
SE=[0 1 0;1 1 1;0 1 0];
bwPic=imerode(grayPic,SE);
figure
imshow(bwPic)
% 邊緣清理:保留圓圈聯(lián)通區(qū)域
bwPic=imclearborder(bwPic);
figure
imshow(bwPic)
% 獲取每一個聯(lián)通區(qū)域
[LPic,labelNum]=bwlabel(bwPic);
% 篩掉超大區(qū)域
pointSizeSet=zeros(1,labelNum);
for i=1:labelNum
pointSizeSet(i)=sum(sum(LPic==i));
end
[~,ind]=find(pointSizeSet>10*median(pointSizeSet));
% 計算每一個聯(lián)通區(qū)域 坐標均值
pointSet=zeros(labelNum,2);
for i=1:labelNum
[X,Y]=find(LPic==i);
Xmean=mean(X);
Ymean=mean(Y);
pointSet(i,:)=[Xmean,Ymean];
end
pointSet(ind,:)=[];
figure
imshow(bwPic)
hold on
scatter(pointSet(:,2),pointSet(:,1),'r','LineWidth',1)
n=1;
while ~isempty(pointSet)
circleSetInd=1;
for j=1:length(pointSet)
disSet=sqrt(sum((pointSet-pointSet(circleSetInd(end),:)).^2,2));
[~,ind]=sort(disSet);
ind=ind(1:min(5,length(ind)));
[~,~,t_ind]=intersect(circleSetInd,ind);
ind(t_ind)=[];
if ~isempty(ind)
circleSetInd=[circleSetInd;ind(1)];
else
circleSet{n}=pointSet(circleSetInd,:);
pointSet(circleSetInd,:)=[];
n=n+1;
break
end
end
end
figure
imshow(oriPic)
hold on
for i=1:n-1
plot(circleSet{i}(:,2),circleSet{i}(:,1),'LineWidth',2)
end
end
注:
2016版本及以前可能這句:
disSet=sqrt(sum((pointSet-pointSet(circleSetInd(end),:)).^2,2));
會出現(xiàn)數組大小不匹配問題,可以將其改為:
tempMat=repmat(pointSet(circleSetInd(end),:),[size(pointSet,1),1]); disSet=sqrt(sum((pointSet-tempMat).^2,2));
以上就是詳解基于Matlab的空心散點檢測的詳細內容,更多關于Matlab空心散點檢測的資料請關注腳本之家其它相關文章!
相關文章
C++動態(tài)規(guī)劃算法實現(xiàn)矩陣鏈乘法
動態(tài)規(guī)劃算法通常用于求解具有某種最優(yōu)性質的問題。在這類問題中,可能會有許多可行解。每一個解都對應于一個值,我們希望找到具有最優(yōu)值的解2022-06-06
C語言宏定義結合全局變量的方法實現(xiàn)單片機串口透傳模式
今天小編就為大家分享一篇關于C語言宏定義結合全局變量的方法實現(xiàn)單片機串口透傳模式,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
C語言動態(tài)規(guī)劃點殺dp算法LeetCode炒股習題案例解析
這篇文章主要介紹為了C語言動態(tài)規(guī)劃點殺dp算法,本文以LeetCode炒股習題案例來為大家進行詳細解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02

