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...
Public Member Functions | |
Imagination () | |
Empty constructor. Use set_original(), an assignment, or one of the other explicit constructors to set the original image. More... | |
Imagination (const std::string &original_image_filename) | |
Constructor with an existing image file. More... | |
Imagination (const cv::Mat &original_image) | |
Constructor with an existing OpenCV image. More... | |
virtual | ~Imagination () |
Destructor. More... | |
virtual EMatType | to_type (const std::string &name) const |
Convert the given name to EMatType. If the name is not an exact match, will return EMatType::kInvalid. More... | |
virtual std::string | to_string (const EMatType type) const |
Provide a simple text name for the given mat image type. This is also used as the key to store images in mats. More... | |
virtual std::string | to_string (const EThresholdType type) const |
Provide a simple text name for the given threshold type. More... | |
virtual bool | operator== (const Imagination &rhs) const |
Deep comparison of the original image only. More... | |
bool | operator!= (const Imagination &rhs) const |
Similar to operator==(). More... | |
virtual Imagination & | clear () |
Clear all of the image cv::Mat objects cached in mats. More... | |
bool | empty () const |
Determine if an image has been set. More... | |
size_t | size () const |
Determine the number of images that currently exist in the mats image cache. More... | |
virtual cv::Mat & | get (const EMatType type, const int parm) |
Similar to the other get() method, but with the ability to pass in a single integer parameter. More... | |
virtual Imagination & | reset () |
Clears the object and resets the original image. More... | |
virtual VStr | save_all (const std::string &prefix="") |
Save all of the image files to disk by repeatedly calling save() with all the image types. More... | |
virtual vz::VContours | find_contours () |
Find the contours of an image using EMatType::kCannyEdgeDetection and vz::find_contours(). More... | |
virtual cv::Mat | create_threshold () |
Get a decent binary image. More... | |
virtual cv::Mat | create_hough_lines_and_angles () |
Find the Hough lines, and make the necessary calculations to determine the angle of each line. More... | |
virtual cv::Mat | create_mosaic (const bool show_labels=true) |
Create a mosaic using a thumbnail of all the images in mats. This is mostly for debug or logging purposes. More... | |
virtual cv::Mat | create_threshold (const EThresholdType type) |
virtual Imagination & | clear (const EMatType type) |
Clear a specific image type from mats. The image will be re-created the next time it is requested. More... | |
virtual Imagination & | clear (const std::string &name) |
Clear a specific image type from mats. The image will be re-created the next time it is requested. More... | |
bool | exists (const EMatType type) const |
Determine if the given image has been created. More... | |
bool | exists (const std::string &name) const |
Determine if the given image has been created. More... | |
virtual Imagination & | set_original (const std::string &original_image_filename) |
Reset this object and use the given image. All previous images in mats are cleared prior to the new image being set. More... | |
virtual Imagination & | set_original (const cv::Mat &original_image) |
virtual Imagination & | set_original (const EMatType type) |
Reset this object and use the given image. All previous images in mats are cleared prior to the new image being set. More... | |
virtual cv::Mat & | get (const EMatType type=EMatType::kOriginalImage) |
Get the specified image from cache. More... | |
virtual cv::Mat & | get (const std::string &name) |
cv::Mat & | operator[] (const EMatType type) |
Alias for get(). More... | |
cv::Mat & | operator[] (const std::string &name) |
Alias for get(). More... | |
virtual Imagination & | replace (const EMatType type, cv::Mat &image) |
Replace the given image type. More... | |
virtual Imagination & | replace (const std::string &name, cv::Mat &image) |
Replace the given image type. More... | |
virtual Imagination & | reset_using (const EMatType type) |
Clears the object and resets the original image to the given image type. More... | |
virtual Imagination & | reset_using (const std::string &name) |
Clears the object and resets the original image to the given image type. More... | |
cv::Mat | resize (const EMatType type, int width, int height=0) |
Resize (scale) the given image. More... | |
cv::Mat | resize (const std::string &name, int width, int height=0) |
Resize (scale) the given image. More... | |
cv::Mat | resize (const EMatType type, const double scale) |
Resize (scale) the given image. More... | |
cv::Mat | resize (const std::string &name, const double scale) |
Resize (scale) the given image. More... | |
virtual std::string | save (const EMatType type, const std::string &prefix="") |
Save the given image to disk. More... | |
virtual std::string | save (const std::string &name, const std::string &prefix="") |
Save the given image to disk. More... | |
Public Attributes | |
double | threshold_value |
Auto-detected threshold value. This is only set once the EMatType::kThreshold image is created. More... | |
EThresholdType | threshold_type |
The threshold type that was used. This is only set once the EMatType::kThreshold image is created. More... | |
vz::VContours | contours |
A vector of contours. More... | |
vz::VVDouble | contours_intervals |
Contour intervals. This is only set once the EMatType::kFindContoursIntervals image is created. More... | |
vz::VV4i | hough_lines |
The Hough lines, angles, angle intervals, and the correction angle are automatically calculated by create_hough_lines_and_angles() once get() is called with a parameter of EMatType::kHoughLines. More... | |
vz::VDouble | hough_lines_angles |
A vector of all angles for all lines in hough_lines. More... | |
vz::VVDouble | hough_lines_angle_intervals |
The interval composed of all angles in hough_lines_angles. More... | |
double | hough_lines_correction_angle |
The best angle according to hough_lines_angle_intervals. More... | |
typedef std::map< std::string, cv::Mat > | Mats |
Map where the key is an image type description, and the value is a OpenCV cv::Mat object. More... | |
Mats | mats |
Map where the key is an image type description, and the value is a OpenCV cv::Mat object. More... | |
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.
This way, if a variation is requested multiple times, it is retrieved from cache instead of performing the same calculations multiple times.
Methods to note include the multiple constructors, get(), and save().
typedef std::map<std::string, cv::Mat> vz::Imagination::Mats |
Map where the key is an image type description, and the value is a OpenCV cv::Mat
object.
While the map may be referenced directly, is is preferable to use get() or operator[] to ensure the requested image is built and cached correctly.
Example:
std::string
instead of EMatType
, the enum is still the preferred way to access the images stored within an Imagination
object.
|
strong |
The EMatType
represents the numerous variations of an image that can be created using Imagination
.
Enumerator | |
---|---|
kInvalid | Empty |
kOriginalImage | The original unmodified image. |
kO2Greyscale | The original image converted to greyscale.
|
kO2GaussianBlur | The original image with a Gaussian blur (default kernel size is 3x3).
|
kO2BlurGreyscale | The original image with a Gaussian blur and then converted to greyscale. |
kO2SimpleColourBalance | The original image with a simple colour balance applied. |
kO2Invert | The original image inverted.
|
kThreshold | The EMatType::kO2SimpleColourBalance image converted to black-and-white.
|
kErode | The EMatType::kThreshold image eroded.
|
kErodeAndDilate | The EMatType::kThreshold image both eroded then dilated.
|
kCannyEdgeDetection | The EMatType::kThreshold image with edge detection.
|
kFindContours | Contour detection with contours drawn over EMatType::kOriginalImage. |
kFindContoursIntervals | Similar to EMatType::kFindContours but includes intervals. |
kO2Histogram | Histogram of the original image.
|
kHoughLines | Hough lines transform. |
kO2AngleCorrected | The original image with an angle correction. |
kMosaic | Combines thumbnails of the other images into a single large mosaic. |
kO2DoG | Difference of Gaussian. |
kThresholdOtsu | |
kThresholdTriangle | |
kThresholdGaussian | |
kThresholdMeanC | |
kThresholdDoG | Threshold based on EMatType::kO2DoG. |
kUserDefined | Any custom image added to the map will come back as "user defined". This may apply to multiple images, while the other enums apply to a single image. |
kMax |
|
strong |
vz::Imagination::Imagination | ( | ) |
Empty constructor. Use set_original(), an assignment, or one of the other explicit constructors to set the original image.
References clear().
vz::Imagination::Imagination | ( | const std::string & | original_image_filename | ) |
Constructor with an existing image file.
References set_original().
vz::Imagination::Imagination | ( | const cv::Mat & | original_image | ) |
Constructor with an existing OpenCV image.
References set_original().
|
virtual |
Destructor.
|
virtual |
Convert the given name to EMatType. If the name is not an exact match, will return EMatType::kInvalid.
References kMax, kUserDefined, and to_string().
Referenced by create_mosaic(), get(), replace(), and save().
|
virtual |
Provide a simple text name for the given mat image type. This is also used as the key to store images in mats.
References kCannyEdgeDetection, kErode, kErodeAndDilate, kFindContours, kFindContoursIntervals, kHoughLines, kInvalid, kMax, kMosaic, kO2AngleCorrected, kO2BlurGreyscale, kO2DoG, kO2GaussianBlur, kO2Greyscale, kO2Histogram, kO2Invert, kO2SimpleColourBalance, kOriginalImage, kThreshold, kThresholdDoG, kThresholdGaussian, kThresholdMeanC, kThresholdOtsu, kThresholdTriangle, and kUserDefined.
Referenced by create_hough_lines_and_angles(), create_threshold(), exists(), get(), operator==(), replace(), resize(), save(), set_original(), and to_type().
|
virtual |
Provide a simple text name for the given threshold type.
References kAutoGaussian, kAutoMeanC, kDifferenceOfGaussian, kInvalid, kMax, kOther, kOtsu, and kTriangle.
|
virtual |
Deep comparison of the original image only.
The rest of the images can always be derived from the original which is why we only need to verify that the original images are the same. The comparison is pixel-by-pixel, so any image difference will be detected.
References vz::compare_exact(), empty(), kOriginalImage, mats, and to_string().
Referenced by operator!=().
|
inline |
Similar to operator==().
References clear(), and operator==().
|
virtual |
Clear all of the image cv::Mat
objects cached in mats.
References contours, contours_intervals, hough_lines, hough_lines_angle_intervals, hough_lines_angles, hough_lines_correction_angle, kInvalid, mats, threshold_type, and threshold_value.
Referenced by Imagination(), operator!=(), and set_original().
|
virtual |
|
virtual |
|
inline |
Determine if the given image has been created.
References exists(), and to_string().
Referenced by create_threshold(), empty(), exists(), and get().
|
inline |
Determine if the given image has been created.
|
inline |
Determine if an image has been set.
References exists(), and kOriginalImage.
Referenced by operator==().
|
inline |
Determine the number of images that currently exist in the mats image cache.
This includes the original image.
References kOriginalImage, and set_original().
|
virtual |
Reset this object and use the given image. All previous images in mats are cleared prior to the new image being set.
std::runtime_error | if the image fails to load (file does not exist?) |
References clear().
Referenced by Imagination(), replace(), reset(), reset_using(), set_original(), and size().
|
virtual |
cv::Mat
is empty, then this is equivalent to calling clear(). References clear(), kOriginalImage, mats, and to_string().
|
virtual |
Reset this object and use the given image. All previous images in mats are cleared prior to the new image being set.
std::runtime_error | if the image fails to load (file does not exist?) |
References set_original().
|
virtual |
Get the specified image from cache.
If the requested image does not exist, it is created from the original image and cached. Example:
std::out_of_range | if the type is invalid |
std::logic_error | if the original image has not been set |
std::logic_error | if the specified image type failed to load |
References vz::add_contours_and_limits_to_image(), vz::add_contours_to_image(), vz::apply_canny_edge_detection(), vz::apply_DoG(), vz::apply_gaussian_blur(), vz::apply_simple_colour_balance(), contours, create_hough_lines_and_angles(), create_mosaic(), create_threshold(), vz::dilate(), vz::erode(), exists(), find_contours(), hough_lines_correction_angle, vz::invert(), kAutoGaussian, kAutoMeanC, kCannyEdgeDetection, kDifferenceOfGaussian, kErode, kErodeAndDilate, kFindContours, kFindContoursIntervals, kHoughLines, kInvalid, kMax, kMosaic, kO2AngleCorrected, kO2BlurGreyscale, kO2DoG, kO2GaussianBlur, kO2Greyscale, kO2Histogram, kO2Invert, kO2SimpleColourBalance, kOriginalImage, kOtsu, kThreshold, kThresholdDoG, kThresholdGaussian, kThresholdMeanC, kThresholdOtsu, kThresholdTriangle, kTriangle, kUserDefined, mats, vz::plot_histogram(), vz::rotate(), threshold_value, vz::to_greyscale(), and to_string().
|
virtual |
std::logic_error | if the specified image type does not exist |
References exists(), kInvalid, kUserDefined, mats, and to_type().
|
virtual |
Similar to the other get() method, but with the ability to pass in a single integer parameter.
This is used by several specific types to alter the default behaviour. Other types that don't use a parameter will fall through to the original get() method.
Types that support a parameter:
type | parameter |
---|---|
EMatType::kO2GaussianBlur | kernel_size for vz::apply_gaussian_blur() |
EMatType::kO2BlurGreyscale | kernel_size for vz::apply_gaussian_blur() |
EMatType::kErode | number of iterations for vz::erode() |
EMatType::kErodeAndDilate | number of iterations for both vz::erode() and vz::dilate() |
EMatType::kCannyEdgeDetection | canny threshold for vz::apply_canny_edge_detection() |
References vz::apply_canny_edge_detection(), vz::apply_gaussian_blur(), vz::dilate(), vz::erode(), kCannyEdgeDetection, kErode, kErodeAndDilate, kO2BlurGreyscale, kO2GaussianBlur, kOriginalImage, kThreshold, mats, vz::to_greyscale(), and to_string().
|
inline |
Alias for get().
Example:
|
inline |
Alias for get().
Example:
References replace().
|
virtual |
Replace the given image type.
If an image already exists for this type, then it is replaced. If an image type does not yet exist, then it is inserted into the map. If the image is empty, then the type is completely removed from mats.
References to_string().
Referenced by clear(), and operator[]().
|
virtual |
Replace the given image type.
If an image already exists for this type, then it is replaced. If an image type does not yet exist, then it is inserted into the map. If the image is empty, then the type is completely removed from mats.
References kOriginalImage, kOther, kThreshold, mats, set_original(), threshold_type, threshold_value, and to_type().
|
inlinevirtual |
Clears the object and resets the original image.
All of the image types (except for EMatType::kOriginalImage) will need to be re-calculated. This used to be a separate function but is now an alias for set_original().
References kOriginalImage, and set_original().
|
inlinevirtual |
Clears the object and resets the original image to the given image type.
This used to be a separate function but is now an alias for set_original().
References set_original().
|
inlinevirtual |
Clears the object and resets the original image to the given image type.
This used to be a separate function but is now an alias for set_original().
References set_original().
|
inline |
Resize (scale) the given image.
Either the width or the height (but not both) should be specified as zero, in which case it will be calculated using the other dimension to maintain the original aspect ratio.
References vz::resize().
|
inline |
Resize (scale) the given image.
Either the width or the height (but not both) should be specified as zero, in which case it will be calculated using the other dimension to maintain the original aspect ratio.
References vz::resize().
|
inline |
Resize (scale) the given image.
A scale less than 1.0 makes the image smaller, while a scale greater than 1.0 makes the image larger.
References vz::resize().
|
inline |
Resize (scale) the given image.
A scale less than 1.0 makes the image smaller, while a scale greater than 1.0 makes the image larger.
References create_hough_lines_and_angles(), create_mosaic(), create_threshold(), find_contours(), vz::resize(), save(), save_all(), and to_string().
|
virtual |
Save the given image to disk.
Returns the filename used, which is based on to_string(). The image filename will be appended to the prefix (if any is specified), and unless the prefix includes an absolute or relative path, will be saved into the current working directory.
References to_string().
Referenced by resize(), and save_all().
|
virtual |
Save the given image to disk.
Returns the filename used, which is based on to_string(). The image filename will be appended to the prefix (if any is specified), and unless the prefix includes an absolute or relative path, will be saved into the current working directory.
References to_type().
|
virtual |
Save all of the image files to disk by repeatedly calling save() with all the image types.
Returns a vector of all the files that were saved.
References kInvalid, kMax, kMosaic, kUserDefined, mats, and save().
Referenced by resize().
|
virtual |
Find the contours of an image using EMatType::kCannyEdgeDetection and vz::find_contours().
The results are cached as Imagination::contours for later use. This method is called when get() is invoked with a parameter of EMatType::kFindContours or EMatType::kFindContoursIntervals.
References contours, vz::find_contours(), and kCannyEdgeDetection.
Referenced by get(), and resize().
|
virtual |
Get a decent binary image.
This will try 2 different methods of creating a binary image, automatically return the best image, and discard the other.
This method is called when get() is invoked with a parameter of EMatType::kThreshold.
Examples of binary threshold images created by this method:
original image | triangle threshold | gaussian threshold | output |
---|---|---|---|
References vz::apply_gaussian_blur(), exists(), vz::invert(), kAutoGaussian, kO2BlurGreyscale, kO2GaussianBlur, kO2SimpleColourBalance, kThreshold, kThresholdDoG, kThresholdGaussian, kThresholdMeanC, kThresholdOtsu, kThresholdTriangle, kTriangle, mats, threshold_type, vz::to_greyscale(), and to_string().
Referenced by get(), and resize().
|
virtual |
Find the Hough lines, and make the necessary calculations to determine the angle of each line.
This method is called when get() is invoked with a parameter of EMatType::kHoughLines.
The lines, angles, intervals, and best angle are stored in hough_lines, hough_lines_angles, hough_lines_angle_intervals, and hough_lines_correction_angle.
References vz::best_value_from_intervals(), vz::find_hough_lines(), hough_lines, hough_lines_angle_intervals, hough_lines_angles, hough_lines_correction_angle, vz::intervals(), kHoughLines, kThreshold, mats, and to_string().
Referenced by get(), and resize().
|
virtual |
Create a mosaic using a thumbnail of all the images in mats. This is mostly for debug or logging purposes.
References kMosaic, kOriginalImage, mats, vz::resize(), and to_type().
Referenced by get(), and resize().
|
virtual |
std::invalid_argument | if the threshold type is invalid |
std::logic_error | if the threshold image wasn't created (internal error) |
References vz::apply_DoG_threshold(), kAutoGaussian, kAutoMeanC, kDifferenceOfGaussian, kInvalid, kMax, kO2BlurGreyscale, kOriginalImage, kOther, kOtsu, kTriangle, threshold_value, and to_string().
Mats vz::Imagination::mats |
Map where the key is an image type description, and the value is a OpenCV cv::Mat
object.
While the map may be referenced directly, is is preferable to use get() or operator[] to ensure the requested image is built and cached correctly.
Example:
std::string
instead of EMatType
, the enum is still the preferred way to access the images stored within an Imagination
object. Referenced by clear(), create_hough_lines_and_angles(), create_mosaic(), create_threshold(), get(), operator==(), replace(), save_all(), and set_original().
double vz::Imagination::threshold_value |
Auto-detected threshold value. This is only set once the EMatType::kThreshold image is created.
Referenced by clear(), create_threshold(), get(), and replace().
EThresholdType vz::Imagination::threshold_type |
The threshold type that was used. This is only set once the EMatType::kThreshold image is created.
Referenced by clear(), create_threshold(), and replace().
vz::VContours vz::Imagination::contours |
A vector of contours.
This is only set once the EMatType::kFindContours image is created. This can then be used with functions such as cv::contourArea()
and vz::add_contours_to_image().
Referenced by clear(), find_contours(), and get().
vz::VVDouble vz::Imagination::contours_intervals |
Contour intervals. This is only set once the EMatType::kFindContoursIntervals image is created.
Referenced by clear().
vz::VV4i vz::Imagination::hough_lines |
The Hough lines, angles, angle intervals, and the correction angle are automatically calculated by create_hough_lines_and_angles() once get() is called with a parameter of EMatType::kHoughLines.
All detected Hough lines.
Referenced by clear(), and create_hough_lines_and_angles().
vz::VDouble vz::Imagination::hough_lines_angles |
A vector of all angles for all lines in hough_lines.
Referenced by clear(), and create_hough_lines_and_angles().
vz::VVDouble vz::Imagination::hough_lines_angle_intervals |
The interval composed of all angles in hough_lines_angles.
Referenced by clear(), and create_hough_lines_and_angles().
double vz::Imagination::hough_lines_correction_angle |
The best angle according to hough_lines_angle_intervals.
Referenced by clear(), create_hough_lines_and_angles(), and get().