使用opencv进行USB摄像头参数配置

#include 

cv::VideoCapture cap;

int fps = 30;
int hue = 50;//色调
int saturation = 50;//饱和度
int contrast = 50;//对比度
int brightness = 50;//亮度
int exposure = 50;//曝光

void on_FPS_trackbar(int ,void *)
{
    cap.set(CV_CAP_PROP_FPS, 30);//帧数30
}
void on_HUE_trackbar(int ,void *)
{
    cap.set(CV_CAP_PROP_HUE,hue * 0.01);
}
void on_SATURATION_trackbar(int ,void *)
{
    cap.set(CV_CAP_PROP_SATURATION,saturation * 0.01);
}
void on_BRIGHTNESS_trackbar(int ,void *)
{
    cap.set(CV_CAP_PROP_BRIGHTNESS,brightness * 0.01);
}
void on_CONTRAST_trackbar(int ,void *)
{
    cap.set(CV_CAP_PROP_CONTRAST,contrast * 0.01);
}
void on_EXPOSURE_trackbar(int ,void *)
{
    cap.set(CV_CAP_PROP_EXPOSURE,exposure * 0.01);
}



int main(int argc,char* argv[])
{
    if(argc != 2){
        printf("eg:cap 1\n");
        return -1;
    }

    int dev_number = atoi((const char*)argv[1]);
    cap.open(dev_number);
    if(!cap.isOpened())
    {
        printf("打开摄像头失败:number=%d\n",dev_number);
    }else {
        printf("宽度width = %.2f\n",cap.get(CV_CAP_PROP_FRAME_WIDTH));
        printf("高度height = %.2f\n",cap.get(CV_CAP_PROP_FRAME_HEIGHT));
        printf("帧率fps = %.2f\n",cap.get(CV_CAP_PROP_FPS));
        printf("色调hue = %.2f\n",cap.get(CV_CAP_PROP_HUE));
        printf("饱和度saturation = %.2f\n",cap.get(CV_CAP_PROP_SATURATION));
        printf("对比度contrast = %.2f\n",cap.get(CV_CAP_PROP_CONTRAST));
        printf("亮度brightness = %.2f\n",cap.get(CV_CAP_PROP_BRIGHTNESS));
        printf("曝光exposure = %.2f\n",cap.get(CV_CAP_PROP_EXPOSURE));

        cap.set(CV_CAP_PROP_FPS, 30);//帧数30
        cap.set(CV_CAP_PROP_HUE,hue * 0.01);
        cap.set(CV_CAP_PROP_SATURATION,saturation * 0.01);
        cap.set(CV_CAP_PROP_BRIGHTNESS,brightness * 0.01);
        cap.set(CV_CAP_PROP_CONTRAST,contrast * 0.01);
        cap.set(CV_CAP_PROP_EXPOSURE,exposure * 0.01);
    }

    cv::namedWindow("camera");
    cv::createTrackbar("帧率","camera", &fps, 100, on_FPS_trackbar);
    cv::createTrackbar("色调","camera", &hue, 100, on_HUE_trackbar);
    cv::createTrackbar("饱和度","camera", &saturation, 100, on_SATURATION_trackbar);
    cv::createTrackbar("对比度","camera", &contrast, 100, on_CONTRAST_trackbar);
    cv::createTrackbar("亮度","camera", &brightness, 100, on_BRIGHTNESS_trackbar);
    cv::createTrackbar("曝光","camera", &exposure, 100, on_EXPOSURE_trackbar);

    cv::Mat srcImage;
    while(1)
    {
        cap >> srcImage;
        cv::imshow("camera",srcImage);
        char c = cv::waitKey(1);
        if(c == 27)
        {
            break;
        }
    }

    cap.release();
    return 0;
}

 

你可能感兴趣的:(c++)