Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

B&W Image brightness- using a Trackbar

Hi, I've written the following code and I bumped into a few problems/questions.

First problem: Trackbar isnt showing. Second: is the ROI configured properly?

The thing is I want the Trackbar to start from the middle ( as 255 means no change to brightness ) and the image will change accordingly .

Anyways at the moment only the window loads and no images load since I want the image to load on screen only when the user changes the Trackbar position.

Thanks.

#include "highgui.h"
#include "cv.h"
#include <iostream>
#define FILENAME "picture.jpg"
#define WINDOWTITLE "Image To Ascii"
int beta=255;

IplImage* img_gray=NULL,*img_gray_smaller=NULL,*img_gray_regular_size=NULL,*img_gray_modify=NULL;

void onTrackbarSlide(int pos){
    *img_gray_modify=*img_gray_smaller;
    int inc=(pos<255)? 0-pos: 510-pos;
    cvSetImageROI(img_gray_modify,cvRect(0,0,80,60));
    cvAddS(img_gray_modify, cvScalar(inc),img_gray_modify);
    cvResetImageROI(img_gray_modify);
    img_gray_regular_size=cvCreateImage(cvSize(800, 600),IPL_DEPTH_8U,1);
    cvResize(img_gray_modify,img_gray_regular_size,CV_INTER_NN);
    cvReleaseImage(&img_gray_modify);
    cvShowImage(WINDOWTITLE, img_gray_regular_size);
}

int main(){
    cvCreateTrackbar("brightness", WINDOWTITLE, &beta, 510, onTrackbarSlide);
    char c='a';
    img_gray=cvLoadImage(FILENAME,CV_LOAD_IMAGE_GRAYSCALE),
    img_gray_smaller=cvCreateImage(cvSize(80, 60),IPL_DEPTH_8U,1),
    //img_gray_regular_size=cvCreateImage(cvSize(800, 600),IPL_DEPTH_8U,1);
    cvNamedWindow(WINDOWTITLE, CV_WINDOW_AUTOSIZE);
    cvResizeWindow(WINDOWTITLE,800,600);
    while(true){
        c=cvWaitKey(0);
        if (c==27)break;
        if (c==32){
            cvReleaseImage(&img_gray_smaller);
            cvReleaseImage(&img_gray);
            cvReleaseImage(&img_gray_regular_size);
            img_gray=cvLoadImage(FILENAME,CV_LOAD_IMAGE_GRAYSCALE);
            img_gray_smaller=cvCreateImage(cvSize(80, 60),IPL_DEPTH_8U,1);
            //img_gray_regular_size=cvCreateImage(cvSize(800, 600),IPL_DEPTH_8U,1);
            cvResize(img_gray,img_gray_smaller,CV_INTER_LINEAR);
            //cvResize(img_gray_smaller,img_gray_regular_size,CV_INTER_NN);
        }
        cvShowImage(WINDOWTITLE, img_gray_regular_size);    
    }
    cvReleaseImage(&img_gray_smaller);
    cvReleaseImage(&img_gray);
    cvReleaseImage(&img_gray_regular_size);
    cvReleaseImage(&img_gray_modify);
}