康耐视PR参数文件加载 cognex ccPMAlignPattern

        std::shared_ptr<ccPMAlignPattern> pattern = std::make_shared<ccPMAlignPattern>();
        ccFileArchive file(PatFile.toStdWString(), ccArchive::eLoading);
        file >> *pattern;

        ccPMAlignPattern& xPattern = *pattern;
        ccPelBuffer_const<c_UInt8> trainimg = xPattern.trainImage();
        const ccPelRoot<c_UInt8>* croot = trainimg.root();
        ccIPair theOffset = trainimg.offset();
        ccIPair rootOffset = trainimg.offsetRoot();
        ccPelRect rootRect = trainimg.windowRoot();
        qDebug() << "x,y:" << theOffset.x() << "," << theOffset.y();
        qDebug() << "x,yroot:" << rootOffset.x() << "," << rootOffset.y();
        qDebug() << "rootRoi:" << rootRect.ul().x() << "," << rootRect.ul().y() << ", " << rootRect.lr().x() << "," << rootRect.lr().y() << ":" << rootRect.width() << "," << rootRect.height();
        // 零拷贝:Mat 直接包装 Cognex 的像素缓冲区
        cv::Mat trainMat(croot->height(), croot->width(), CV_8UC1,
            (void*)croot->pels(), croot->rowUpdate());
        // 掩膜图像 
        ccPelBuffer_const<c_UInt8> maskimg = xPattern.maskImage();
        const ccPelRoot<c_UInt8>* mskcroot = maskimg.root();
        cv::Mat mskMat(mskcroot->height(), mskcroot->width(), CV_8UC1,
            (void*)mskcroot->pels(), mskcroot->rowUpdate());
        // draw 
        cv::Mat clorimg(mskcroot->height(), mskcroot->width(), CV_8UC3);
        cv::Point offset = cv::Point(maskimg.offset().x(), maskimg.offset().y());
        // 3. 训练区域(ROI 在图像坐标系中的矩形)
        ccPelRect region = xPattern.trainRegion();
        // region.ul()  → 左上角 cc2Vect
        // region.lr()  → 右下角 cc2Vect
        // region.width(), region.height()

        // 4. 模式原点(在训练图像 client 坐标系中)
        cc2Vect origin = xPattern.origin();
        qDebug() << "origin:" << origin.x() << "," << origin.y();

        // 5. 训练图像窗口的偏移(窗口左上角在 root 图像中的位置)
        ccIPair imgOffset = trainimg.offset();
        // imgOffset.x(), imgOffset.y()

        // 6. 训练方法
        ccPMAlignDefs::TrainMethod method = xPattern.trainMethod();
        // 训练窗口在原图中的偏移
        cc2Vect topLeft(imgOffset.x(), imgOffset.y());

        // 计算定位框相对于原点的偏移
        cc2Vect boxOffset = topLeft - origin;  // PR 矩形框左上角相对原点
        // paddding box 的 左上角位置
        cc2Vect padBoxOffset = theOffset - rootOffset;

        // 训练区域尺寸
        double boxW = region.width();
        double boxH = region.height();

        // train region 
        cv::Rect rect = cv::Rect(imgOffset.x(), imgOffset.y(), boxW, boxH);
        cv::Rect rootRoi(cv::Point(rootRect.ul().x(), rootRect.ul().y()), cv::Point(rootRect.lr().x(), rootRect.lr().y()));

            // 构造出原始图像区域
        cv::Mat srcimg(mskcroot->height(), mskcroot->width(), CV_8UC1, cv::Scalar(0));
        // train image copy to src region
        cv::Rect trainRoi(padBoxOffset.x(), padBoxOffset.y(), trainMat.cols, trainMat.rows);
        trainMat.copyTo(srcimg(trainRoi));

        //训练区域和原点
        prRotEditor->setRotatedRect(QRectF(theOffset.x(), theOffset.y(), rootRect.width(), rootRect.height()), prRotEditor->angle());
        originEditor->setOriginAngle(QPointF(origin.x(), origin.y()), 0);

 

posted @ 2026-06-29 17:56  哈库拉  阅读(3)  评论(0)    收藏  举报