I'm sorry, but I'm going to reformulate the question a bit, since I decided to choose a different approach, using opencv. That's the problem, I'm trying to add the image I created via opencv to the 60xx tags, but the image output is a little strange. I have no idea how to fix it. I was prompted that for the DCM_OverlayData tag, you need to transfer the image pixel by pixel, how to do this in the methods of the dcmtk libraries, I did not find anything similar, except create6xxx3000OverlayData, only this method does not work. How do I transfer an image to DCM_OverlayData?
there should be text on top of the image, but it outputs some strips
Code: Select all
#include "dcmtk/dcmdata/dcfilefo.h"
#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcuid.h"
#include <string>
#include <memory>
#include <cstring>
#include "dcmtk/dcmimgle/dcmimage.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include "Base64.h"
#include <vector>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
DcmFileFormat dicom, test;
if (dicom.loadFile("test.dcm").bad())
throw runtime_error("don't open DicomFile\n");
DcmDataset *d = dicom.getDataset();
E_TransferSyntax xfer = d->getOriginalXfer();
if (xfer == EXS_Unknown)
xfer = EXS_LittleEndianExplicit;
shared_ptr<DicomImage> dimage = make_shared<DicomImage>(d, xfer, CIF_NeverAccessEmbeddedOverlays);
if (not dimage or (dimage->getStatus() != EIS_Normal))
throw runtime_error("don't create DicomImage\n");
string str = "Text in Images";
Mat mat(30, str.length() * 11, CV_8U, Scalar(0, 0, 0));
// normalize(mat, mat, 0, 255, NORM_MINMAX);
Point text_position(10, 20);//Declaring the text position//
double font_size = 0.5;//Declaring the font size//
Scalar font_Color(255, 255, 255);//Declaring the color of the font//
int font_weight = 1;//Declaring the font weight//
putText(mat, str.c_str(), text_position, FONT_HERSHEY_COMPLEX, font_size, font_Color, font_weight);//Putting the text in the matrix//
/*vector<uchar> buf;
string encoded_png;
cv::imencode(".png", mat, buf);
auto base64_png = reinterpret_cast<const unsigned char*>(buf.data());
encoded_png = "data:image/jpeg;base64," + base64_encode(base64_png, buf.size());*/
{
const Sint16 arr[] = {1, 1};
if (
!d->putAndInsertUint16(DCM_OverlayRows, dimage->getHeight()).good() ||
!d->putAndInsertUint16(DCM_OverlayColumns, dimage->getWidth()).good() ||
!d->putAndInsertString(DCM_OverlayDescription, "additional information").good() ||
!d->putAndInsertString(DCM_OverlayType, "G").good() ||
!d->putAndInsertString(DCM_NumberOfFramesInOverlay, "1").good() ||
!d->putAndInsertSint16Array(DCM_OverlayOrigin, arr, 2).good() ||
!d->putAndInsertUint16(DCM_ImageFrameOrigin, 1).good() ||
!d->putAndInsertUint16(DCM_OverlayBitsAllocated, 16).good() ||
!d->putAndInsertUint16(DCM_OverlayBitPosition, 15).good() ||
!d->putAndInsertUint8Array(DCM_OverlayData, (const Uint8 *)mat.data, static_cast<size_t>(mat.rows * mat.cols * mat.channels())).good())
throw runtime_error("Can't get Dicom image\n");
dicom.saveFile("out.dcm", xfer);
}
return 0;
}