Ask Your Question
0

can't creat multiple trackbars in opencv2.4.9 with visual studio 2012

asked 2014-09-18 14:43:42 -0600

ashish gravatar image

updated 2014-09-18 16:12:05 -0600

unxnut gravatar image

Hello! i'm biggner in programming. I've copied this code in visual studio 2012,

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
Mat src;
void MyCallbackForBrightness(int iValueForBrightness, void *userData)
{
    Mat dst;
    int iValueForContrast = *( static_cast<int*>(userData) );
    //Calculating brightness and contrast value
    int iBrightness = iValueForBrightness - 50;
    double dContrast = iValueForContrast / 50.0;
    //Calculated contrast and brightness value
    cout << "MyCallbackForBrightness : Contrast=" << dContrast << ", Brightness=" << iBrightness << endl;
    //adjust the brightness and contrast
    src.convertTo(dst, -1, dContrast, iBrightness);
    //show the brightness and contrast adjusted image
    imshow("My Window", dst);
}

void MyCallbackForContrast(int iValueForContrast, void *userData)
{
    Mat dst;
    int iValueForBrightness = *( static_cast<int*>(userData) );
    //Calculating brightness and contrast value
    int iBrightness = iValueForBrightness - 50;
    double dContrast = iValueForContrast / 50.0;
    //Calculated contrast and brightness value
    cout << "MyCallbackForContrast : Contrast=" << dContrast << ", Brightness=" << iBrightness << endl;
    //adjust the brightness and contrast
    src.convertTo(dst, -1, dContrast, iBrightness);
    //show the brightness and contrast adjusted image
    imshow("My Window", dst);
}

int main(int argc, char** argv)
{
    // Read original image
    IplImage *dd = cvLoadImage("D:\\ashish.png");
    Mat src(dd);
    //if fail to read the image
    if (src.data == false)
    {
        cout << "Error loading the image" << endl;
        return -1;
    }
    // Create a window
    namedWindow("My Window", 1);
    int iValueForBrightness = 50;
    int iValueForContrast = 50;
    //Create track bar to change brightness
    createTrackbar("Brightness", "My Window", &iValueForBrightness, 100, MyCallbackForBrightness, &iValueForContrast);
    //Create track bar to change contrast
    createTrackbar("Contrast", "My Window", &iValueForContrast, 100, MyCallbackForContrast, &iValueForBrightness);
    imshow("My Window", src);
   // Wait until user press some key
    waitKey(0);
    return 0;
}

in output image is displayed on the window only with "one"(not two as in code) trackbar but as i changed the position of slider on that it give following error

Unhandled exception at at 0x75CCB727 in mynewopen.exe: Microsoft C++ exception: cv::Exception at memory location 0x0036F4CC.

---and also the name of the trackbar that is in code is not same as in displayed output it shows some random symbols.

edit retag flag offensive close merge delete

Comments

ashish, note , that you can format the code shown here.

edit your question, select the code with mouse, press 10101 button.

berak gravatar imageberak ( 2014-09-18 14:46:27 -0600 )edit

in output image is shown with only one trackbar but as i slide the slider i get messege Unhandled exception at 0x00C13964 in mynewopen.exe: 0xC0000005: Access violation reading location 0x00000000

ashish gravatar imageashish ( 2014-09-19 04:00:33 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-09-18 16:23:35 -0600

unxnut gravatar image

Your functions should not have formal parameters as you have defined. Instead, those should be global to the code. You should define the parameters after Mat src; close to the top as

int iValueForBrightness;
int iValueForContrast;
int *userData;

and remove the local definitions. Then, change the function headers to:

void MyCallbackForBrightness(int, void *)
void MyCallbackForContrast(int, void *)

and this should work. Also, why are you reading the image as IplImage. You can read the Mat straight by:

src = imread ( "D:\\ashish.png" );
edit flag offensive delete link more

Comments

in output image is shown with only one trackbar but as i slide the slider i get messege

Unhandled exception at 0x00C13964 in mynewopen.exe: 0xC0000005: Access violation reading location 0x00000000

ashish gravatar imageashish ( 2014-09-19 03:59:26 -0600 )edit

The best option you have is to go through a debugger. However, going through your code, I am not sure if convertTo will create a new matrix for you. You may want to allocate dst as the same size and type as src, possibly globally, as it is overwritten anyways in the callbacks.

unxnut gravatar imageunxnut ( 2014-09-19 08:37:47 -0600 )edit

Question Tools

Stats

Asked: 2014-09-18 14:43:42 -0600

Seen: 1,354 times

Last updated: Sep 18 '14