-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bounding Box Visualizer #31
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @file visualization_tools.hpp | ||
* @author Kartik Dutt | ||
* | ||
* Visualization tools to visualize detection and segmentation algorithms. | ||
* | ||
* mlpack is free software; you may redistribute it and/or modify it under the | ||
* terms of the 3-clause BSD license. You should have received a copy of the | ||
* 3-clause BSD license along with mlpack. If not, see | ||
* http://www.opensource.org/licenses/BSD-3-Clause for more information. | ||
*/ | ||
|
||
#ifndef MODELS_VISUALIZATION_TOOLS_HPP | ||
#define MODELS_VISUALIZATION_TOOLS_HPP | ||
|
||
#include <mlpack/prereqs.hpp> | ||
#include <opencv2/opencv.hpp> | ||
|
||
/** | ||
* Tools to visualize data and predictions. | ||
*/ | ||
class VisualizationTools | ||
{ | ||
public: | ||
/** | ||
* Save and plot bounding boxes on images. | ||
*/ | ||
template<typename ImageType> | ||
static void VisualizeBoundingBoxes( | ||
ImageType& images, | ||
arma::field<arma::vec>& boundingBoxes, | ||
const size_t imageWidth = 224, | ||
const size_t imageHeight = 224, | ||
const size_t imageDepth = 3, | ||
const bool cornerRepresentation = true, | ||
const bool plot = false, | ||
const bool saveImages = false, | ||
const std::vector<std::string> imagePath = std::vector<std::string>()) | ||
{ | ||
Comment on lines
+29
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the method name might be simplified into one like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I'll change it. |
||
if (saveImages) | ||
{ | ||
mlpack::Log::Assert(images.n_cols == imagePath.size(), | ||
"Mismatch between number of images," + std::to_string(image.n_cols) +\ | ||
" and image file paths" + to_string(imagePath.size()) + "."); | ||
} | ||
for (size_t i = 0; i < image.n_cols; i++) | ||
{ | ||
Comment on lines
+46
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because it processes a set of images sequentially, why don't you set this method to get an image, not a set of images? Users can extend this feature by calling it multiple times in their codes. Of course, we can create two methods, one is for a single image and the other is for multiple images. I'm just worried about this API may constrain the flexibility. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure that makes sense. Will make the changes. |
||
arma::vec bBoxes = boundingBoxes(0, i); | ||
arma::cube image(imageWidth * imageHeight * imageDepth, 1, 1); | ||
image.slice(0).col(0) = image.col(i); | ||
image.reshape(imageWidth, imageHeight, imageDepth); | ||
cv::mat img = CubeToOpenCV(image); | ||
for (size_t boxIdx = 0; boxIdx < bBoxes.n_elem; boxIdx += 4) | ||
{ | ||
cv::Point upperRightPoint, lowerLeftPoint; | ||
if (cornerRepresentation) | ||
{ | ||
upperRightPoint = cv::Point(bBoxes(boxIdx * 4), | ||
bBoxes(boxIdx * 4 + 1)); | ||
lowerLeftPoint = cv::Point(bBoxes(boxIdx * 4 + 2), | ||
bBoxes(boxIdx * 4 + 3)); | ||
} | ||
else | ||
{ | ||
upperRightPoint = cv::Point(bBoxes(boxIdx * 4), | ||
bBoxes(boxIdx * 4 + 1)); | ||
lowerLeftPoint = cv::Point(bBoxes(boxIdx * 4) + | ||
bBoxes(boxIdx * 4 + 2), bBoxes(boxIdx * 4 + 1) + | ||
bBoxes(boxIdx * 4 + 3)); | ||
} | ||
|
||
cv::rectangle(img, lowerRightPoint, upperRightPoint, | ||
cv::Scalar(rand() % 255, rand() % 255, rand() % 255)); | ||
Comment on lines
+72
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, it seems to be better for users to be able to choose a color of the bounding box. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how we would want to do this. There might be multiple objects in the image belonging to different class so either we could take input a map where each label corresponded to a tuple. Kindly let me know if this makes sense. |
||
} | ||
} | ||
|
||
if (plot) | ||
{ | ||
cv::imshow("Image", img); | ||
} | ||
|
||
if (saveImages) | ||
{ | ||
cv::imwrite(imagePath[i], img); | ||
} | ||
} | ||
|
||
private: | ||
/** | ||
* Convert armadillo cube to opencv matrix. | ||
*/ | ||
template<typename eT> | ||
cv::Mat<eT> CubeToOpenCVMat(const Mat<eT>& input) | ||
{ | ||
vector<cv::Mat_<T>> channels; | ||
for (size_t c = 0; c < input.n_slices; ++c) | ||
{ | ||
auto* data = const_cast<T *>(input.slice(c).memptr()); | ||
channels.push_back({int(input.n_cols), int(input.n_rows), data}); | ||
} | ||
|
||
cv::Mat dst; | ||
cv::merge(channels, dst); | ||
return dst; | ||
} | ||
}; | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this is commented out?