Class BinaryImageOps
Contains a standard set of operations performed on binary images. A pixel has a value of false if it is equal to zero or true equal to one.
NOTE: If an element's value is not zero or one then each function's behavior is undefined.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
clusterToBinary
(List<List<Point2D_I32>> clusters, GrayU8 binary) Sets each pixel in the list of clusters to one in the binary image.contour
(GrayU8 input, ConnectRule rule, @Nullable GrayS32 output) Given a binary image, connect together pixels to form blobs/clusters using the specified connectivity rule.contourExternal
(GrayU8 input, ConnectRule rule) Finds the external contours only in the imagestatic GrayU8
Dilates an image according to a 4-neighborhood.static GrayU8
Dilates an image according to a 8-neighborhood.static GrayU8
Binary operation which is designed to remove all pixels but ones which are on the edge of an object.static GrayU8
Binary operation which is designed to remove all pixels but ones which are on the edge of an object.static GrayU8
Erodes an image according to a 4-neighborhood.static GrayU8
Erodes an image according to a 8-neighborhood.static GrayU8
Inverts each pixel from true to false and vis-versa.static GrayU8
labelToBinary
(GrayS32 labelImage, GrayU8 binaryImage) Converts a labeled image into a binary image by setting any non-zero value to one.static GrayU8
labelToBinary
(GrayS32 labelImage, GrayU8 binaryImage, boolean[] selectedBlobs) Only converts the specified blobs over into the binary imagestatic GrayU8
labelToBinary
(GrayS32 labelImage, GrayU8 binaryImage, int numLabels, int... selected) Only converts the specified blobs over into the binary image.static List<List<Point2D_I32>>
labelToClusters
(GrayS32 labelImage, int numLabels, DogArray<Point2D_I32> queue) Scans through the labeled image and adds the coordinate of each pixel that has been labeled to a list specific to its label.static GrayU8
For each pixel it applies the logical 'and' operator between two images.static GrayU8
For each pixel it applies the logical 'or' operator between two images.static GrayU8
For each pixel it applies the logical 'xor' operator between two images.static void
Used to change the labels in a labeled binary image.static GrayU8
removePointNoise
(GrayU8 input, @Nullable GrayU8 output) Binary operation which is designed to remove small bits of spurious noise.static int[]
selectRandomColors
(int numBlobs, Random rand) Several blob rending functions take in an array of colors so that the random blobs can be drawn with the same color each time.static GrayU8
Applies a morphological thinning operation to the image.
-
Constructor Details
-
BinaryImageOps
public BinaryImageOps()
-
-
Method Details
-
logicAnd
For each pixel it applies the logical 'and' operator between two images.- Parameters:
inputA
- First input image. Not modified.inputB
- Second input image. Not modified.output
- Output image. Can be same as either input. If null a new instance will be declared, Modified.- Returns:
- Output of logical operation.
-
logicOr
For each pixel it applies the logical 'or' operator between two images.- Parameters:
inputA
- First input image. Not modified.inputB
- Second input image. Not modified.output
- Output image. Can be same as either input. If null a new instance will be declared, Modified.- Returns:
- Output of logical operation.
-
logicXor
For each pixel it applies the logical 'xor' operator between two images.- Parameters:
inputA
- First input image. Not modified.inputB
- Second input image. Not modified.output
- Output image. Can be same as either input. If null a new instance will be declared, Modified.- Returns:
- Output of logical operation.
-
invert
Inverts each pixel from true to false and vis-versa.- Parameters:
input
- Input image. Not modified.output
- Output image. Can be same as input. If null a new instance will be declared, Modified.- Returns:
- Output of logical operation.
-
erode4
Erodes an image according to a 4-neighborhood. Unless a pixel is connected to all its neighbors its value is set to zero.
- Parameters:
input
- Input image. Not modified.numTimes
- How many times the operation will be applied to the image.output
- If not null, the output image. If null a new image is declared and returned. Modified.- Returns:
- Output image.
-
dilate4
Dilates an image according to a 4-neighborhood. If a pixel is connected to any other pixel then its output value will be one.
- Parameters:
input
- Input image. Not modified.numTimes
- How many times the operation will be applied to the image.output
- If not null, the output image. If null a new image is declared and returned. Modified.- Returns:
- Output image.
-
edge4
Binary operation which is designed to remove all pixels but ones which are on the edge of an object. The edge is defined as lying on the object and not being surrounded by a pixel along a 4-neighborhood.
NOTE: There are many ways to define an edge, this is just one of them.
- Parameters:
input
- Input image. Not modified.output
- If not null, the output image. If null a new image is declared and returned. Modified.outsideZero
- if true then pixels outside the image are treated as zero, otherwise one- Returns:
- Output image.
-
erode8
Erodes an image according to a 8-neighborhood. Unless a pixel is connected to all its neighbors its value is set to zero.
- Parameters:
input
- Input image. Not modified.numTimes
- How many times the operation will be applied to the image.output
- If not null, the output image. If null a new image is declared and returned. Modified.- Returns:
- Output image.
-
dilate8
Dilates an image according to a 8-neighborhood. If a pixel is connected to any other pixel then its output value will be one.
- Parameters:
input
- Input image. Not modified.numTimes
- How many times the operation will be applied to the image.output
- If not null, the output image. If null a new image is declared and returned. Modified.- Returns:
- Output image.
-
edge8
Binary operation which is designed to remove all pixels but ones which are on the edge of an object. The edge is defined as lying on the object and not being surrounded by 8 pixels.
NOTE: There are many ways to define an edge, this is just one of them.
- Parameters:
input
- Input image. Not modified.output
- If not null, the output image. If null a new image is declared and returned. Modified.outsideZero
- if true then pixels outside the image are treated as zero, otherwise one- Returns:
- Output image.
-
removePointNoise
Binary operation which is designed to remove small bits of spurious noise. An 8-neighborhood is used. If a pixel is connected to less than 2 neighbors then its value zero. If connected to more than 6 then its value is one. Otherwise it retains its original value.- Parameters:
input
- Input image. Not modified.output
- If not null, the output image. If null a new image is declared and returned. Modified.- Returns:
- Output image.
-
thin
Applies a morphological thinning operation to the image. Also known as skeletonization.- Parameters:
input
- Input image. Not modified.maxIterations
- Maximum number of cycles it will thin for. -1 for the maximum requiredoutput
- If not null, the output image. If null a new image is declared and returned. Modified.- Returns:
- Output image.
- See Also:
-
contour
public static List<Contour> contour(GrayU8 input, ConnectRule rule, @Nullable @Nullable GrayS32 output) Given a binary image, connect together pixels to form blobs/clusters using the specified connectivity rule. The found blobs will be labeled in an output image and also described as a set of contours. Pixels in the contours are consecutive order in a clockwise or counter-clockwise direction, depending on the implementation. The labeled image will assign background pixels a label of 0 and each blob will be assigned a unique ID starting from 1.
The returned contours are traces of the object. The trace of an object can be found by marking a point with a pen and then marking every point on the contour without removing the pen. It is possible to have the same point multiple times in the contour.
- Parameters:
input
- Input binary image. Not modified.rule
- Connectivity rule. Can be 4 or 8. 8 is more commonly used.output
- (Optional) Output labeled image. If null, an image will be declared internally. Modified.- Returns:
- List of found contours for each blob.
- See Also:
-
contourExternal
Finds the external contours only in the image- Parameters:
input
- Input binary image. Not modified.rule
- Connectivity rule. Can be 4 or 8. 8 is more commonly used.- Returns:
- List of found contours for each blob.
-
convertContours
-
relabel
Used to change the labels in a labeled binary image.- Parameters:
input
- Labeled binary image.labels
- Look up table where the indexes are the current label and the value are its new value.
-
labelToBinary
Converts a labeled image into a binary image by setting any non-zero value to one.- Parameters:
labelImage
- Input image. Not modified.binaryImage
- Output image. Modified.- Returns:
- The binary image.
-
labelToBinary
Only converts the specified blobs over into the binary image- Parameters:
labelImage
- Input image. Not modified.binaryImage
- Output image. If null a new one will be declared. Modified.selectedBlobs
- Each index corresponds to a blob and specifies if it is included or not. Expected size is the number of found clusters + 1.- Returns:
- The binary image.
-
labelToBinary
public static GrayU8 labelToBinary(GrayS32 labelImage, GrayU8 binaryImage, int numLabels, int... selected) Only converts the specified blobs over into the binary image. Easier to use version oflabelToBinary(GrayS32, GrayU8, boolean[])
.- Parameters:
labelImage
- Input image. Not modified.binaryImage
- Output image. If null a new one will be declared. Modified.numLabels
- Number of labels in the image. This is the number of found clusters + 1.selected
- The index of labels which will be marked as 1 in the output binary image.- Returns:
- The binary image.
-
labelToClusters
public static List<List<Point2D_I32>> labelToClusters(GrayS32 labelImage, int numLabels, DogArray<Point2D_I32> queue) Scans through the labeled image and adds the coordinate of each pixel that has been labeled to a list specific to its label.- Parameters:
labelImage
- The labeled image.numLabels
- Number of labeled objects inside the image.queue
- (Optional) Storage for pixel coordinates. Improves runtime performance. Can be null.- Returns:
- List of pixels in each cluster.
-
clusterToBinary
Sets each pixel in the list of clusters to one in the binary image.- Parameters:
clusters
- List of all the clusters.binary
- Output
-
selectRandomColors
Several blob rending functions take in an array of colors so that the random blobs can be drawn with the same color each time. This function selects a random color for each blob and returns it in an array.- Parameters:
numBlobs
- Number of blobs found.rand
- Random number generator- Returns:
- array of RGB colors for each blob + the background blob
-