Ask Your Question
0

B&W Image brightness- using a Trackbar

asked 2012-09-25 05:31:28 -0600

Despair gravatar image

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);
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2012-09-25 05:45:15 -0600

Daniil Osokin gravatar image

updated 2012-09-25 05:45:47 -0600

Hi! I'm think, you should firstly create a window and then the trackbar. Here is an example: Adding a Trackbar to our applications!

edit flag offensive delete link more

Comments

Thanks alot. I have the Oreilly opencv book and I went back to that chapter didnt notice the FIRST WINDOW !! part :)

thanks again!

Despair gravatar imageDespair ( 2012-09-25 07:12:32 -0600 )edit

Question Tools

Stats

Asked: 2012-09-25 05:31:28 -0600

Seen: 822 times

Last updated: Sep 25 '12