2013年1月22日 星期二

【OpenCV】擷取2個視訊裝置的影像並調整亮度Compare illuminance of two image from different camera

利用OpenCV擷取兩個不同視訊的影像,因不同視訊所擷取個影像光線會有落差,所以調整兩個不同視訊的影像光線,將較暗的視訊影像調整至較亮的視訊影像一致。

#include <stdafx.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <windows.h>

int HistogramBins = 256;
float HistogramRange1[2]={0,255};
float *HistogramRange[1]={&HistogramRange1[0]};

CvHistogram *Histogram;

void CalibrationIlluminane(IplImage *image, double ratio)
{
    CvScalar scalarImg;
    double v;
    for(int i=0; i<image->height; i++)
    for(int j=0; j<image->width; j++)
    {
        scalarImg = cvGet2D(image, i, j);
        v = (int)(scalarImg.val[0]*ratio);
        cvSet2D(image, i, j, cvScalar(v));
    }
    printf("ratio = %f\n",ratio);
}

int CalculateGrayPixel(IplImage *image)
{
    Histogram = cvCreateHist(1,&HistogramBins, CV_HIST_ARRAY, HistogramRange);
    cvCalcHist(&image, Histogram);
    
    int luma = 0;
    for(int i = 0; i < HistogramBins; i++)
        luma += i*(int)(cvQueryHistValue_1D(Histogram, i)/10);
    
    return luma;
}

int main()
{
    CvCapture *capture0;
    CvCapture *capture1;

    IplImage *frame0;
    IplImage *frame1;
    IplImage* gray0;
    IplImage* gray1;


    capture0 =cvCaptureFromCAM(0); 
    capture1 =cvCaptureFromCAM(1); 

    cvNamedWindow("Webcam0",1); 
    cvNamedWindow("Webcam1",1);
    cvNamedWindow("Calibaration",1); 

    int count = 0;
    while(true)
    {
        count++;
        frame0 = cvQueryFrame(capture0);
        frame1 = cvQueryFrame(capture1);
        IplImage* frame0_pry = cvCreateImage(cvSize(frame0->width/2, frame0->height/2), frame0->depth, 3);
        cvResize(frame0, frame0_pry, CV_INTER_LINEAR);
        
        gray0 = cvCreateImage(cvGetSize(frame0_pry),IPL_DEPTH_8U,1);
        gray1 = cvCreateImage(cvGetSize(frame1),IPL_DEPTH_8U,1);
        cvCvtColor(frame0_pry, gray0, CV_RGB2GRAY);
        cvCvtColor(frame1, gray1, CV_RGB2GRAY);
        
        cvShowImage("Webcam0",gray0);
        cvShowImage("Webcam1",gray1);
        
        if(count%10 == 0)
        {
            int luma0 = 0, luma1 = 0;
            luma0 = CalculateGrayPixel(gray0);
            luma1 = CalculateGrayPixel(gray1);
            
            if(luma0 > luma1)
            {
                CalibrationIlluminane(gray1, (luma0*1.0)/luma1);
                cvShowImage("Calibaration",gray1);
            }
            if(luma1 > luma0)
            {
                CalibrationIlluminane(gray0, (luma1*1.0)/luma0);
                cvShowImage("Calibaration",gray0);
            }                
        }
        
        cvReleaseImage(&gray0);
        cvReleaseImage(&gray1);
        cvReleaseImage(&frame0_pry);

        if(cvWaitKey(10)>=0)
        {
            break; 
        } 
        
    }
    cvReleaseImage(&frame0);
    cvReleaseImage(&frame1);

    cvReleaseCapture(&capture0);
    cvReleaseCapture(&capture1);
    cvDestroyWindow("Webcam0");
    cvDestroyWindow("Webcam1");
}



左(第一個視訊),中(調整光線後的影像),右(第二個視訊)

相關文章:OpenCV學習日記

0 意見:

張貼留言