Qt中的Qt OpenCV更新区域

时间:2014-08-12 12:34:57

标签: c++ qt opencv roi

我开始在qt应用程序中集成Opencv,所以我有一个以下的程序结构:

QGraphicsView
    |
    |->QGraphicsPixmapItem (where the captured Image will be)
               |
               |
               |->QGraphicsRectItem (a rectangle that define the roi)

我有以下功能来处理图像:

void Inspection::Process()
{

   IplImage* m_CapureImage= Capture()->GetImage(); //cvLoadImage("e:\\Desert.jpg");


   IplImage* m_ProcessingImage= cvCreateImage(cvGetSize(m_CapureImage), IPL_DEPTH_8U, 1);

    cvCvtColor(m_CapureImage,m_ProcessingImage,CV_BGR2GRAY);


   // Process all ROI's in inspection    
    for (int var = 0; var < ROIs()->rowCount(QModelIndex()); ++var) {
        ROI* roi=ROIs()->data(ROIs()->index(var,0),Qt::UserRole).value<ROI*>();
        if(roi!=0)
            roi->Process(m_ProcessingImage);
    }
    QImage qImg = IplImage2QImage(m_ProcessingImage);
    m_BackgroundItem->setPixmap(QPixmap::fromImage(qImg));
}

/// 

    QImage IplImage2QImage(const IplImage *iplImage)
    {
        int height = iplImage->height;
        int width = iplImage->width;

        if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
        {
            const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
            QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
            return img.rgbSwapped();
        } else if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1){
            const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
            QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);

            QVector<QRgb> colorTable;
            for (int i = 0; i < 256; i++){
                colorTable.push_back(qRgb(i, i, i));
            }
            img.setColorTable(colorTable);
            return img;
        }else{
            qWarning() << "Image cannot be converted.";
            return QImage();
        }
    }

所以,我的问题是: 我改变了roi的位置并在iplImage的区域做了一些改变,我现在正在做的是再次打电话:

QImage qImg = IplImage2QImage(m_ProcessingImage);
m_BackgroundItem->setPixmap(QPixmap::fromImage(qImg));

所以我将再次加载所有的iplImage。有没有办法只更新pixmap中iplImage的具体ROI?

由于

编辑1:

我改变了图像显示实现,所以现在QGraphicsPixmapItem只显示原始捕获的图像,然后我将创建一个自定义QGraphicsRectItem并覆盖paint方法来绘制处理后的ROI

0 个答案:

没有答案