# Proper use of regionprops

9 views (last 30 days)
Matlabhelp on 4 Oct 2016
Commented: Rena Berman on 12 Oct 2020 at 17: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

Joe Yeh on 4 Oct 2016
I don't think regionprops can do that for you. However, writing your own codes to match patterns shouldn't be too complicated. To get more help from the community, please explain your problem in more details and provide codes that you've already written.
Matlabhelp on 4 Oct 2016
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 on 12 Oct 2020 at 17:12

Dimitris Iliou on 13 Oct 2016
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 Comment

Image Analyst on 13 Oct 2016
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 on 13 Oct 2016
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.