matlab提取图像中的一部分并移动,在Matlab中从图像中提取对象
這是一個很好的解決問題.這是一種你可以使用的方法,但我承認它絕不是完美的,也可能不那么健壯.希望它能帶給你創意……
我所做的基本上是用中值濾波器過濾圖像(就像你做的那樣)并使用bwareaopen刪除小元素.然后我打電話給regionprops來獲得一堆屬性,其中最重要的是區域和偏心.這個想法是所有字母“a”應該有一個類似的偏心,因此一旦我們知道一個字母的怪癖,我們就可以找到其他大致相同的字母.你可以使用額外的屬性使代碼更加健壯,這些屬性可以使字母從其他字母中脫穎而出;也許比例為MajorAxisLength / MinorAxisLength.我會把那部分留給你:)
因此,在這種情況下選擇字母的最簡單方法是選擇面積最大的對象,即圖像中心的大對象.一旦我們有了它的偏心率,我們可以應用一些閾值并僅選擇那些使用具有類似偏心率的regionprops找到的對象.之前應用的中值濾波器和對bwareaopen的調用在這里很重要,因為右邊4個方框中的噪聲可能會使事情變得復雜,如果它們沒有被刪除,因為一些隨機點可能有一個類似于我們親愛的怪癖字母“a”.
話雖如此,這是評論的代碼.請注意,我將文本變量的名稱更改為textIm,因為text是Matlab函數.
clc
clear
close all
textIm = imread('http://i.stack.imgur.com/N4nCm.png');
%// find threshold and change to binary image
border = graythresh(textIm);
%// =========== NEW \ ===========
%// NOTICE the use of ~im2bw(...)
textbw = ~im2bw(textIm, border);
%// remove noise with median filter
%// =========== NEW \ ===========
textfilt = medfilt2(textbw,[7 7]);
textfilt = bwareaopen(textfilt,8);
%// =========== NEW \ ===========
%// Use an absurdely large line structuring element oriented at 25 degrees
%// to make the a's stand out
se = strel('line', 20 ,25);
textfilt = imclose(textfilt, se);
%// Get a couple properties. Note the "Eccentricity"
S = regionprops(textfilt, 'Area','Eccentricity','Centroid','BoundingBox');
All_areas = vertcat(S.Area);
%// Find the largest element (i.e. the big a). We will use it to get its
%// eccentricity and fetch other a's.
[MaxArea, MaxAreaIdx] = (max(All_areas(:)));
%// Get eccentricity of largest letter.
RefEcc = S(MaxAreaIdx).Eccentricity
這里大“a”的偏心率是0.6654.偏心率為0表示圓,偏心率為1表示直線.
%// Just concatenate everything. Easier to work with.
All_Ecc = vertcat(S.Eccentricity);
All_Centroids = vertcat(S.Centroid);
All_BB = vertcat(S.BoundingBox)
%// Find elements that have the approximate eccentricity of the large a
%// found earlier. You can be more/less stringent and add more conditions here.
PotA = find(All_Ecc > RefEcc*.8 & All_Ecc < RefEcc*1.2)
%// Display output with centroids and bounding boxes.
imshow(textIm)
hold on
scatter(All_Centroids(PotA,1),All_Centroids(PotA,2),60,'r','filled');
for k = 1:numel(PotA)
rectangle('Position',All_BB(PotA(k),:),'EdgeColor','y','LineWidth',2)
end
輸出,質心為紅點,邊界框為黃色矩形:
那很有趣!希望我能以某種方式提供幫助.您可能需要調整其他字母的代碼,或者如果圖像中有其他圓形對象,但我想這是一個開始.
總結
以上是生活随笔為你收集整理的matlab提取图像中的一部分并移动,在Matlab中从图像中提取对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 889. 已知前序后序
- 下一篇: LeetCode 105. 已知前序中序