src.operations.segmentation package¶
Submodules¶
src.operations.segmentation.threshold module¶
- class src.operations.segmentation.threshold.Threshold(parent)¶
Bases:
PyQt5.QtWidgets.QDialog,src.operations.operation.Operation,src.operations.segmentation.threshold_ui.ThresholdUIThe Threshold class implements a thresholding point operation.
- calc_adaptive_thresh(method, block_size)¶
Calculate adaptive threshold based on method and block size.
- Parameters
method (str) – The method to perform, can be: “Mean” or “Gaussian”
block_size (int) – The value for block size
- Returns
The new thresholded image data
- Return type
class:numpy.ndarray
- calc_theshold_otsu()¶
Calculate Otsu’s thresholding.
- calc_threshold_binary(thresh_value)¶
Calculate threshold binary point operation.
if the pixel is higher than
thresh_value, then the new pixel intensity is set to a maximum value -color_depth-1. Otherwise, the pixels are set to 0- Parameters
thresh_value (int) – The value for thresholding
- Returns
The new thresholded image data
- Return type
class:numpy.ndarray
- calc_threshold_zero(thresh_value)¶
Calculate threshold to zero point operation.
If the pixel is lower than
thresh_value, the new pixel value will be set to 0.- Parameters
thresh_value (int) – The value for thresholding
- Returns
The new thresholded image data
- Return type
class:numpy.ndarray
- update_form()¶
Update the slider behavior and slider text based on threshold type.
- update_img_preview()¶
Update image preview window.
Calculate image thresholding based on type and slider value.
Reload image preview using the base
operation.Operationmethod.
- update_slider_value()¶
Update
label_slider_valuewhenever is changed.
- src.operations.segmentation.threshold.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) → dst¶
. @brief Applies an adaptive threshold to an array. . . The function transforms a grayscale image to a binary image according to the formulae: . - THRESH_BINARY . f[dst(x,y) = fork{texttt{maxValue}}{if (src(x,y) > T(x,y))}{0}{otherwise}f] . - THRESH_BINARY_INV . f[dst(x,y) = fork{0}{if (src(x,y) > T(x,y))}{texttt{maxValue}}{otherwise}f] . where f$T(x,y)f$ is a threshold calculated individually for each pixel (see adaptiveMethod parameter). . . The function can process the image in-place. . . @param src Source 8-bit single-channel image. . @param dst Destination image of the same size and the same type as src. . @param maxValue Non-zero value assigned to the pixels for which the condition is satisfied . @param adaptiveMethod Adaptive thresholding algorithm to use, see #AdaptiveThresholdTypes. . The #BORDER_REPLICATE | #BORDER_ISOLATED is used to process boundaries. . @param thresholdType Thresholding type that must be either #THRESH_BINARY or #THRESH_BINARY_INV, . see #ThresholdTypes. . @param blockSize Size of a pixel neighborhood that is used to calculate a threshold value for the . pixel: 3, 5, 7, and so on. . @param C Constant subtracted from the mean or weighted mean (see the details below). Normally, it . is positive but may be zero or negative as well. . . @sa threshold, blur, GaussianBlur
- src.operations.segmentation.threshold.normalize(src, dst[, alpha[, beta[, norm_type[, dtype[, mask]]]]]) → dst¶
. @brief Normalizes the norm or value range of an array. . . The function cv::normalize normalizes scale and shift the input array elements so that . f[| texttt{dst} | _{L_p}= texttt{alpha}f] . (where p=Inf, 1 or 2) when normType=NORM_INF, NORM_L1, or NORM_L2, respectively; or so that . f[min _I texttt{dst} (I)= texttt{alpha} , , , max _I texttt{dst} (I)= texttt{beta}f] . . when normType=NORM_MINMAX (for dense arrays only). The optional mask specifies a sub-array to be . normalized. This means that the norm or min-n-max are calculated over the sub-array, and then this . sub-array is modified to be normalized. If you want to only use the mask to calculate the norm or . min-max but modify the whole array, you can use norm and Mat::convertTo. . . In case of sparse matrices, only the non-zero values are analyzed and transformed. Because of this, . the range transformation for sparse matrices is not allowed since it can shift the zero level. . . Possible usage with some positive example data: . @code{.cpp} . vector<double> positiveData = { 2.0, 8.0, 10.0 }; . vector<double> normalizedData_l1, normalizedData_l2, normalizedData_inf, normalizedData_minmax; . . // Norm to probability (total count) . // sum(numbers) = 20.0 . // 2.0 0.1 (2.0/20.0) . // 8.0 0.4 (8.0/20.0) . // 10.0 0.5 (10.0/20.0) . normalize(positiveData, normalizedData_l1, 1.0, 0.0, NORM_L1); . . // Norm to unit vector: ||positiveData|| = 1.0 . // 2.0 0.15 . // 8.0 0.62 . // 10.0 0.77 . normalize(positiveData, normalizedData_l2, 1.0, 0.0, NORM_L2); . . // Norm to max element . // 2.0 0.2 (2.0/10.0) . // 8.0 0.8 (8.0/10.0) . // 10.0 1.0 (10.0/10.0) . normalize(positiveData, normalizedData_inf, 1.0, 0.0, NORM_INF); . . // Norm to range [0.0;1.0] . // 2.0 0.0 (shift to left border) . // 8.0 0.75 (6.0/8.0) . // 10.0 1.0 (shift to right border) . normalize(positiveData, normalizedData_minmax, 1.0, 0.0, NORM_MINMAX); . @endcode . . @param src input array. . @param dst output array of the same size as src . . @param alpha norm value to normalize to or the lower range boundary in case of the range . normalization. . @param beta upper range boundary in case of the range normalization; it is not used for the norm . normalization. . @param norm_type normalization type (see cv::NormTypes). . @param dtype when negative, the output array has the same type as src; otherwise, it has the same . number of channels as src and the depth =CV_MAT_DEPTH(dtype). . @param mask optional operation mask. . @sa norm, Mat::convertTo, SparseMat::convertTo
- src.operations.segmentation.threshold.threshold(src, thresh, maxval, type[, dst]) → retval, dst¶
. @brief Applies a fixed-level threshold to each array element. . . The function applies fixed-level thresholding to a multiple-channel array. The function is typically . used to get a bi-level (binary) image out of a grayscale image ( #compare could be also used for . this purpose) or for removing a noise, that is, filtering out pixels with too small or too large . values. There are several types of thresholding supported by the function. They are determined by . type parameter. . . Also, the special values #THRESH_OTSU or #THRESH_TRIANGLE may be combined with one of the . above values. In these cases, the function determines the optimal threshold value using the Otsu’s . or Triangle algorithm and uses it instead of the specified thresh. . . @note Currently, the Otsu’s and Triangle methods are implemented only for 8-bit single-channel images. . . @param src input array (multiple-channel, 8-bit or 32-bit floating point). . @param dst output array of the same size and type and the same number of channels as src. . @param thresh threshold value. . @param maxval maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding . types. . @param type thresholding type (see #ThresholdTypes). . @return the computed threshold value if Otsu’s or Triangle methods used. . . @sa adaptiveThreshold, findContours, compare, min, max
src.operations.segmentation.threshold_ui module¶
- class src.operations.segmentation.threshold_ui.ThresholdUI¶
Bases:
src.operations.operation_ui.OperationUI,src.operations.form_ui.FormUIBuild UI for
threshold.Threshold.- init_ui(threshold)¶
Create user interface for
threshold.Threshold.The method creates the widget objects in the proper containers and assigns the object names to them.
- Parameters
threshold (
threshold.Threshold) – The dialog threshold window
src.operations.segmentation.watershed module¶
- class src.operations.segmentation.watershed.Watershed(parent)¶
Bases:
PyQt5.QtWidgets.QDialog,src.operations.operation.Operation,src.operations.segmentation.watershed_ui.WatershedUIThe Watershed class implements a watershed segmentation.
- static calc_watershed(img_color)¶
Calculate watershed segmentation with different previews.
- There are 4 previews of segmented objects:
Color image.
Grayscale image.
Pseudocolor.
Blended.
- Parameters
img_color (class:numpy.ndarray) – The image data to perform watershed
- Returns
The pair: objects count and dictionary with available previews
- Return type
tuple[int, dict]
- update_img_preview()¶
Update image preview window.
Select image preview based on chosen preview.
Reload image preview using the base
operation.Operationmethod.
- src.operations.segmentation.watershed.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) → dst¶
. @brief Calculates the weighted sum of two arrays. . . The function addWeighted calculates the weighted sum of two arrays as follows: . f[texttt{dst} (I)= texttt{saturate} ( texttt{src1} (I)* texttt{alpha} + texttt{src2} (I)* texttt{beta} + texttt{gamma} )f] . where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each . channel is processed independently. . The function can be replaced with a matrix expression: . @code{.cpp} . dst = src1*alpha + src2*beta + gamma; . @endcode . @note Saturation is not applied when the output array has the depth CV_32S. You may even get . result of an incorrect sign in the case of overflow. . @param src1 first input array. . @param alpha weight of the first array elements. . @param src2 second input array of the same size and channel number as src1. . @param beta weight of the second array elements. . @param gamma scalar added to each sum. . @param dst output array that has the same size and number of channels as the input arrays. . @param dtype optional depth of the output array; when both input arrays have the same depth, dtype . can be set to -1, which will be equivalent to src1.depth(). . @sa add, subtract, scaleAdd, Mat::convertTo
- src.operations.segmentation.watershed.applyColorMap(src, colormap[, dst]) → dst¶
. @brief Applies a GNU Octave/MATLAB equivalent colormap on a given image. . . @param src The source image, grayscale or colored of type CV_8UC1 or CV_8UC3. . @param dst The result is the colormapped source image. Note: Mat::create is called on dst. . @param colormap The colormap to apply, see #ColormapTypes
applyColorMap(src, userColor[, dst]) -> dst . @brief Applies a user colormap on a given image. . . @param src The source image, grayscale or colored of type CV_8UC1 or CV_8UC3. . @param dst The result is the colormapped source image. Note: Mat::create is called on dst. . @param userColor The colormap to apply of type CV_8UC1 or CV_8UC3 and size 256
- src.operations.segmentation.watershed.connectedComponents(image[, labels[, connectivity[, ltype]]]) → retval, labels¶
. @overload . . @param image the 8-bit single-channel image to be labeled . @param labels destination labeled image . @param connectivity 8 or 4 for 8-way or 4-way connectivity respectively . @param ltype output image label type. Currently CV_32S and CV_16U are supported.
- src.operations.segmentation.watershed.cvtColor(src, code[, dst[, dstCn]]) → dst¶
. @brief Converts an image from one color space to another. . . The function converts an input image from one color space to another. In case of a transformation . to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note . that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the . bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue . component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and . sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on. . . The conventional ranges for R, G, and B channel values are: . - 0 to 255 for CV_8U images . - 0 to 65535 for CV_16U images . - 0 to 1 for CV_32F images . . In case of linear transformations, the range does not matter. But in case of a non-linear . transformation, an input RGB image should be normalized to the proper value range to get the correct . results, for example, for RGB f$rightarrowf$ L*u*v* transformation. For example, if you have a . 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will . have the 0..255 value range instead of 0..1 assumed by the function. So, before calling #cvtColor , . you need first to scale the image down: . @code . img *= 1./255; . cvtColor(img, img, COLOR_BGR2Luv); . @endcode . If you use #cvtColor with 8-bit images, the conversion will have some information lost. For many . applications, this will not be noticeable but it is recommended to use 32-bit images in applications . that need the full range of colors or that convert an image before an operation and then convert . back. . . If conversion adds the alpha channel, its value will set to the maximum of corresponding channel . range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F. . . @param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC… ), or single-precision . floating-point. . @param dst output image of the same size and depth as src. . @param code color space conversion code (see #ColorConversionCodes). . @param dstCn number of channels in the destination image; if the parameter is 0, the number of the . channels is derived automatically from src and code. . . @see @ref imgproc_color_conversions
- src.operations.segmentation.watershed.distanceTransform(src, distanceType, maskSize[, dst[, dstType]]) → dst¶
. @overload . @param src 8-bit, single-channel (binary) source image. . @param dst Output image with calculated distances. It is a 8-bit or 32-bit floating-point, . single-channel image of the same size as src . . @param distanceType Type of distance, see #DistanceTypes . @param maskSize Size of the distance transform mask, see #DistanceTransformMasks. In case of the . #DIST_L1 or #DIST_C distance type, the parameter is forced to 3 because a f$3times 3f$ mask gives . the same result as f$5times 5f$ or any larger aperture. . @param dstType Type of output image. It can be CV_8U or CV_32F. Type CV_8U can be used only for . the first variant of the function and distanceType == #DIST_L1.
- src.operations.segmentation.watershed.morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst¶
. @brief Performs advanced morphological transformations. . . The function cv::morphologyEx can perform advanced morphological transformations using an erosion and dilation as . basic operations. . . Any of the operations can be done in-place. In case of multi-channel images, each channel is . processed independently. . . @param src Source image. The number of channels can be arbitrary. The depth should be one of . CV_8U, CV_16U, CV_16S, CV_32F or CV_64F. . @param dst Destination image of the same size and type as source image. . @param op Type of a morphological operation, see #MorphTypes . @param kernel Structuring element. It can be created using #getStructuringElement. . @param anchor Anchor position with the kernel. Negative values mean that the anchor is at the . kernel center. . @param iterations Number of times erosion and dilation are applied. . @param borderType Pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported. . @param borderValue Border value in case of a constant border. The default value has a special . meaning. . @sa dilate, erode, getStructuringElement . @note The number of iterations is the number of times erosion or dilatation operation will be applied. . For instance, an opening operation (#MORPH_OPEN) with two iterations is equivalent to apply . successively: erode -> erode -> dilate -> dilate (and not erode -> dilate -> erode -> dilate).
- src.operations.segmentation.watershed.subtract(src1, src2[, dst[, mask[, dtype]]]) → dst¶
. @brief Calculates the per-element difference between two arrays or array and a scalar. . . The function subtract calculates: . - Difference between two arrays, when both input arrays have the same size and the same number of . channels: . f[texttt{dst}(I) = texttt{saturate} ( texttt{src1}(I) - texttt{src2}(I)) quad texttt{if mask}(I) ne0f] . - Difference between an array and a scalar, when src2 is constructed from Scalar or has the same . number of elements as src1.channels(): . f[texttt{dst}(I) = texttt{saturate} ( texttt{src1}(I) - texttt{src2} ) quad texttt{if mask}(I) ne0f] . - Difference between a scalar and an array, when src1 is constructed from Scalar or has the same . number of elements as src2.channels(): . f[texttt{dst}(I) = texttt{saturate} ( texttt{src1} - texttt{src2}(I) ) quad texttt{if mask}(I) ne0f] . - The reverse difference between a scalar and an array in the case of SubRS: . f[texttt{dst}(I) = texttt{saturate} ( texttt{src2} - texttt{src1}(I) ) quad texttt{if mask}(I) ne0f] . where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each . channel is processed independently. . . The first function in the list above can be replaced with matrix expressions: . @code{.cpp} . dst = src1 - src2; . dst -= src1; // equivalent to subtract(dst, src1, dst); . @endcode . The input arrays and the output array can all have the same or different depths. For example, you . can subtract to 8-bit unsigned arrays and store the difference in a 16-bit signed array. Depth of . the output array is determined by dtype parameter. In the second and third cases above, as well as . in the first case, when src1.depth() == src2.depth(), dtype can be set to the default -1. In this . case the output array will have the same depth as the input array, be it src1, src2 or both. . @note Saturation is not applied when the output array has the depth CV_32S. You may even get . result of an incorrect sign in the case of overflow. . @param src1 first input array or a scalar. . @param src2 second input array or a scalar. . @param dst output array of the same size and the same number of channels as the input array. . @param mask optional operation mask; this is an 8-bit single channel array that specifies elements . of the output array to be changed. . @param dtype optional depth of the output array . @sa add, addWeighted, scaleAdd, Mat::convertTo
- src.operations.segmentation.watershed.threshold(src, thresh, maxval, type[, dst]) → retval, dst¶
. @brief Applies a fixed-level threshold to each array element. . . The function applies fixed-level thresholding to a multiple-channel array. The function is typically . used to get a bi-level (binary) image out of a grayscale image ( #compare could be also used for . this purpose) or for removing a noise, that is, filtering out pixels with too small or too large . values. There are several types of thresholding supported by the function. They are determined by . type parameter. . . Also, the special values #THRESH_OTSU or #THRESH_TRIANGLE may be combined with one of the . above values. In these cases, the function determines the optimal threshold value using the Otsu’s . or Triangle algorithm and uses it instead of the specified thresh. . . @note Currently, the Otsu’s and Triangle methods are implemented only for 8-bit single-channel images. . . @param src input array (multiple-channel, 8-bit or 32-bit floating point). . @param dst output array of the same size and type and the same number of channels as src. . @param thresh threshold value. . @param maxval maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding . types. . @param type thresholding type (see #ThresholdTypes). . @return the computed threshold value if Otsu’s or Triangle methods used. . . @sa adaptiveThreshold, findContours, compare, min, max
- src.operations.segmentation.watershed.watershed(image, markers) → markers¶
. @brief Performs a marker-based image segmentation using the watershed algorithm. . . The function implements one of the variants of watershed, non-parametric marker-based segmentation . algorithm, described in @cite Meyer92 . . . Before passing the image to the function, you have to roughly outline the desired regions in the . image markers with positive (>0) indices. So, every region is represented as one or more connected . components with the pixel values 1, 2, 3, and so on. Such markers can be retrieved from a binary . mask using #findContours and #drawContours (see the watershed.cpp demo). The markers are “seeds” of . the future image regions. All the other pixels in markers , whose relation to the outlined regions . is not known and should be defined by the algorithm, should be set to 0’s. In the function output, . each pixel in markers is set to a value of the “seed” components or to -1 at boundaries between the . regions. . . @note Any two neighbor connected components are not necessarily separated by a watershed boundary . (-1’s pixels); for example, they can touch each other in the initial marker image passed to the . function. . . @param image Input 8-bit 3-channel image. . @param markers Input/output 32-bit single-channel image (map) of markers. It should have the same . size as image . . . @sa findContours . . @ingroup imgproc_misc
src.operations.segmentation.watershed_ui module¶
- class src.operations.segmentation.watershed_ui.WatershedUI¶
Bases:
src.operations.operation_ui.OperationUI,src.operations.form_ui.FormUIBuild UI for
watershed.Watershed.- init_ui(watershed)¶
Create user interface for
watershed.Watershed.The method creates the widget objects in the proper containers and assigns the object names to them.
- Parameters
watershed (
watershed.Watershed) – The dialog watershed window