Any method like np.concatenate to use in c++?

Viewed 109

I recently study with Dicom images deep learning. I want to move my dicom image processing code from python to c++. Following is my python code.

    def window_img(dcm, width=None, level=None):
        pixels = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept
        lower = level - (width / 2)
        upper = level + (width / 2)
        pixels[pixels<lower] = lower
        pixels[pixels>upper] = upper
        pixels = (pixels - (level - width//2)) / width
        return pixels

    def meta_dicom_convert(dcm, ww, wc):
        b = window_img(dcm, ww[0], wc[0])
        g = window_img(dcm, ww[1], wc[1])
        r = window_img(dcm, ww[2], wc[2])
        image = np.concatenate([b, g, r], axis=2)
    return image

And I try to do something like this in c++. However, I only found some method like cv::merge. I use cv::merge. But it seems a little differet from images combined by np.concatenate.

My C++ code:


    DcmFileFormat dfile;
    OFCondition result = dfile.loadFile("D:\\python\\RSNA\\stage_2_train\\ID_c8355f255.dcm");
    DcmDataset *dcmdataset = dfile.getDataset();
    double slope, intercept;
    dcmdataset->findAndGetFloat64(DCM_RescaleSlope, slope);
    dcmdataset->findAndGetFloat64(DCM_RescaleIntercept, intercept);

    DicomImage dcmimage_brain("D:\\python\\RSNA\\stage_2_train\\ID_c8355f255.dcm");
    DicomImage dcmimage_subdural("D:\\python\\RSNA\\stage_2_train\\ID_c8355f255.dcm");
    DicomImage dcmimage_bone("D:\\python\\RSNA\\stage_2_train\\ID_c8355f255.dcm");

    int nWidth = dcmimage_brain.getWidth();
    int nHeight = dcmimage_brain.getHeight();
    cout << "size =  " << nWidth << " x " << nHeight << endl;
    int wcenter = 40, wwidth = 80;
    int lower = wcenter - (wwidth / 2);
    int upper = wcenter + (wwidth / 2);
    dcmimage_brain.setWindow(wcenter, wwidth);
    Uint8 *pixeldata_brain = (Uint8*)(dcmimage_brain.getOutputData(8));

    if (pixeldata_brain)
    {

        for (int i = 0; i < nWidth * nHeight; ++i) {
            if (pixeldata_brain[i] < lower)
                pixeldata_brain[i] = lower;
            if (pixeldata_brain[i] > upper)
                pixeldata_brain[i] = upper;
            pixeldata_brain[i] = (255 * (pixeldata_brain[i] - lower)) / (upper - lower);
        }
    }
    Mat brain = Mat(nWidth, nHeight, CV_8UC1, pixeldata_brain);
    //brain.convertTo(brain, CV_8UC1);
    wcenter = 80;
    wwidth = 200;
    lower = wcenter - (wwidth / 2);
    upper = wcenter + (wwidth / 2);
    dcmimage_subdural.setWindow(wcenter, wwidth);
    Uint8 *pixeldata_subdural = (Uint8*)(dcmimage_subdural.getOutputData(8));
    if (pixeldata_subdural)
    {
        for (int i = 0; i < nWidth * nHeight; ++i) {
            if (pixeldata_subdural[i] < lower)
                pixeldata_subdural[i] = lower;
            if (pixeldata_subdural[i] > upper)
                pixeldata_subdural[i] = upper;
            pixeldata_subdural[i] = (255 * (pixeldata_subdural[i] - lower)) / (upper - lower);
        }
    }
    Mat subdural = Mat(nWidth, nHeight, CV_8UC1, pixeldata_subdural);
    //subdural.convertTo(subdural, CV_8UC1);
    wcenter = 40;
    wwidth = 380;
    lower = wcenter - (wwidth / 2);
    upper = wcenter + (wwidth / 2);
    dcmimage_bone.setWindow(wcenter, wwidth);
    Uint8 *pixeldata_bone = (Uint8*)(dcmimage_bone.getOutputData(8));
    if (pixeldata_bone) {
        for (int i = 0; i < nWidth * nHeight; ++i) {
            if (pixeldata_bone[i] < lower)
                pixeldata_bone[i] = lower;
            if (pixeldata_bone[i] > upper)
                pixeldata_bone[i] = upper;
            pixeldata_bone[i] = (255 * (pixeldata_bone[i] - lower) / (upper - lower));
        }
    }
    Mat bone = Mat(nWidth, nHeight, CV_8UC1, pixeldata_bone);
    imshow("bone", bone);
    //bone.convertTo(bone, CV_8UC1);
    //Mat bone = Mat(nWidth, nHeight, CV_16UC1, pbone);
    //bone.convertTo(bone, CV_8UC1);
    // new Image size
    Mat image = Mat::zeros(nWidth, nHeight, CV_8UC3);

    vector<Mat> channels;
    split(image, channels);
    channels.at(0) = brain;
    channels.at(1) = subdural;
    channels.at(2) = bone;
    merge(channels, image);
    imshow("mergeImage", image);
    waitKey();

result image: (left side: cv::merge) (right side: np.concatenate) enter image description here

Is cv::merge actually not just concatenate images in channels? Is there any other method I can use in c++ to do thing like np.concatenate? Any reply would be appreciate. thanks

0 Answers
Related