qt 实训第十天

实现一个摄像头的灰度功能,即把照片变灰色

先在界面设计的上面建造一个pushbutton和三个radiobutton(gray , warm ,cold),分别是三个要实现的功能,还有一个显示图片的Label

dialog.h //仅仅增加了一个函数,三个实现的功能函数,一个QButtonGroup,

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QTimer>
#include <QButtonGroup>

#include "imgproc/imgproc.hpp"
#include "highgui/highgui.hpp"

#define VIDEO_WIDTH   640
#define VIDEO_HEIGHT  480

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    bool InitCamera();
    bool SaveImage(QImage*);
    bool Gray(QImage*);
    bool Warm(QImage*);
    bool Cold(QImage*);

private slots:
    void ShowImage();
    void OnBtCaptureClicked();

private:
    void AdjustWidgetPos();

private:
    Ui::Dialog *ui;

    QButtonGroup* m_RadioGroup;

    CvCapture* m_Capture;
   // IplImage* m_Frame;
     IplImage* m_Frame;

    // QImage Image;
     QTimer* m_TimerFlush;
     // if true,save image
     bool m_IsSave;

};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>
#include <QImage>
#include <QFileDialog>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    //初始化变量
    //Initialize variable
    m_RadioGroup = NULL;
    m_RadioGroup = new QButtonGroup(this);
    if(m_RadioGroup==NULL)
        exit(1);

    m_Capture = NULL;
    m_TimerFlush = NULL;
    m_IsSave = false;

    //调整ui显示函数
    //Adjust the ui position
    AdjustWidgetPos();
    //Set the image display position
    //ui->LB_IMAGE->move( 0,0 );
    //ui->LB_IMAGE->resize( VIDEO_WIDTH , VIDEO_HEIGHT );

    //this->resize( VIDEO_WIDTH, VIDEO_HEIGHT + 50);

    //Img.load("/home/pi/456.png");
    //ui->LB_IMAGE->setPixmap( QPixmap::fromImage( Img ) );

    //设置延时,让摄像头持续显示捕捉到的帧
    //Set the start time
    m_TimerFlush = new QTimer( this );
    if( m_TimerFlush == NULL)
        exit ( 0 );
    m_TimerFlush->setInterval( 20 );

    //连接按钮与槽函数
    //Set the SIGNAL and SLOTS
    connect( ui->BT_CAPTURE,SIGNAL( clicked() ),this,SLOT(OnBtCaptureClicked() ) );

    //连接显示图片槽函数,用定时器实现图像动态显示
    connect( m_TimerFlush,SIGNAL( timeout()),this, SLOT( ShowImage()));
    if( ! InitCamera() )
    {
        QMessageBox::critical( this, "Camera Error", "Initialize camera failed");
        exit ( 1 );
    }

    /* if( ! ShowImage())
    {
        QMessageBox::critical( this, "Camera Error", "Show image failed");
        exit ( 1 );
    }*/

    m_TimerFlush->start();
    ShowImage();

}

Dialog::~Dialog()
{
    delete ui;
    if(m_Capture)
        cvReleaseCapture( &m_Capture);
    if(m_TimerFlush)
        delete m_TimerFlush;
    if(m_RadioGroup)
        delete m_RadioGroup;
}

//初始化摄像头
//This function is initialize camera
bool Dialog::InitCamera()
{
    //列出本机摄像头设备
    //list the camera
    m_Capture = cvCreateCameraCapture( -1 );

    if(m_Capture == NULL)
        return false;

    //设置宽度和高度
    //Set the width and height
    cvSetCaptureProperty( m_Capture, CV_CAP_PROP_FRAME_WIDTH,VIDEO_WIDTH);
    cvSetCaptureProperty( m_Capture, CV_CAP_PROP_FRAME_HEIGHT,VIDEO_HEIGHT);
    return true;
}

//显示摄像头捕捉的图片
//This function is show the camera image
void Dialog::ShowImage()
{

    IplImage* m_Frame = NULL;

    QImage Image;

    m_Frame = cvQueryFrame( m_Capture );
    if( m_Frame == NULL )
        return ;

    Image = QImage( ( uchar* )m_Frame->imageData,m_Frame->width,m_Frame->height,QImage::Format_RGB888 ).rgbSwapped();

    switch(m_RadioGroup->checkedId())            //这个函数跟以后的if语句和ui的显示图片的语句不能改变顺序,只能这样
    {
        case 1:Gray(&Image);break;
        case 2:Warm(&Image);break;
        case 3:Cold(&Image);break;
    }

    if(m_IsSave)
    {
        m_IsSave = false;
        SaveImage(&Image);
    }

    ui->LB_IMAGE->setPixmap( QPixmap::fromImage( Image ));

}

//保存图片
//This function is save the image
bool Dialog::SaveImage(QImage* Image)
{

    QString FilePath = QFileDialog::getSaveFileName(this,"Save Image","","png(*.png);;bmp(*.bmp);;jpg(*.jpg)");

    if(Image->isNull())
        return false;
    //return Image->save( QString( "/home/zzukylin/Pictures/%1.png").arg(rand() % 100),"png", 100);
    return Image->save(FilePath);
}

//连接拍照按钮的槽函数
//This function is connect the capture button
void Dialog::OnBtCaptureClicked()
{
    m_IsSave = true;
}

//调整ui显示布局及按钮显示位置
//This function is adjust the ui position
void Dialog::AdjustWidgetPos()
{
    this->resize(VIDEO_WIDTH,VIDEO_HEIGHT+50);

    ui->LB_IMAGE->resize(VIDEO_WIDTH,VIDEO_HEIGHT);
    ui->LB_IMAGE->move(0,0);


    ui->BT_CAPTURE->move(50,VIDEO_HEIGHT+10);

    //设置RadioButton的显示位置,一一排列出来,一一往后排
    //Set the RadioButton position
    ui->RB_NORMAL->move(ui->BT_CAPTURE->pos().x()+ui->BT_CAPTURE->geometry().width()+10,VIDEO_HEIGHT+15);
    ui->RB_GRAY->move(ui->RB_NORMAL->pos().x()+ui->BT_CAPTURE->geometry().width()+10,VIDEO_HEIGHT+15);
    ui->RB_WARM->move(ui->RB_GRAY->pos().x()+ui->BT_CAPTURE->geometry().width()+10,VIDEO_HEIGHT+15);
    ui->RB_COLD->move(ui->RB_WARM->pos().x()+ui->BT_CAPTURE->geometry().width()+10,VIDEO_HEIGHT+15);

    //设置按钮分组
    //Set the RadioGroup
    m_RadioGroup->addButton(ui->RB_NORMAL,0);
    m_RadioGroup->addButton(ui->RB_GRAY,1);
    m_RadioGroup->addButton(ui->RB_WARM,2);
    m_RadioGroup->addButton(ui->RB_COLD,3);

    ui->RB_NORMAL->setChecked(true);

}

//图片显示灰色功能
//This function is let the picture become grey
bool Dialog::Gray(QImage* Image)
{

    int width = 0,height = 0;
    if(Image->isNull())
        return false;

    width = Image->width();
    height = Image->height();
    //white  RGB(255,255,255);
    //black RGB(0,0,0);
    //red   RGB(255,0,0);
    //green RGB(0,255,0);
    //blue  RGB(0,0,255);

    for(int x=0;x<width;x++)
    {
        for(int y=0;y<height;y++)
        {
            QColor OldColor = QColor(Image->pixel(x,y));

            int avg = (OldColor.red() + OldColor.green() + OldColor.blue()) / 3;
            Image->setPixel(x,y,qRgb(avg,avg,avg));
        }
    }

    return true;
}

//图片显示暖色功能
//This function is let the picture become warm
bool Dialog::Warm(QImage* Image)
{
    int width = 0,height = 0;

    if(Image->isNull())
        return false;

    width = Image->width();
    height = Image->height();
    int r,g,b;
    for(int x=0;x<width;x++)
    {
        for(int y=0;y<height;y++)
        {
            QColor OldColor = QColor(Image->pixel(x,y));
            r=OldColor.red() + 30;
            g=OldColor.green() + 30;
            b=OldColor.blue();

            r=qBound(0,r,255);
            g=qBound(0,g,255);
            Image->setPixel(x,y,qRgb(r,g,b));

        }
    }
    return true;
}
//图片显示冷色功能
//This function is let the picture become cold
bool Dialog::Cold(QImage* Image)
{
    int width = 0,height = 0;
    if(Image->isNull())
        return false;

    width = Image->width();
    height = Image->height();

    int r,g,b;
    for(int x=0;x<width;x++)
    {
        for(int y=0;y<height;y++)
        {
            QColor OldColor = QColor(Image->pixel(x,y));
            r=OldColor.red();
            g=OldColor.green();
            b=OldColor.blue()+30;

            b=qBound(0,b,255);

            Image->setPixel(x,y,qRgb(r,g,b));

        }
    }

    return true;
}

当三原色都为相同或相近的值的时候为灰色

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值