Ask Your Question
0

Why I cant resize the image with the track bar?

asked 2015-07-14 00:34:20 -0500

I am a beginner and using OpenCV 2.4.11. Here is my code:

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
// Global variables
const int slider_max = 100;
int slider;
Mat img;
// Callback function for trackbar event
void on_trackbar(int , void *)
{
    float scale = slider/100;
    Mat img_converted;
    Size s(img.size().width*scale, img.size().height*scale);
    resize(img, img_converted, s);
    imshow("Trackbar app", img_converted);
}

int main()
{
    img = imread("1.jpg");
    namedWindow("Trackbar app", CV_WINDOW_AUTOSIZE);
    slider = 0;
    createTrackbar("Size", "Trackbar app", &slider, slider_max, on_trackbar);
    while (char(waitKey(1)) != 'q') {}
    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-07-14 01:34:18 -0500

berak gravatar image
// Global variables
const int slider_max = 100;
int slider;
Mat img;
// Callback function for trackbar event
void on_trackbar(int , void *)
{
    // here's the main gotcha:
    // int(10)/int(100) == 0 !
    // we have to cast to float, to avoid the problem with integer division !
    // also, scale should never become 0, so add a minimum value of 1:
    float scale = float(slider+1)/100;

    Mat img_converted;
    Size s(img.size().width*scale, img.size().height*scale);
    resize(img, img_converted, s);
    imshow("Scaled", img_converted);
}

int main()
{
    img = imread("im/lena.jpg");
    namedWindow("Trackbar app", 0);

    // better start with a nice value (not 0)
    slider = 100;  

    // trick: put the trackbar in its own window,
    // it really sucks, if it has to resize along with the image !!
    createTrackbar("Size", "Trackbar app", &slider, slider_max, on_trackbar);

    // make it show without having to move the trackbar initially:
    on_trackbar(0,0);

    // just wait forever. (no loop nessecary)
    waitKey();
    return 0;
}
edit flag offensive delete link more

Comments

1

Thank you berak.

JJohnny gravatar imageJJohnny ( 2015-07-14 03:34:26 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-07-14 00:34:20 -0500

Seen: 929 times

Last updated: Jul 14 '15