我是 GDCM(c++) 的新手。我想要做的是为 dicom 图像编写一个非常简单的拉普拉斯滤波器。我已经得到了图像的缓冲区,但是接下来要做什么呢?有没有一种方法可以让我像在二维数组中那样访问坐标像素?更改像素值后,如何将缓冲区保存到原始图像?
到目前为止,我只是想学习 DICOM,所以我的代码实际上什么也没做,但我想知道的是如何访问像素数据并更改它。也许我可以将这些像素视为二维数组。我的代码是:
#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;
}
我认为没有方法可以访问二维格式的数组,但你可以做的是计算图像在一维数组中的位置。例如,如果 x 是列的索引,y 是行的索引,则像素颜色是位置 y* width + x。您还应该考虑像素大小。通常 dicom 图像大小是每个像素 2 个字节,在这种情况下像素颜色是位置 2* (y*width + x)
我是一名优秀的程序员,十分优秀!