Ask Your Question
0

Programmatically control zoom Opencv

asked 2014-08-18 04:19:18 -0500

Xirious gravatar image

updated 2014-08-18 04:19:48 -0500

I've written a simple program in OpenCV to open a tiff image. This program is almost identical to this post (save for two lines) but I'll add it here for clarity:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
 if( argc != 2)
 {
 cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
 return -1;
 }

 Mat image;
 image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

 if(! image.data )                              // Check for invalid input
 {
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
 }

 namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
 imshow( "Display window", image );                   // Show our image inside it.
 moveWindow("Display window",0,0); //set window postion to top left
 setWindowProperty("Display window", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); //make fullscreen

 waitKey(0);                                          // Wait for a keystroke in the window
 return 0;

}

I've added two lines to ensure the image opens from the top left and is full screen (the second line may or may not be used in the future). What I would like to do is perform the same action as when I roll the mouse wheel and "zoom" on the image but I'd like to control this precisely so that control a) the position and b) level of zoom for the "blue" window in the following image (achieved by scrolling the mouse normally). enter image description here.

You'll notice a small grey window at the top of the image indicating how much and where the 'zoomed' in portion of the image currently is. How can I control this 'zoomed' window in code?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-08-18 08:16:46 -0500

itay gravatar image

I did this in Java and the code is like this:

ChangeCameraDisplay((int)(event.getX() + 500), (int)(event.getY() +500), getWindowManager().getDefaultDisplay().getWidth() * 2 ,getWindowManager().getDefaultDisplay().getHeight() * 2 );

while ChangeCameraDisplay function implementation is:

    private void ChangeCameraDisplay(int left, int top, int stretchWidthAdd, int stretchHeightAdd)
{

    cameraView.setTranslationX(-1 * left);
    cameraView.setTranslationY(-1 * top);



    //Get the width of the screen
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight = getWindowManager().getDefaultDisplay().getHeight();


    //Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = cameraView.getLayoutParams();

    //Set the width of the SurfaceView to the width of the screen
    lp.width = screenWidth + stretchWidthAdd;

    //Set the height of the SurfaceView to match the aspect ratio of the video 
    //be sure to cast these as floats otherwise the calculation will likely be 0
    lp.height = screenHeight + stretchHeightAdd;

    //Commit the layout parameters
    cameraView.setLayoutParams(lp);    
}

Explanation:

  1. I moved the image by translate the X and Y.

  2. I'm stretching the image by increasing the width and the height of the image on the current surface.

*I didn't change the surface size so the results look like zooming in.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-08-18 04:19:18 -0500

Seen: 1,307 times

Last updated: Aug 18 '14