I'm trying to get user input from the image window using the CImg library and C++. I want the user to draw a shape on the displayed window so I can save and use their data later, but I haven't found anything that resembles what I am trying to do. I'm just wondering if it is possible.
Asked
Active
Viewed 430 times
1 Answers
0
Basically, you need to set up to capture mouse-clicks like this:
#include <iostream>
#include "CImg.h"
using namespace cimg_library;
int main(int argc,char **argv) {
// Load a background image
CImg<unsigned char> src("image.jpg");
unsigned char red[]={255,0,0};
// Display it
CImgDisplay disp(src,"Image");
// Remember position of last mouse click
int prev_x = -1;
int prev_y = -1;
// Main display loop
while (!disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC()) {
CImgDisplay::wait(disp);
// When clicking on the image
if (disp.button()) {
int x = disp.mouse_x();
int y = disp.mouse_y();
std::cout << "x: " << x << ", y: " << y << std::endl;
if(prev_x!=-1){
src.draw_line(prev_x,prev_y,x,y,red).display(disp);
}
prev_x = x;
prev_y = y;
}
}
}
You would then append the x,y
positions to an std::vector
each time the user clicks, in order to remember them. Presumably, you would then want to check with each click, whether the current click position was within a few pixels of the start position and if so, close the polygon, fill it and add it to some list of shapes.

Mark Setchell
- 191,897
- 31
- 273
- 432