5#include <visp3/core/vpConfig.h>
6#include <visp3/core/vpImage.h>
7#include <visp3/gui/vpDisplayFactory.h>
8#include <visp3/io/vpImageIo.h>
10#if defined(VISP_HAVE_MODULE_IMGPROC)
12#include <visp3/core/vpMomentObject.h>
13#include <visp3/imgproc/vpImgproc.h>
17int main(
int argc,
char *argv[])
20#if defined(VISP_HAVE_MODULE_IMGPROC) && defined(VISP_HAVE_DISPLAY)
23#ifdef ENABLE_VISP_NAMESPACE
27 std::string input_filename =
"coins1.jpg";
29 bool white_foreground =
false;
31 for (
int i = 1;
i < argc;
i++) {
32 if (std::string(argv[i]) ==
"--input" && i + 1 < argc) {
33 input_filename = std::string(argv[i + 1]);
35 else if (std::string(argv[i]) ==
"--method" && i + 1 < argc) {
38 else if (std::string(argv[i]) ==
"--white_foreground") {
39 white_foreground =
true;
41 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
42 std::cout <<
"Usage: " << argv[0]
43 <<
" [--input <input image>]"
44 " [--method <0: Huang, 1: Intermodes, 2: IsoData, 3: "
45 "Mean, 4: Otsu, 5: Triangle>]"
46 " [--white_foreground]"
58#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
68 display->init(I, 0, 0,
"Coins");
75 display2->init(I_bin, I.getWidth(), 0,
"Binarisation");
81 display3->init(I_fill, 0, I.getHeight() + 80,
"Fill holes");
94 display4->init(I_close, I.getWidth(), I.getHeight() + 80,
"Closing");
98 for (
unsigned int cpt = 0; cpt < I_close.
getSize(); cpt++)
99 I_contours.bitmap[cpt] = I_close.
bitmap[cpt] ? 1 : 0;
102 std::vector<std::vector<vpImagePoint> > contours;
110 display5->init(I_draw_contours, 0, 2 * I.getHeight() + 80,
"Contours");
120 for (
size_t i = 0;
i < contours.size();
i++) {
121 std::vector<vpPoint> vec_p;
123 for (
size_t j = 0;
j < contours[
i].size();
j++) {
125 pt.
set_x(contours[i][j].get_u());
126 pt.
set_y(contours[i][j].get_v());
132 obj.fromVector(vec_p);
136 if (std::fabs(obj.get(0, 0)) >= I.getSize() / 200) {
138 std::stringstream ss;
139 ss <<
"Coin " << nb_coins;
141 int centroid_x =
static_cast<int>(std::fabs(obj.get(1, 0) / obj.get(0, 0)));
142 int centroid_y =
static_cast<int>(std::fabs(obj.get(0, 1) / obj.get(0, 0)));
157#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11) && defined(VISP_HAVE_DISPLAY)
158 if (display !=
nullptr) {
162 if (display2 !=
nullptr) {
166 if (display3 !=
nullptr) {
170 if (display4 !=
nullptr) {
174 if (display5 !=
nullptr) {
Class that defines generic functionalities for display.
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void dilatation(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
static void erosion(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
Definition of the vpImage class member functions.
unsigned int getWidth() const
unsigned int getSize() const
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
Class for generic objects.
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
void set_x(double x)
Set the point x coordinate in the image plane.
void set_y(double y)
Set the point y coordinate in the image plane.
VISP_EXPORT void findContours(const VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I_original, vpContour &contours, std::vector< std::vector< VISP_NAMESPACE_ADDRESSING vpImagePoint > > &contourPts, const vpContourRetrievalType &retrievalMode=CONTOUR_RETR_TREE)
VISP_EXPORT void drawContours(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const std::vector< std::vector< VISP_NAMESPACE_ADDRESSING vpImagePoint > > &contours, unsigned char grayValue=255)
VISP_EXPORT void fillHoles(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I)
VISP_EXPORT unsigned char autoThreshold(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const vpAutoThresholdMethod &method, const unsigned char backgroundValue=0, const unsigned char foregroundValue=255)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.