1

I am working on a project that involves selecting an ROI from high resolution image(more like 5187x3268 like that). Right now i am using findContours in OpenCV to detect a round object(since hough circles is kind of slow for high res images). The problem is that due to high amount of texture on the object, findContours will be erroneous sometimes.

What i am doing now is, to show the user what findContours has detected in a Qt Window, and decide if it has detected correctly. If it has detected correctly, the user will press Ok button, if not No, Let me select button will be pressed.

When ever the user pressed No, Let me select, the application will start capturing mouseEvent and displays a rectangle using QRubberBand. I am using QLabel to display the image, since my screen size is 1920x1080, i have to resize the image to some resolution( lets say 1537x1280, so that it leaves some space for buttons). I am using opencv resize to resize the image.

width = myImageDisplayer.width()
height = myImageDisplayer.height()

resizedImage = cv2.resize(myImage,(height,width),cv2.INTER_LINEAR)

I am using ratios to calculate the size reduction like this(

xReduction = originalImage.rows()/resizedImage.rows()
yReduction = originalImage.cols()/resizedImage.cols()

, and multiplying the event.pos() coordinates with the ratios to get correct coordinates in the original image.

xrealCoordinates = event.pos().x()*xReduction
yrealCoordinates = event.pos().y()*yReduction

Since the coordinates will be of float, im rounding them off. The problem is in rounding off the float values, as i am losing precision in conversion.

Precision is important since i need to recalculate the principal coordinates(calculated by calibrating the stereo setup)after selecting ROI from the images.

enter image description here

How does Opencv calculates original image coordinates correctly after resizing? I observed it when i opened the same image using imshow, and if i drag my mouse i can see original image coordinates, even though the image has been resized to fit the screen.

If anybody can help me in this issue, i will be thankful.

the_parzival
  • 354
  • 3
  • 19
  • That rounding you mention is not the problem, and the calculation is sound. The problem is that you're working at scale, but can measure the cursor position only down to a pixel. Image a case where you scaled each dimension of the image to 1/4 of the original, so that each 4x4 group of pixels in the original becomes a single pixel in the result. Now, if you clicked on this pixel, all you know that it would fall somewhere in that 4x4 area of the original. – Dan Mašek Jul 21 '17 at 18:48
  • To let the user make selections precise to a pixel, I'd add support for scrolling, multiple zoom levels, and some buttons (or other suitable controls) for precise adjustment of the selection position and size. – Dan Mašek Jul 21 '17 at 18:51
  • Yeah, but even i tested with opencv's `selectRoi`. Eventhough it is resizing the image to certain dimensions, it recalculates the original image coordinates, and to my experience they are exact to pixel. I am wondering what is OpenCV doing to get the coorect pixel coordinates? – the_parzival Jul 21 '17 at 18:57
  • https://github.com/opencv/opencv/blob/master/modules/highgui/src/window_QT.cpp#L2929 | But still, even if you could get mouse screen position at subpixel accuracy, you still won't see what it is you're exactly selecting... – Dan Mašek Jul 21 '17 at 19:35
  • I actually dont have to see exactly what i am selecting, i would be glad if i can cover certain area around the subject. Since i have to recalculate the `principal coordinates` after `ROI`ing the image, the selection has to be exact or else there would be an error in the calculation. Right now im thinking of cheating by rounding of last two digits to zero i.e ` for example if the calculated image coordinates in original are 1573, then i will round it off to 1500`. Hope it works. – the_parzival Jul 21 '17 at 22:25
  • It would be really helpful if you could post a sample input image, along with the scaling factor you used, the coordinates in the two mouse events that set the selection, the results your calculation produced, and the results you would expect to see. – Dan Mašek Jul 21 '17 at 22:37
  • Will post all of them on monday, for the original image i have to go to my work location. While i was thinking about it, i got an idea if i can deduce integer scale factors after resizing the image. `Example : The above image is of resolution 7360x4912, if i can resize the image to 920x614 i.e 8 is the scaling factor`. I can comfortably multiply mouse position to get original image coordinates. Since im multiplying with an integer, i dont have to round off decimals. – the_parzival Jul 21 '17 at 22:40
  • I understand. BTW, how do you resize the image? Could you, please, [edit] the question and show that bit of code? ... Oh, OK, you specify the target size. I think your logic is sound, but it doesn't really matter whether it's a float or integer scaling factor. The rounding is not a problem, since each pixel is 1x1 unit. I feel the problem comes from somewhere else. Let's wait till monday. – Dan Mašek Jul 21 '17 at 22:44
  • Great. Maybe one thing I'd try is to calculate the bottom right corner of the selection as `xrealCoordinates = (event.pos().x() + 1)*xReduction - 1` and similar for y. – Dan Mašek Jul 21 '17 at 22:53

0 Answers0