opencv test code-1

#include "mainwidget.h"
#include <QDebug>
MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    this->m_vLayout=new QVBoxLayout;
    this->m_hLayout=new QHBoxLayout;
    this->m_btnRGB=new QToolButton;
    this->m_btnRGB->setText(tr("RGB"));
    connect(this->m_btnRGB,SIGNAL(clicked()),this,SLOT(ZSlot2RGB()));

    this->m_btnGray=new QToolButton;
    this->m_btnGray->setText(tr("Gray"));
    connect(this->m_btnGray,SIGNAL(clicked()),this,SLOT(ZSlot2Gray()));

    this->m_btnFindCircle=new QToolButton;
    this->m_btnFindCircle->setText(tr("Recognize"));
    connect(this->m_btnFindCircle,SIGNAL(clicked()),this,SLOT(ZSlotFindCircle()));

    this->m_hLayout->addWidget(this->m_btnRGB);
    this->m_hLayout->addWidget(this->m_btnGray);
    this->m_hLayout->addWidget(this->m_btnFindCircle);
    this->m_vLayout->addStretch(1);
    this->m_vLayout->addLayout(this->m_hLayout);
    this->setLayout(this->m_vLayout);

    this->m_srcImg=cvLoadImage("/home/shell.albert/opencvtest/2.png");
    if(this->m_srcImg)
    {
        //cvCvtColor(this->m_srcImg,this->m_srcImg,CV_BGR2RGB);
        this->resize(this->m_srcImg->width+20,this->m_srcImg->height+20);
        this->m_dstImg=cvCreateImage(cvGetSize(this->m_srcImg),IPL_DEPTH_8U,1);
    }
    this->m_imgAction=IMG_SHOW_RGB;
}

MainWidget::~MainWidget()
{
    delete this->m_btnGray;
    delete this->m_btnRGB;
    delete this->m_btnFindCircle;
    delete this->m_hLayout;
    delete this->m_vLayout;
    if(this->m_srcImg)
    {
        cvReleaseImage(&this->m_srcImg);
    }
    if(this->m_dstImg)
    {
        cvReleaseImage(&this->m_dstImg);
    }
}
void MainWidget::paintEvent(QPaintEvent *event)
{
    QPainter tPainter(this);
    switch(this->m_imgAction)
    {
    case IMG_SHOW_RGB:
    {
        cvCvtColor(this->m_srcImg,this->m_srcImg,CV_BGR2RGB);
        uchar *imgData=(uchar*)this->m_srcImg->imageData;
        QImage tImage(imgData,this->m_srcImg->width,this->m_srcImg->height,this->m_srcImg->widthStep,QImage::Format_RGB888);
        tPainter.drawImage(10,10,tImage);
        cvCvtColor(this->m_srcImg,this->m_srcImg,CV_RGB2BGR);
    }
        break;
    case IMG_SHOW_GRAY:
    {
#if 0
        cvCvtColor(this->m_srcImg,this->m_dstImg,CV_BGR2GRAY);
        uchar *imgData=(uchar*)this->m_dstImg->imageData;
        QImage tImage(imgData,this->m_dstImg->width,this->m_dstImg->height,this->m_dstImg->widthStep,QImage::Format_Indexed8);
        QVector<QRgb> colorTable;
        for(qint32 i=0;i<256;i++)
        {
            colorTable.append(qRgb(i,i,i));
        }
        tImage.setColorTable(colorTable);
        tImage.setColorCount(255);
        tPainter.drawImage(10,10,tImage);
#endif
        IplImage *tDstImg=cvCreateImage(cvGetSize(this->m_srcImg),IPL_DEPTH_8U,1);
        IplImage *tImg=cvCreateImage(cvGetSize(this->m_srcImg),IPL_DEPTH_8U,1);
        cvCvtColor(this->m_srcImg,tDstImg,CV_BGR2GRAY);
        cvCanny(tDstImg,tImg,50,150,3);
        uchar *imgData=(uchar*)tImg->imageData;
        QImage tImage(imgData,tImg->width,tImg->height,tImg->widthStep,QImage::Format_Indexed8);
        QVector<QRgb> colorTable;
        for(qint32 i=0;i<256;i++)
        {
            colorTable.append(qRgb(i,i,i));
        }
        tImage.setColorTable(colorTable);
        tImage.setColorCount(255);
        tPainter.drawImage(10,10,tImage);
        cvReleaseImage(&tDstImg);
        cvReleaseImage(&tImg);
    }
        break;
    case IMG_FIND_CIRCLE:
    {
        //step 1:BGR to Gray.
        cvCvtColor(this->m_srcImg,this->m_dstImg,CV_BGR2GRAY);

        CvMemStorage *storage=cvCreateMemStorage(0);
        //cvSmooth(dstImg,dstImg,CV_GAUSSIAN,5,5);

        CvSeq *circles=cvHoughCircles(this->m_dstImg,storage,CV_HOUGH_GRADIENT,2,this->m_dstImg->width/3,300,100,0,200);
        IplImage *color=cvCreateImage(cvGetSize(this->m_dstImg),IPL_DEPTH_8U,3);
        cvCvtColor(this->m_dstImg,color,CV_GRAY2RGB);
        printf("circle total=%d\n",circles->total);
        for(int i=0;i<circles->total;i++)
        {
            float *p=(float*)cvGetSeqElem(circles,i);
            float tX=p[0];
            float tY=p[1];
            float tRadius=p[2];
            //the destination is on the right side
            //so here we ignore the left.
            if(tX<this->m_srcImg->width/2)
            {
                qDebug()<<"bypass left side";
                continue;
            }
            //draw a circle to wrap it.
            CvPoint pt=cvPoint(cvRound(p[0]),cvRound(p[1]));
            cvCircle(color,pt,cvRound(p[2]),CV_RGB(255,0,0),3,8,0);
            //draw a 10x10 rectangle aroud circle center.
            CvPoint pt1=pt;
            CvPoint pt2=pt;
            pt1.x-=5;
            pt1.y-=5;
            pt2.x+=5;
            pt2.y+=5;
            cvRectangle(color,pt1,pt2,cvScalar(255,0,0,0),3);

            //draw text.
            CvPoint txtPt=pt;
            txtPt.y-=40;
            txtPt.x-=60;
            const char *txtInfo="fint it!";
            CvFont tFont=cvFont(3.0,3);
            cvPutText(color,txtInfo,txtPt,&tFont,cvScalar(255,255,0));
        }
        //step3.display it.
        uchar *imgData=(uchar*)color->imageData;
        QImage tImage(imgData,color->width,color->height,color->widthStep,QImage::Format_RGB888);
        tPainter.drawImage(10,10,tImage);
        cvReleaseImage(&color);
    }
        break;
    default:
        break;
    }
}
void MainWidget::ZSlot2RGB()
{
    this->m_imgAction=IMG_SHOW_RGB;
    this->update();
}
void MainWidget::ZSlot2Gray()
{
    this->m_imgAction=IMG_SHOW_GRAY;
    this->update();
}
void MainWidget::ZSlotFindCircle()
{
    this->m_imgAction=IMG_FIND_CIRCLE;
    this->update();
}

The ratio test in OpenCV is a method for filtering out false matches in feature matching algorithms. In feature matching, keypoints are detected in two images and their descriptors are calculated. These descriptors are then matched to find corresponding keypoints in both images. However, some matches may be incorrect due to noise, occlusion, or other factors. The ratio test helps filter out these incorrect matches. It works by comparing the distance between the best and second-best matches for each keypoint. If the distance ratio is below a certain threshold, the match is considered correct. If the ratio is above the threshold, the match is discarded. The threshold value is typically set to 0.7, meaning that the distance to the second-best match should be at least 1.4 times greater than the distance to the best match. Here's an example code snippet for implementing the ratio test in OpenCV: ``` # detect keypoints and compute descriptors for both images kp1, des1 = detector.compute(img1, None) kp2, des2 = detector.compute(img2, None) # match descriptors using a matcher object matches = matcher.knnMatch(des1, des2, k=2) # apply ratio test to filter out false matches good_matches = [] for m, n in matches: if m.distance < 0.7 * n.distance: good_matches.append(m) ``` In this code, `detector` is a keypoint detector object and `matcher` is a descriptor matcher object. The `knnMatch` function returns a list of the two best matches for each descriptor in the first image (`des1`) with descriptors in the second image (`des2`). The ratio test is then applied to filter out false matches, and the remaining matches are stored in `good_matches`.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值