5 #include <visp3/core/vpConfig.h>
6 #include <visp3/core/vpImage.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpDisplayOpenCV.h>
9 #include <visp3/gui/vpDisplayX.h>
10 #include <visp3/io/vpImageIo.h>
12 #if defined(VISP_HAVE_MODULE_IMGPROC)
14 #include <visp3/core/vpMomentObject.h>
15 #include <visp3/imgproc/vpImgproc.h>
19 int main(
int argc,
char *argv[])
22 #if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
25 #ifdef ENABLE_VISP_NAMESPACE
29 std::string input_filename =
"coins1.jpg";
31 bool white_foreground =
false;
33 for (
int i = 1; i < argc; i++) {
34 if (std::string(argv[i]) ==
"--input" && i + 1 < argc) {
35 input_filename = std::string(argv[i + 1]);
37 else if (std::string(argv[i]) ==
"--method" && i + 1 < argc) {
40 else if (std::string(argv[i]) ==
"--white_foreground") {
41 white_foreground =
true;
43 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
44 std::cout <<
"Usage: " << argv[0]
45 <<
" [--input <input image>]"
46 " [--method <0: Huang, 1: Intermodes, 2: IsoData, 3: "
47 "Mean, 4: Otsu, 5: Triangle>]"
48 " [--white_foreground]"
61 vpDisplayX d, d2, d3, d4, d5;
62 #elif defined(VISP_HAVE_GDI)
64 #elif defined(HAVE_OPENCV_HIGHGUI)
67 d.
init(I, 0, 0,
"Coins");
74 d2.init(I_bin, I.
getWidth(), 0,
"Binarisation");
80 d3.init(I_fill, 0, I.
getHeight() + 80,
"Fill holes");
97 for (
unsigned int cpt = 0; cpt < I_close.
getSize(); cpt++)
98 I_contours.bitmap[cpt] = I_close.
bitmap[cpt] ? 1 : 0;
101 std::vector<std::vector<vpImagePoint> > contours;
109 d5.init(I_draw_contours, 0, 2 * I.
getHeight() + 80,
"Contours");
119 for (
size_t i = 0; i < contours.size(); i++) {
120 std::vector<vpPoint> vec_p;
122 for (
size_t j = 0; j < contours[i].size(); j++) {
124 pt.
set_x(contours[i][j].get_u());
125 pt.
set_y(contours[i][j].get_v());
131 obj.fromVector(vec_p);
135 if (std::fabs(obj.get(0, 0)) >= I.
getSize() / 200) {
137 std::stringstream ss;
138 ss <<
"Coin " << nb_coins;
140 int centroid_x = (int)std::fabs(obj.get(1, 0) / obj.get(0, 0));
141 int centroid_y = (int)std::fabs(obj.get(0, 1) / obj.get(0, 0));
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") VP_OVERRIDE
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)
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)