read tiff image tesseract and leptonica - c++

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

Related

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

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);

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();

OpenCV imwrite() not saving image

I am trying to save an image from OpenCV on my mac and I am using the following code and so far it has not been working.
cv::imwrite("/Users/nickporter/Desktop/Gray_Image.jpg", cvImage);
Can anyone see why this might not be saving?
OpenCV does have problems in saving to JPG images sometimes, try to save to BMP instead:
cv::imwrite("/Users/nickporter/Desktop/Gray_Image.bmp", cvImage);
Also, before this, make sure you image cvImage is valid. You can check it by showing the image first:
namedWindow("image", WINDOW_AUTOSIZE);
imshow("image", cvImage);
waitKey(30);
I met the same problem and one possible reason is that the target folder to place your image. Suppose you want copy A.jpg to folder "C:\\folder1\\folder2\\", but in fact when folder2 doesn't exist, the copy cannot be successful(It is from my actual test, not from official announcement). And I solved this issue by checking whether the folder exists and create one folder if it doesn't exist. Here is some code may it help using c++ & boost::filesystem. May it help.
#include <boost/filesystem.hpp>
#include <iostream>
std::string str_target="C:\\folder1\\folder2\\img.jpg";
boost::filesystem::path path_target(str_target);
boost::filesystem::path path_folder=path_target.parent_path();//extract folder
if(!boost::filesystem::exists(path_folder)) //create folder if it doesn't exist
{
boost::filesystem::create_directory(path_folder);
}
cv::imwrite(str_target,input_img);
I also suggest to check folder permissions. Opencv quietly returns from imwrite without any exception even if output folder doesn't have write permissions.
I've just had a similar problem, loading in a jpg and trying to save it back as a jpg. Added this code and it seem to be fine now.
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(100);
And you need to include the param in your writefile.
cv::imwrite("/Users/nickporter/Desktop/Gray_Image.jpg", cvImage, compression_params);
OpenCV 3.2 imwrite() seems to have a problem to write jpg file with Windows Debug mode. I use this way instead of imwrite().
cv::Mat cvImage;
#ifdef DEBUG
IplImage image = IplImage(cvImage);
cvSaveImage("filename.jpg", &image);
#else
cv::imwrite("filename.jpg", cvImage);
#endif
The following function can be dropped into your code to support writing out jpg images for debugging purposes.
You just need to pass in an image and a filename for it. In the function, specify a path you wish to write to & have permission to do so with.
void imageWrite(const cv::Mat &image, const std::string filename)
{
// Support for writing JPG
vector<int> compression_params;
compression_params.push_back( CV_IMWRITE_JPEG_QUALITY );
compression_params.push_back( 100 );
// This writes to the specified path
std::string path = "/path/you/provide/" + filename + ".jpg";
cv::imwrite(path, image, compression_params);
}
Although it is not true for your case. This problem may arise if the image path given as argument to the cv::imwrite function exceeds the allowed maximum path length (or possibly allowed file name length) for your system.
for linux see: https://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs
for windows see: https://www.quora.com/What-is-the-maximum-character-limit-for-file-names-in-windows-10

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.

Qt multipage TIFF

I need to save a multipage TIFF from my program, but it seems Qt doesn't support multipage TIFF. Still, I need to do it. What would be the best way to go about doing it from my program?
So far, I'm thinking about using ImageMagick's command line utility to create the multipage TIFF from many JPEG files I create, or adding libtiff to my project and trying to use it, or using GDI+ (on Windows at least) to generate the TIFF.
Any other ideas I might have missed?
I'd like to avoid using external EXEs or DLLs if possible, i.e. if I can add a library directly to my project's source code it would be best.
Also, if you know of a project where it's already done, please post a link to it, I'd rather not reinvent the wheel.
Just wanted to add my info on a similar topic. I ended up just building libTiff from the latest (4.0.3) source. My project is all in x64, but it was pretty easy:
Download and unzip libTIFF source
Open the VS2010 (or whatever) for x64 (or x32) cmd
cd to your unzipped folder from step 1
type: nmake /f makefile.vc
Fetch the files from /libtiff folder and add to your project
Here's an example of reading 16-bit TIFF data:
TIFF *MultiPageTiff = TIFFOpen("C:\\MultiPageTiff.tif", "r");
std::vector<unsigned short*> SimulatedQueue;
//Read First TIFF to setup the Buffers and init
//everything
int Width, Height;
//Bit depth, in bits
unsigned short depth;
TIFFGetField(MultiPageTiff, TIFFTAG_IMAGEWIDTH, &Width);
TIFFGetField(MultiPageTiff, TIFFTAG_IMAGELENGTH, &Height);
TIFFGetField(MultiPageTiff, TIFFTAG_BITSPERSAMPLE, &depth);
//This should be Width*(depth / sizeof(char))
tsize_t ScanlineSizeBytes = TIFFScanlineSize(MultiPageTiff);
if(MultiPageTiff){
int dircount = 0;
do{
dircount++;
//I'm going to be QQueue'ing these up, so a buffer needs to be
//allocated per new TIFF page
unsigned short *Buffer = new unsigned short[Width*Height];
//Copy all the scan lines
for(int Row = 0; Row < Height; Row++){
TIFFReadScanline(MultiPageTiff, &Buffer[Row*Width], Row, 0);
}
SimulatedQueue.push_back(Buffer);
}while(TIFFReadDirectory(MultiPageTiff));
TIFFClose(MultiPageTiff);
}
Sources:
Building libTIFF from VS - http://www.remotesensing.org/libtiff/build.html#PC
Example MultiPage TIFF - http://www.remotesensing.org/libtiff/libtiff.html
Misc. Tiff Manuals - http://www.remotesensing.org/libtiff/man/
Qt uses libtiff to read and write TIFF. So I would use the same library, just less headache. Secondly: look at http://qt.gitorious.org/qt/qt/blobs/4.8/src/gui/image/qtiffhandler.cpp to get a clue of how Qt writes one QImage. To support multiple pages I think you need to use TIFFSetField() (see here, TIFFTAG_PAGENAME and TIFFTAG_PAGENUMBER). I would start to either expand the write() function or write something similar, where you:
TIFFClientOpen();
iterate over a list of QImages
set the page per QImage
do the things QTiffHandler::write() does
TIFFClose();
see also: http://code.google.com/p/multiphoton/source/browse/MatroxImagingLibrary.cpp?#1628