0

I am working on Surf detection algorithm in OpenCV3.0 with QtCreator.

I am cropping a QImage displayed on a QLabel using Mouse events.So that i can compare the cropped image with a live webcam feed

My problem is when i convert a QImage to Mat Image it works perfectly fine.

But when i try to convert a cropped QImage to Mat Image, my output picture is completely strange.

Here goes my Mouseevent code:

void surf_detection::mousePressEvent(QMouseEvent *ev)
{
if(ui->label_2->underMouse()){
   // cout <<"Entered Press"<<endl;
    origin = ev->pos();
    //if (!rubberBand)
        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);

        rubberBand->show();
}
}

void surf_detection::mouseMoveEvent(QMouseEvent *ev)
{
if(ui->label_2->underMouse()){
rubberBand->setGeometry(QRect(origin, ev->pos()).normalized());
// cout <<"Entered Move"<<endl;
}
}

void surf_detection::mouseReleaseEvent(QMouseEvent *ev)
{
if(ui->label_2->underMouse()){

QPoint a = mapToGlobal(origin);
QPoint b = ev->globalPos();

a = ui->label_2->mapFromGlobal(a);
b = ui->label_2->mapFromGlobal(b);

rubberBand->hide();

QPixmap OriginalPix(*ui->label_2->pixmap());

double sx = ui->label_2->rect().width();
double sy = ui->label_2->rect().height();
sx = OriginalPix.width() / sx;
sy = OriginalPix.height() / sy;

a.setX(int(a.x() * sx));
b.setX(int(b.x() * sx));
a.setY(int(a.y() * sy));
b.setY(int(b.y() * sy));


QRect myRect(a,b);

QImage newImage;
newImage = OriginalPix.toImage();

copyImage = newImage.copy(myRect);

ui->label_2->setScaledContents(true);
ui->label_2->setPixmap(QPixmap::fromImage(copyImage));
ui->label_2->repaint();

}

Here copyImage is a member variable which i intend to use for SURF detection.

My QImage to Mat Image code:

UMat surf_detection::QImagetocv(QImage img){

Mat tmp(img.height(),img.width(),CV_8UC3,
        (uchar*)img.bits(),img.bytesPerLine());
UMat con;
tmp.copyTo(con);
UMat result;
cvtColor(con,result,CV_BGR2RGB);
return result;

}

I don't understand where am I going wrong with the conversion. Comments appreciated!

I have attached a photo of my strange output here Output image.

MarKS
  • 494
  • 6
  • 22
  • Check the conversion code in [this](http://stackoverflow.com/a/33923407/5008845) answer, which handles different qimage types. – Miki Dec 04 '15 at 18:51
  • I am aware of this link. Since my QImage is RGB888 type i guess i am using the right conversion method. Issue is weird when i convert normal QImage to Mat it works perfectly. But converting a cropped QImage gives strange results. – MarKS Dec 04 '15 at 19:08

0 Answers0