vz
namespace for C Code Run's "Vision" tools and libraries.
More...
Classes | |
class | Imagination |
Imagination objects are used to perform simple and common image manipulations, as well as act as an image cache to store variations on an image. More... | |
Typedefs | |
typedef std::vector< cv::Mat > | VMats |
Vector of OpenCV cv::Mat . More... | |
typedef std::vector< std::string > | VStr |
Vector of text strings. More... | |
typedef std::vector< cv::Point > | Contour |
A single contour is a vector of many points. More... | |
typedef std::vector< Contour > | VContours |
Many contours are then combined to create a vector of contours. More... | |
typedef std::vector< cv::RotatedRect > | VRotatedRects |
Vector of rotated rectangles. More... | |
typedef std::vector< cv::Vec4i > | VV4i |
Vector of Vec4i. More... | |
typedef std::vector< int > | VInt |
Vector of int. More... | |
typedef std::set< double > | SDouble |
Set of doubles. More... | |
typedef std::vector< double > | VDouble |
Vector of doubles. More... | |
typedef std::vector< VDouble > | VVDouble |
Vector of vz::VDouble. More... | |
typedef std::vector< size_t > | VSizet |
Vector of size_t. More... | |
typedef std::map< std::string, std::string > | MStr |
Map of strings where both the key and the value are strings. More... | |
Functions | |
VVDouble | intervals (VDouble &data, const double threshold_multiplier=2.0) |
Automatically determine some intervals in a vector of double values. More... | |
VDouble | popular_values_from_intervals (const VVDouble &vv, const double threshold=0.5) |
Find one or more possible values based on the given intervals. More... | |
double | best_value_from_intervals (const VVDouble &vv) |
Returns the average of the largest grouping within the intervals. More... | |
std::string | to_string (const VVDouble &vv) |
Convert the given interval to a text string. More... | |
size_t | k_means (const VDouble &data, size_t k, VSizet &assignments, VDouble ¢roids) |
Perform k-means clustering on a vector of doubles . More... | |
size_t | k_means_vector (const VDouble &data, size_t k, vz::VVDouble &results, VDouble ¢roids) |
Similar to vz::k_means(), but the results that would have been returned in assignments are de-referenced and formatted as a vector of clusters. More... | |
bool | compare_exact (const cv::Mat &lhs, const cv::Mat &rhs) |
Compare two images. This will only return true if the images are exactly the same. More... | |
cv::Mat | to_greyscale (const cv::Mat &src) |
Convert the source cv::Mat image to single-channel grey. More... | |
cv::Mat | apply_gaussian_blur (const cv::Mat &src, const int kernel_size=3) |
Apply a Gaussian blur to the image. More... | |
cv::Mat | apply_blur_and_convert_to_greyscale (const cv::Mat &src, const int kernel_size=3) |
Apply a Gaussian blur to the image and then convert the result to single-channel greyscale. More... | |
cv::Mat | apply_simple_colour_balance (const cv::Mat &src) |
Apply a simple colour balance to the image. More... | |
cv::Mat | erode (const cv::Mat &src, const int iterations=1) |
Erode the image. More... | |
cv::Mat | dilate (const cv::Mat &src, const int iterations=1) |
Dilate the image. More... | |
cv::Mat | invert (const cv::Mat &src) |
Invert the image. More... | |
cv::Mat | apply_canny_edge_detection (const cv::Mat &src, const double canny_threshold) |
Apply canny edge detection. More... | |
VContours | find_contours (const cv::Mat &src) |
Find the contours in the given source image. More... | |
cv::Mat | add_contours_to_image (const cv::Mat &src, const VContours &contours, const cv::Scalar &colour=cv::Scalar(0, 255, 255)) |
Add the given contours (if any) to the image. More... | |
cv::Mat | add_contours_and_limits_to_image (const cv::Mat &src, const VContours &contours) |
Very similar to add_contours_to_image(), but colours the contours based on groupings/limits of the contour areas. More... | |
cv::Mat | plot_histogram (const cv::Mat &src, const int width=640, const int height=480, const cv::Scalar background_colour=cv::Scalar(0, 0, 0)) |
Plot the image's histogram. More... | |
cv::Mat | find_hough_lines (const cv::Mat &src, VV4i &lines) |
Apply the Hough Lines transform. More... | |
cv::Mat | rotate (const cv::Mat &src, double degrees) |
Rotate an image an arbitrary number of degrees around the exact center of the image. More... | |
cv::Mat | resize (const cv::Mat &src, int width, int height=0) |
Resize the given image, while maintaining the original aspect ratio. More... | |
cv::Mat | resize (const cv::Mat &src, const double scale) |
Resize the given image, keeping the same aspect ratio. More... | |
cv::Mat | letterbox (const cv::Mat &src, int width, int height, const cv::Scalar &colour=cv::Scalar(0, 0, 0)) |
Resize the image to fit within the given dimensions and apply a border to pad the size until the desired size is achieved. More... | |
cv::Mat | apply_DoG (const cv::Mat &src, const double k=1.6, const double sigma=0.5, const double gamma=1.0) |
Perform 2 Gaussian blurs of different size, and subtract the 2nd from the 1st. More... | |
cv::Mat | apply_DoG_threshold (const cv::Mat &src, const double k=200.0, const double sigma=0.5, const double gamma=1.0) |
Obtain the DoG (Difference of Gaussian) by calling apply_DoG(), then find a threshold image from the result. More... | |
VRotatedRects | convert_contours_to_rotated_rects (const VContours &contours) |
Convert all of the contours to rotated rectangles. More... | |
vz
namespace for C Code Run's "Vision" tools and libraries.
typedef std::vector<cv::Mat> vz::VMats |
Vector of OpenCV cv::Mat
.
typedef std::vector<std::string> vz::VStr |
Vector of text strings.
typedef std::vector<cv::Point> vz::Contour |
A single contour is a vector of many points.
typedef std::vector<Contour> vz::VContours |
Many contours are then combined to create a vector of contours.
typedef std::vector<cv::RotatedRect> vz::VRotatedRects |
Vector of rotated rectangles.
typedef std::vector<cv::Vec4i> vz::VV4i |
Vector of Vec4i.
typedef std::vector<int> vz::VInt |
Vector of int.
typedef std::set<double> vz::SDouble |
Set of doubles.
typedef std::vector<double> vz::VDouble |
Vector of doubles.
typedef std::vector<VDouble> vz::VVDouble |
Vector of vz::VDouble.
typedef std::vector<size_t> vz::VSizet |
Vector of size_t.
typedef std::map<std::string, std::string> vz::MStr |
Map of strings where both the key and the value are strings.
vz::VVDouble vz::intervals | ( | VDouble & | data, |
const double | threshold_multiplier = 2.0 |
||
) |
Automatically determine some intervals in a vector of double
values.
Based on some example code discovered at https://stackoverflow.com/questions/17479944/partitioning-an-float-array-into-similar-segments-clustering while looking into segmentation and natural break optimizations.
[in,out] | data | The input data vector is sorted by intervals (). |
[in] | threshold_multiplier | Used to calculate the threshold when determining where to start a new interval. The larger the threshold, the smaller the number of intervals. A threshold multiplier near 0.0 will cause every value to be in a new interval. |
For example, given this data:
Calling vz::intervals
() might return something similar to this:
std::invalid_argument | if threshold_multiplier is <= 0 |
Referenced by add_contours_and_limits_to_image(), and vz::Imagination::create_hough_lines_and_angles().
vz::VDouble vz::popular_values_from_intervals | ( | const VVDouble & | vv, |
const double | threshold = 0.5 |
||
) |
Find one or more possible values based on the given intervals.
If multiple values are returned, they will be sorted from most-likely to least-likely as determined by the size of the interval groupings.
[in] | vv | The intervals returned by vz::intervals(). |
[in] | threshold | Helps determine how many groupings to consider. If set to 1.0, then only the largest grouping will be considered, while a value of 0.5 will take into consideration any grouping that is at least 1/2 the size of the largest interval. |
For example, given this data:
Calling vz::popular_values_from_intervals
() might return something similar to this:
double vz::best_value_from_intervals | ( | const VVDouble & | vv | ) |
Returns the average of the largest grouping within the intervals.
For example, given this data:
Calling vz::best_value_from_intervals
() might return something similar to this:
Referenced by vz::Imagination::create_hough_lines_and_angles().
std::string vz::to_string | ( | const VVDouble & | vv | ) |
Convert the given interval to a text string.
The resulting string is meant for debug purposes, not serialization.
For example:
Perform k-means clustering on a vector of doubles
.
Unfortunately, k-means requires the caller to know the exact number of clusters.
[in] | data | Vector of doubles . |
[in] | k | The number of clusters. The result may have fewer than this if there isn't enough input data. |
[out] | assignments | A vector of indexes indicating the cluster into which a data point was assigned. Indexes will range from zero to k - 1. |
[out] | centroids | The final centroids as determined by k-means – the location of each cluster. |
For example, given this data:
Calling vz::k_means
() with k=2
might return something similar to the following after 2 iterations:
std::invalid_argument | if k (number of clusters) is invalid |
Referenced by k_means_vector().
size_t vz::k_means_vector | ( | const VDouble & | data, |
size_t | k, | ||
vz::VVDouble & | results, | ||
VDouble & | centroids | ||
) |
Similar to vz::k_means(), but the results that would have been returned in assignments
are de-referenced and formatted as a vector of clusters.
For example, given this data:
Calling k_means_split
() with k=2
might return something similar to the following after 2 iterations:
References k_means().
bool vz::compare_exact | ( | const cv::Mat & | lhs, |
const cv::Mat & | rhs | ||
) |
Compare two images. This will only return true
if the images are exactly the same.
Referenced by vz::Imagination::operator==().
cv::Mat vz::to_greyscale | ( | const cv::Mat & | src | ) |
Convert the source cv::Mat
image to single-channel grey.
If the source is already greyscale, then returns a clone of the source image.
Examples:
original image | result image |
---|---|
std::invalid_argument | if the image is empty |
std::invalid_argument | if the image has an unsupported number of channels |
Referenced by apply_blur_and_convert_to_greyscale(), apply_DoG(), vz::Imagination::create_threshold(), and vz::Imagination::get().
cv::Mat vz::apply_gaussian_blur | ( | const cv::Mat & | src, |
const int | kernel_size = 3 |
||
) |
Apply a Gaussian blur to the image.
[in] | src | The input image. This can be a colour, greyscale, or binary image. |
[in] | kernel_size | The kernel size must be positive, and an odd number. The larger the kernel size, the larger the blur area. A kernel of size 1 does not blur. |
Examples:
original image | kernel_size=3 | kernel_size=9 | kernel_size=15 |
---|---|---|---|
std::invalid_argument | if the image is empty |
std::invalid_argument | if the kernel size is < 1 or an even number |
Referenced by apply_blur_and_convert_to_greyscale(), vz::Imagination::create_threshold(), and vz::Imagination::get().
cv::Mat vz::apply_blur_and_convert_to_greyscale | ( | const cv::Mat & | src, |
const int | kernel_size = 3 |
||
) |
Apply a Gaussian blur to the image and then convert the result to single-channel greyscale.
This is equivalent to calling both apply_gaussian_blur() and to_greyscale().
[in] | src | The input image. This can be a colour, greyscale, or binary image. |
[in] | kernel_size | The kernel size must be positive, and an odd number. The larger the kernel size, the larger the blur area. A kernel of size 1 does not blur. |
Examples:
original image | kernel_size=3 | kernel_size=9 | kernel_size=15 |
---|---|---|---|
References apply_gaussian_blur(), and to_greyscale().
cv::Mat vz::apply_simple_colour_balance | ( | const cv::Mat & | src | ) |
Apply a simple colour balance to the image.
This is the color balancing technique used in Adobe Photoshop's "auto levels" command. The idea is that in a well balanced photo, the brightest color should be white and the darkest black. Thus, we can remove the color cast from an image by scaling the histograms of each of the R, G, and B channels so that they span the complete 0-255 scale.
Examples:
original image | result image |
---|---|
std::invalid_argument | if the image is empty |
Referenced by vz::Imagination::get().
cv::Mat vz::erode | ( | const cv::Mat & | src, |
const int | iterations = 1 |
||
) |
Erode the image.
Dark area in the image are "grown" to erode lighter area. Reverses the changes done by vz::dilate().
[in] | src | The input image. |
[in] | iterations | The number of erosion iterations to run. Must be a positive number. |
Examples:
original image | iterations=1 | iterations=2 | iterations=3 |
---|---|---|---|
The results are typically better when the source is a binary image:
original image | iterations=1 | iterations=2 | iterations=3 |
---|---|---|---|
std::invalid_argument | if the image is empty |
Referenced by vz::Imagination::get().
cv::Mat vz::dilate | ( | const cv::Mat & | src, |
const int | iterations = 1 |
||
) |
Dilate the image.
This works best on black-and-white threshold images. Does the opposite as vz::erode(), meaning that white areas of the image are "grown".
[in] | src | The input image. |
[in] | iterations | The number of dilation iterations to run. Must be a positive number. |
Examples:
original image | erode iterations=3 | erode + dilate iterations=3 |
---|---|---|
std::invalid_argument | if the image is empty |
Referenced by vz::Imagination::get().
cv::Mat vz::invert | ( | const cv::Mat & | src | ) |
Invert the image.
Examples:
original image | result image |
---|---|
std::invalid_argument | if the image is empty |
Referenced by vz::Imagination::create_threshold(), and vz::Imagination::get().
cv::Mat vz::apply_canny_edge_detection | ( | const cv::Mat & | src, |
const double | canny_threshold | ||
) |
Apply canny edge detection.
The result of canny edge detection is a black-and-white image, and the output can differ significantly depending on the input source.
[in] | src | The input image. |
[in] | canny_threshold | A threshold to apply when converting the input image to black-and-white. This must be a positive number. |
Examples:
input image description | input image | output image |
---|---|---|
original colour image | ||
gaussian blur image (3x3 kernel size) | ||
gaussian blur image (15x15 kernel size) | ||
threshold image |
std::invalid_argument | if the image is empty |
Referenced by vz::Imagination::get().
vz::VContours vz::find_contours | ( | const cv::Mat & | src | ) |
Find the contours in the given source image.
Unlike most other functions in the vz
namespace, the output of this function is not a visual image. Instead, the output is a vector of contours, each of which is a vector of points.
To get a visual representation of the contours, call vz::add_contours_to_image().
[in] | src | The input image should be a black-and-white image, such as those produced by apply_canny_edge_detection(). |
find_contours
() detects, see the output of add_contours_to_image().std::invalid_argument | if the image is empty |
Referenced by vz::Imagination::find_contours().
cv::Mat vz::add_contours_to_image | ( | const cv::Mat & | src, |
const VContours & | contours, | ||
const cv::Scalar & | colour = cv::Scalar(0, 255, 255) |
||
) |
Add the given contours (if any) to the image.
[in] | src | Input image which is cloned prior to drawing the contours. |
[in] | contours | The vector of contours to draw. This is obtained via vz::find_contours(). |
[in] | colour | The RGB colour to use when drawing the contours. The default colour is yellow. |
Example:
input image | output image |
---|---|
std::invalid_argument | if the image is empty |
Referenced by vz::Imagination::get().
cv::Mat vz::add_contours_and_limits_to_image | ( | const cv::Mat & | src, |
const VContours & | contours | ||
) |
Very similar to add_contours_to_image(), but colours the contours based on groupings/limits of the contour areas.
std::invalid_argument | if the image is empty |
References intervals().
Referenced by vz::Imagination::get().
cv::Mat vz::plot_histogram | ( | const cv::Mat & | src, |
const int | width = 640 , |
||
const int | height = 480 , |
||
const cv::Scalar | background_colour = cv::Scalar(0, 0, 0) |
||
) |
Plot the image's histogram.
[in] | src | Input must be either 3-channel (BGR) or 1-channel (greyscale) image. |
[in] | width | Width of output histogram plot. |
[in] | height | Height of output histogram plot. |
[in] | background_colour | Defaults to black, but also common would be cv::Scalar(255,255,255) to get a white background. |
Examples of histograms on colour images:
original image | histogram |
---|---|
Examples of histograms on greyscale images:
original image | histogram |
---|---|
std::invalid_argument | if the image is empty |
std::invalid_argument | if the image is neither greyscale nor 3-channel RGB |
std::invalid_argument | if the output dimensions are out of range |
Referenced by vz::Imagination::get().
cv::Mat vz::find_hough_lines | ( | const cv::Mat & | src, |
vz::VV4i & | lines | ||
) |
Apply the Hough Lines transform.
[in] | src | Input image must be canny edge detection. |
[out] | lines | Vector of 4 integer endpoints, where:
|
Example:
original image | input image (canny edge) | output image |
---|---|---|
Referenced by vz::Imagination::create_hough_lines_and_angles().
cv::Mat vz::rotate | ( | const cv::Mat & | src, |
double | degrees | ||
) |
Rotate an image an arbitrary number of degrees around the exact center of the image.
The image may be extended to ensure it isn't truncated during the rotation. New areas will be filled in as black.
Mathematical degrees are used, meaning the rotation is performed counter-clockwise. Specifying negative degrees will result in a clockwise rotation. Angles greater than 360.0 and lesser than -360.0 are valid, and are automatically normalized to 0-360.
Example:
input image | output image |
---|---|
std::invalid_argument | if the image is empty |
Referenced by vz::Imagination::get().
cv::Mat vz::resize | ( | const cv::Mat & | src, |
int | width, | ||
int | height = 0 |
||
) |
Resize the given image, while maintaining the original aspect ratio.
Either the width or the height must be specified as non-zero to indicate the desired image size. The other measurement will be calculated to keep the correct aspect ratio. If both the width and height have been specified, then it is up to the caller to maintain the correct aspect ratio.
std::invalid_argument | if the image is empty |
std::invalid_argument | if either the width or the height is negative |
std::invalid_argument | if both width and hight are invalid |
Referenced by vz::Imagination::create_mosaic(), letterbox(), vz::Imagination::resize(), and resize().
cv::Mat vz::resize | ( | const cv::Mat & | src, |
const double | scale | ||
) |
Resize the given image, keeping the same aspect ratio.
A scale of 1.0 will return an image of the same size, while 0.5 will be 1/2 the size, and 2.0 will be twice as large as the original.
std::invalid_argument | if the image is empty |
std::invalid_argument | if the scale is less than zero |
References resize().
cv::Mat vz::letterbox | ( | const cv::Mat & | src, |
int | width, | ||
int | height, | ||
const cv::Scalar & | colour = cv::Scalar(0, 0, 0) |
||
) |
Resize the image to fit within the given dimensions and apply a border to pad the size until the desired size is achieved.
The aspect ratio will be maintained. If the image does not fit perfectly within the given dimensions, apply letterboxing (black bars on top and bottom or left and right sides) to achieve the desired final dimensions. Default colour for the letterboxing is black.
Example:
input image | output image |
---|---|
std::invalid_argument | if the image is empty |
std::invalid_argument | if either the width or the height is less than 1 |
References letterbox(), and resize().
Referenced by letterbox().
cv::Mat vz::apply_DoG | ( | const cv::Mat & | src, |
const double | k = 1.6 , |
||
const double | sigma = 0.5 , |
||
const double | gamma = 1.0 |
||
) |
Perform 2 Gaussian blurs of different size, and subtract the 2nd from the 1st.
std::invalid_argument | if the image is empty |
References to_greyscale().
Referenced by apply_DoG_threshold(), and vz::Imagination::get().
cv::Mat vz::apply_DoG_threshold | ( | const cv::Mat & | src, |
const double | k = 200.0 , |
||
const double | sigma = 0.5 , |
||
const double | gamma = 1.0 |
||
) |
Obtain the DoG (Difference of Gaussian) by calling apply_DoG(), then find a threshold image from the result.
References apply_DoG().
Referenced by vz::Imagination::create_threshold().
vz::VRotatedRects vz::convert_contours_to_rotated_rects | ( | const VContours & | contours | ) |
Convert all of the contours to rotated rectangles.