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

时间:2022-04-02 18:05:49

I'm new to GDCM(c++). What I want to do, is to code a very simple laplacian filter to the dicom image. I have gotten the buffer of the image, but what to do next? Is there a method that can let me access the pixels with coordinates as in a 2D array? After changing the pixel values, how to save the buffer to the original image?

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


So far im just trying to learn DICOM, so my code is actually doing nothing, but what I want to know is how to access pixel data and change it. Probably I can treat those pixels as a 2D array. My code is:

到目前为止,我只是想学习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 个解决方案

#1


I think there is no method to access the array in a 2D format, but what you can do is to calculate the image position in the 1D array. For example, if x is the index of the column, and y the index of the row, the pixel color is the position y* width + x. You should also take into account the pixel size. Usually dicom images size is 2 bytes per pixel, in this case the pixel color is the position 2* (y*width + x)

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

#1


I think there is no method to access the array in a 2D format, but what you can do is to calculate the image position in the 1D array. For example, if x is the index of the column, and y the index of the row, the pixel color is the position y* width + x. You should also take into account the pixel size. Usually dicom images size is 2 bytes per pixel, in this case the pixel color is the position 2* (y*width + x)

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