Problem 45432. Pitting corrosion on a metal plate: Find the largest pit
You are given an N x M matrix of ones and zeros, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The ones denote areas where the plate has corroded visibly, whereas zeros are areas where the metal is still in good condition. The corroded areas (the matrix elements with ones) can be found at multiple points on the metal plate.
A collection of corroded areas that are next to each other is called a pit. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.
For this problem, a pit is defined more clearly as follows: Two corroded areas at [row R1, column C1] and [row R2, column C2] belong to the same pit if and only if max(abs(R1-R2),abs(C1-C2)) <= 1. In other words, if two corroded areas are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.
The size of a pit is equal to the number of corroded areas (or ones) that belong to it.
Write a function that accepts a matrix X denoting the image in question. Output the size of the largest pit in this image. You are ensured that the size of the matrix is constrained at 1 <= N <= 20 and 1 <= M <= 20.
As an example, consider the following 10 x 10 image (left). The corroded areas that belong to the same pit are then labeled, showing that the largest pit is Pit 1, with a size of 6 (right). In contrast, Pits 2, 3, 4 and 5 have size 2, 2, 4, and 4, respectively.
Sample test case: X = [0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 4 4 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 2 0 0 0 0 5 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 2 0 0 0 5 5 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 1 1 0 0 0 0]; 0 0 0 0 3 3 0 0 0 0 The largest pit in this image is Pit 1, with size 6.
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers20
Suggested Problems
-
Find relatively common elements in matrix rows
2050 Solvers
-
It dseon't mettar waht oedrr the lrettes in a wrod are.
1808 Solvers
-
Given two arrays, find the maximum overlap
1499 Solvers
-
Back to basics 21 - Matrix replicating
1531 Solvers
-
Find the sides of an isosceles triangle when given its area and height from its base to apex
1911 Solvers
More from this Author19
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!