Proper use of regionprops

3 次查看(过去 30 天)
Matlabhelp
Matlabhelp 2016-10-4
评论: Rena Berman 2020-10-12
Hello
I have data which consists of a 20x20 matrix. The data has been split into their own cells as shown by the code below. Im trying to use the regionprops command to identify patterns within each cell, whereas a pattern consists of a 3 row's of 1s. e.g 1-1-1. or a block of 1's, or a line of 1's vertically. How do i go about using this function to do it
numberLocations = cell(1,5);
for k = 1:5 numberLocations{k} = (roundData==k);
end
  3 个评论
Matlabhelp
Matlabhelp 2016-10-4
编辑:Walter Roberson 2016-10-13
If you are able to help with this then instead of regionprops. Using the same 20x20 matrix i have converted each value in that matrix to either a 1,2,3,4 or 5 from this code. Where as roundData = matrix and rangeValue1 changes the value into a 1 and rangeValue2 change the value into a 2 and so on.
roundData(roundData >= restrictRange4 & roundData <restrictRange5) = rangeValue5;
roundData(roundData > restrictRange3 & roundData <restrictRange4) = rangeValue4;
roundData(roundData >= restrictRange2 & roundData < restrictRange3) = rangeValue3;
roundData(roundData >= restrictRange1 & roundData <restrictRange2) = rangeValue2;
roundData(roundData > 0 & roundData < restrictRange1) = rangeValue1;
The other way i would've tried to find a pattern such as a row ( 1,1,1 or 2,2,2 ) is
For r = 1:MAX_COLUMNS
c = 1:MAX_COLUMNS
% mapValue is a certain cell in my array, (mapValue1 is row 1, column 1 and so on for the others)
mapValue1 = roundData(r,c)
mapValue2 = roundData(r+1,c)
mapValue3 = roundData(r+2,c)
If mapValue1 == mapValue2 == mapValue3
end
end
The problem i actually had with this code was displaying it on another array that is had found, after the IF i didn;t know how to store the newly found data. How do i do so?
Rena Berman
Rena Berman 2020-10-12
(Answers Dev) Restored edit

请先登录,再进行评论。

回答(2 个)

Dimitris Iliou
Dimitris Iliou 2016-10-13
If I understand correctly, you want to use regioprops in order to identify a ‘1-1-1’ pattern in your data.
Regionprops might not be the best workflow to use in order to identify the pattern.
I would like to suggest a different workflow that might be more quick and efficient for your case. Write a script that:
  1. Uses a 3x3 mask that has the ’1-1-1’ pattern in it (i.e. mask = [0 0 0; 1 1 1; 0 0 0];)
  2. ‘Slide’ the mask over your data and calculate the cross-correlation between the mask and the data at each point.
  3. If the cross-correlation result is 1 then you have found your pattern.
To estimate the cross-correlation you could use the xcorr command. You can find more details about xcorr and how to use it in the following documentation link:
The same process can be used if your mask is vertical line of 1’s or a block. You would only need to modify the mask block and then use xcorr as before.
  1 个评论
Image Analyst
Image Analyst 2016-10-13
编辑:Image Analyst 2016-10-13
I think you meant 3, not 1. Anyway, this is a common misconception. Other patterns could also give a correlation of 3, not only the desired pattern. See this demo to prove it to yourself:
format short g;
m=zeros(10);
template = [1, 1, 1]
m(3, 4:6) = template % Place horizontal pattern in location 1
m(5:7, 2) = template' % Place vertical pattern in location 2
m(7, 6) = 3 % Place pattern in location 3
% Do correlation and normalized cross correlation.
mCorr = xcorr2(m, template)

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-10-13
You want the hit or miss transform, bwhitmiss(). See this demo, which finds horizontal and vertical patterns of [1,1,1]:
format short g;
m=zeros(10);
template = [1, 1, 1]
m(3, 4:6) = template % Place horizontal pattern in location 1
m(5:7, 2) = template' % Place vertical pattern in location 2
m(7, 6) = 3 % Place pattern in location 3
hmHorizontal = bwhitmiss(m, template, [0,0,0])
hmVertical = bwhitmiss(m, template', [0,0,0])
output = hmHorizontal | hmVertical
When the template is centered over your pattern, you will have a 1, and you'll get zero where the pattern doesn't match perfectly.

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by