GDCM:我想对图像做一个简单的锐化滤镜,但不知道如何更改像素值

时间:2015-05-06 03:01:15

标签: c++ dicom medical gdcm

我是GDCM(c ++)的新手。我想要做的是将一个非常简单的拉普拉斯滤波器编码为dicom图像。我已经获得了图像的缓冲区,但接下来该怎么做?有没有一种方法可以让我像2D数组一样使用坐标访问像素?更改像素值后,如何将缓冲区保存到原始图像?

到目前为止,我只是想学习DICOM,所以我的代码实际上什么也没做,但我想知道的是如何访问像素数据并进行更改。可能我可以将这些像素视为2D数组。我的代码是:

#include "gdcmPhotometricInterpretation.h"
#include <iostream>
#include "gdcmImageReader.h"
#include "gdcmImageWriter.h"
#include "gdcmBitmapToBitmapFilter.h"
#include "gdcmImageToImageFilter.h"

using namespace gdcm;
using namespace std;

int main(int argc, char *argv[]) {

    if (argc < 2) {
        std::cerr << argv[0] << " input.dcm output.dcm" << std::endl;

        return 1;
    }

    const char *filename = argv[1];       //name of read-in file
    const char *outfilename = argv[2];    //name of write-out file

    // Instanciate the image reader
    gdcm::ImageReader reader;
    reader.SetFileName(filename);
    if (!reader.Read()) {
        cerr << "Could not read: " << filename << endl;

        return 1;
    }

    const Image &image = reader.GetImage();

    //Get some properties from the image
    //Dimension of the image
    unsigned int n_dim = image.GetNumberOfDimensions();
    const unsigned int *dims = image.GetDimensions();
    //Origin
    const double *origin = image.GetOrigin();
    const PhotometricInterpretation &pl = image.GetPhotometricInterpretation();

    for (unsigned int i = 0; i < n_dim; ++i) {
        std::cout << "Dim(" << i << "): " << dims[i] << std::endl;
    }

    for (unsigned int i = 0; i < n_dim; ++i) {
        cout << "Origin(" << i << "): " << origin[i] << endl;
    }
    std::cout << "PhotometricInterpretation: " << pl << endl;

// The output of gdcm::Reader is a gdcm::File
    gdcm::File &file = reader.GetFile();

// the dataset is the the set of element we are interested in:
    gdcm::DataSet &ds = file.GetDataSet();


    const unsigned int *dimension = image.GetDimensions();
    unsigned int dimX = dimension[0];
    unsigned int dimY = dimension[1];
    PixelFormat pf = image.GetPixelFormat();
    unsigned long len = image.GetBufferLength();
    char *buffer = new char[len];

    image.GetBuffer(buffer);

    /*char * p = buffer;
    double temp;
    int ybr2[3];
    for (int r = 0; r < dimX; ++r)
        for (int g = 0; g < dimY; ++g)
             {

                ybr2[0] = r;
                ybr2[1] = g;

                //*p++ = (char) ybr2[0];
                *p++ = (char) ybr2[1];

            }*/

    DataElement pixeldata = image.GetDataElement();

    pixeldata.SetByteValue(buffer, len);
    delete[] buffer;
    SmartPointer<Image> im = image;
    im->SetDataElement(pixeldata);

    gdcm::ImageWriter WriterNew;
    //WriterNew.SetImage(image);
    WriterNew.SetImage(*im);
    WriterNew.SetFileName(outfilename);

    if (!WriterNew.Write()) {
        std::cerr << "Could not write: " << outfilename << std::endl;
        return 1;
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

我认为没有方法以2D格式访问数组,但您可以做的是计算1D数组中的图像位置。例如,如果x是列的索引,并且y是行的索引,则像素颜色是位置y * width + x。您还应该考虑像素大小。通常dicom图像大小为每像素2个字节,在这种情况下像素颜色是位置2 *(y * width + x)