c++ - function in struct does not do anything -


i have simple struct represent percentage left, top, right, bottom of image use cropping:

struct roirect {     unsigned int left;     unsigned int top;     unsigned int right;     unsigned int bottom;      bool isallzero() {         return left == 0 && top == 0 && left == 0 && bottom == 0;     }      void getcvrect(cv::size cvsize, cv::rect &cvrect) {             int height = cvsize.height;             int width = cvsize.width;              int x = ((double)(left/height)*100);             int y = ((double)(top/height)*100);             int w = width - ((double)(right/width)*100);             int h = height - ((double)(bottom/width)*100);              cvrect.x = x;             cvrect.y = y;             cvrect.width = w;             cvrect.height = h;     } }; 

i initialize struct values 10,15,20,25. means image should cropped 10% right side, 15% top , on.

in class call getcvrect of struct , pass in size of image , raw cv::rect object above function calculate percentages , return rectangle cropped image:

//inside function cv::rect rect; //rect calculated getcvrect function of struct bool crop; //should crop or not? if == 0 not! if(!mroirect.isallzero()) {     crop = true;     mroirect.getcvrect(mat.size(), rect); } 

but effort in vain! pass in size first argument , pretty sure image size e.g. 640x480...the rect object after function call shows 640x480 ...so function absolutly nothing.

what doing wrong, , can fix or better smarter way of doing task?

the correct implementation (for interested)

    int x = ((double) left / 100) * width;     int y = ((double) top / 100) * height;     int w = width - ((double) right / 100) * width;     int h = height - ((double) bottom / 100) * width; 

the problem in 4 lines:

        int x = ((double)(left/height)*100);         int y = ((double)(top/height)*100);         int w = width - ((double)(right/width)*100);         int h = height - ((double)(bottom/width)*100); 

here left/height, etc... use integer division , result gets casted double. effect is, of course, x , y , both 0 , w == width , h == height. meant write likely

        int x = ((double) left)/height*100;         int y = ((double) top)/height*100;         int w = width - ((double) right)/width*100;         int h = height - ((double) bottom)/width*100; 

Comments