How to convert/create a dicom image from jpeg using Imebra library? - c++

I want to create of convert a jpeg image to a dicom image using imebra library on iOS platform. In the objective c examples of the library the steps to convert or read a dicom into jpeg are given, however, I am going through the documentation and trying to achieve vice versa (i.e. jpeg to dicom). I have no background of c++ so any help would be appreciated. Links to the library and documentation:
1.https://imebra.com
2.https://imebra.com/wp-content/uploads/documentation/imebra_build_2013-10-30_10-00-11/html/dicom2jpeg_2dicom2jpeg_8cpp-example.html
Some basic code below, thanks in advance.
-(void)createDcmFromJpg
{
NSString *imagePath = [[NSBundle mainBundle]pathForResource:#"test2" ofType:#"jpeg"];
//Create read stream
using namespace puntoexe;
ptr<stream> readStream(new stream);
readStream->openFile(NSStringToStringW(imagePath), std::ios::in);
//create dataset
ptr<streamReader> reader(new streamReader(readStream));
ptr<imebra::dataSet> testDataSet = imebra::codecs::codecFactory::getCodecFactory()->load(reader);
//set Tags
testDataSet->setString(0x0010, 0, 0x0010, 0, "testStr");
testDataSet->setString(0x0010, 0, 0x0010, 1, "testStr1");
//Next step/steps ?
}

The imebra::dataSet represents a collection of Dicom tags.
When you read a file (jpeg or dicom), then Imebra creates a dataSet which is a set of Dicom tags.
When reading a Dicom file then the dataSet will mirror exactly the tags stored in the Dicom file, while when reading a jpeg image Imebra will create a dataSet containing the proper dicom tag that embeds the jpeg image: all the needed Dicom tags will be already in the dataSet and you can add your own (like patient name, etc).
The next step consists in saving the dataSet to a Dicom stream:
with Imebra V4 (current):
// Load jpeg
std::unique_ptr<imebra::Dataset> testDataset(imebra::CodecFactory::load("/path/to/jpegfile.jpg"));
// Save as Dicom
imebra::CodecFactory::save(testDataset, "/path/to/file.dcm", imebra::codecType_t::dicom);
with Imebra 2015 (legacy):
// Open the Dicom file for writing
ptr<stream> writeStream(new stream);
writeStream->openFile(NSStringToStringW(dicomfilepath), std::ios::out);
// Declare the writer (will take care of writing into the stream)
ptr<streamWriter> writer (new streamWriter(writeStream));
// Use the dicom codec to write the dataSet into the file
ptr<imebra::codecs::codec> dicomCodec(new imebra::codecs::dicomCodec);
dicomCodec->write(writer, testDataSet);

Related

Is there a way to convert an image in the FLIR proprietary .seq format into .png?

I'm trying to convert a frame in .SEQ format into .PNG
I used the pleora ebus-sdk to automatically find and connect to my gigE FLIR camera.
Then I used:
PvBuffer* data = (PvBuffer*)myFLIR.GetFrameData();
void* data1 = myFLIR.PrepareTauSEQ(data);
size_t size = sizeof(FFF_FILE_HEADER) + sizeof(BI_DATA_T) + sizeof(GEOMETRIC_INFO_T) + data->GetAcquiredSize();
If I write data1 with size to a .seq file using std::ofstream, I can then open this .seq file in any FLIR software with no problem.
But, I want to have a PNG instead of a SEQ. How should I do this?

Compress DICOM file with DCMTK (C++)

damn i'm very frustated...
Following the example in this page http://support.dcmtk.org/docs/mod_dcmjpeg.html, I have written a C++ program to decompress a JPEG-compressed DICOM image file
Now I want to do the vice versa, from uncompressed to compressed and if I use the other example in the same page, with the same (or other file) the code compile and run but is not able to compress the file...
I saw that afetr the following code, the originale Xfer and the Current is the same, and this is not good because need to be different
dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);
It's like the chooseRepresentation method fail....
More the line
dataset->canWriteXfer(EXS_JPEGProcess14SV1)
return false
I saw that in the dcpixel.cc file, with debugging the code go in
DcmPixelData::canChooseRepresentation(.........
....
....
// representation not found, check if we have a codec that can create the
// desired representation.
if (original == repListEnd)
{
result = DcmCodecList::canChangeCoding(EXS_LittleEndianExplicit, toType.getXfer());
}
and result is FALSE....
How can I fix it? Someone have a code that works to compress a DICOM image with DCMTK or another library
This is the full code:
int main()
{
//dcxfer.h
DJDecoderRegistration::registerCodecs(); // register JPEG codecs
DcmFileFormat fileformat;
/**** MONO FILE ******/
if (fileformat.loadFile("Files/cnv3DSlice (1)_cnv.dcm").good())
{
DcmDataset *dataset = fileformat.getDataset();
DcmItem *metaInfo = fileformat.getMetaInfo();
DJ_RPLossless params; // codec parameters, we use the defaults
// this causes the lossless JPEG version of the dataset to be created
dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);
// check if everything went well
if (dataset->canWriteXfer(EXS_JPEGProcess14SV1))
{
// force the meta-header UIDs to be re-generated when storing the file
// since the UIDs in the data set may have changed
delete metaInfo->remove(DCM_MediaStorageSOPClassUID);
delete metaInfo->remove(DCM_MediaStorageSOPInstanceUID);
// store in lossless JPEG format
fileformat.saveFile("Files/test_jpeg_compresso.dcm", EXS_JPEGProcess14SV1);
}
}
DJDecoderRegistration::cleanup(); // deregister JPEG codecs
return 0;
}
When trying to compress an image you need to call
DJEncoderRegistration::registerCodecs();
Decompress is
DJDecoderRegistration::registerCodecs();

read tiff image tesseract and leptonica

I want to read tiff file. And I save txt each .png files which is in tiff file. If I use below code, I cannot save each page with its name. How can I do ? (Cpp code)
// Open input image with leptonica library
Pix *image = pixRead("/usr/src/tesseract-3.02/phototest.tif");
api->SetImage(image);
// Get OCR result
char *outText;
outText = api->GetUTF8Text();
According to the Leptonica API there is a special function pixReadTiff which reads a certain page from your tif file as Pix.
PIX *pixReadTiff(const char *filename, l_int32 n)
It returns NULL or an error if the page does not exists.
Just iterate through all pages.
To get the number of pages, you can use this function:
l_int32 tiffGetCount(FILE *fp, l_int32 *pn)
For other details you might want to look into the API yourself.
You might look into this: http://tpgit.github.io/Leptonica/tiffio_8c_source.html

How do I load an image (raw bytes) with OpenCV?

I am using Mat input = imread(filename); to read an image but I'd like to do it from memory instead. The source of the file is from an HTTP server. To make it faster, instead of writing the file to disk and then use imread() to read from it, i'd like to skip a step and directly load it from memory. How do I go about doing this?
Updated to add error
I tried the following but I'm getting segmentation fault
char * do_stuff(char img[])
{
vector<char> vec(img, img + strlen(img));
Mat input = imdecode(Mat(vec), 1);
}
See the man page for imdecode().
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imdecode
I had a similar problem. I needed to decode a jpeg image stream in memory and use the Mat image output for further analysis.
The documentation on OpenCV::imdecode did not provide me enough information to solve the problem.
However, the code here by OP worked for me. This is how I used it ( in C++ ):
//Here pImageData is [unsigned char *] that points to a jpeg compressed image buffer;
// ImageDataSize is the size of compressed content in buffer;
// The image here is grayscale;
cv::vector<unsigned char> ImVec(pImageData, pImageData + ImageDataSize);
cv:Mat ImMat;
ImMat = imdecode(ImVec, 1);
To check I saved the ImMat and was able to open the image file using a image viewer.
cv::imwrite("opencvDecodedImage.jpg", ImMat);
I used : OpenCV 2.4.10 binaries for VC10 on x86.
I hope this information can help others.

vtkImageData to DcmDataset

I hold a volume image in a vtkImageData and need to convert it to DcmDataset (DCMTK). I know that I need to set general DICOM tags like patient data to the data set. That's not the problem.
Especially I'm interested in putting the pixel data to DcmDataset. Does anybody know an example or can explain how to do that?
Thanks in advance
Quoting from the DCMTK FAQ:
Is there a tool that converts common graphic formats like PGM/PPM,
PNG, TIFF, JPEG or BMP to DICOM?
No, unfortunately, there is no such tool in DCMTK. Currently, you have to write your own little program for that purpose.
The following code snippet from the toolkit's documentation could be a starting point:
char uid[100];
DcmFileFormat fileformat;
DcmDataset *dataset = fileformat.getDataset();
dataset->putAndInsertString(DCM_SOPClassUID, UID_SecondaryCaptureImageStorage);
dataset->putAndInsertString(DCM_SOPInstanceUID, dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT));
dataset->putAndInsertString(DCM_PatientsName, "Doe^John");
/* ... */
dataset->putAndInsertUint8Array(DCM_PixelData, pixelData, pixelLength);
OFCondition status = fileformat.saveFile("test.dcm", EXS_LittleEndianExplicit);
if (status.bad())
cerr << "Error: cannot write DICOM file (" << status.text() << ")" << endl;
The current snapshot of the DCMTK (> version 3.5.4) contains a new
command line tool "img2dcm" that allows for converting JPEG images to
certain DICOM image SOP classes.
I would perhaps look first at the source code for img2dcm (documented here) to see the general process and then post back with any specific questions. IMHO, DCMTK is very powerful but extremely difficult to understand.