I'm trying to implement and run OpenCV sample code Reading Geospatial Raster files with GDAL
For DEM model, i download N37W123.hgt.zip from the SRTM file located at the USGS , (that is in the Results section of that page).
but, DEM model not loaded to cv::Mat dem by cv::Mat dem = cv::imread(argv[2], cv::IMREAD_LOAD_GDAL | cv::IMREAD_ANYDEPTH ); and i get run time error throw std::runtime_error("DEM image type must be CV_16SC1");
1) Why is this happening?
2) All DEM data type are 16 signed integer. is it ok?
3) How can read DEM model block with GDALDataset::RasterIO or GDALRasterBand::ReadBlock directly?
Most Likely you have to enable/set WITH_GDAL flag to true in CMake while building opencv.
for reference:
https://docs.opencv.org/4.4.0/d4/da8/group__imgcodecs.html#imread
gdal is most likely expecting an HGT file, not a ZIP file: Link.
In Python you would extract the archive first using the zipfile module, then you can access the file directly into a numpy array:
from osgeo import gdal
ds = gdal.Open(filename)
data = ds.ReadAsArray()
EDIT: You've pointed out in a comment that you are using C++, so see the tutorial for reading the image directly: Link
Related
I am trying to convert the Ogg file to mp3/wav formats. I used:
FFmpeg
pyaudio
dlls
But nothing worked out.
Also, I am trying to first read the ogg data from an HTTP URL then want to convert it to mp3/wav, and then using speech_recognition converting to text.
If I don't use any method I get the following error.
Error: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if the file is corrupted or in another format.
Please suggest any libraries.
Code snippet:
audio_data = Data.get("audio")
if '.wav'or'.mp3' not in audio_data:
file = ("newspeech.mp3")
new_audio = urllib.request.urlretrieve(audio_data,file)
I trained a mnist_fashion model with tensorflow2.4, and then used opencv to call the generated .pb file and the following error occurred.
Net net = readNetFromTensorflow(weightFile);
String field 'tensorflow.FunctionDef.Node.ret' contains invalid UTF-8 data when parsing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.
Failed to parse GraphDef file: models/saved_model.pb) in cv::dnn::ReadTFNetParamsFromBinaryFileOrDie
I found a solution, just convert saved_model.pb to frozen_graph.pb.
[https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py][1]
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);
I'm trying to read in a .mat file from Matlab using matio and the variable comes in with the the correct rank and dims, but the data is null:
mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open("the_file.mat",MAT_ACC_RDONLY);
matvar = Mat_VarReadInfo(matfp,"my_var");
assert(matvar->rank==2);
assert(nrows==matvar->dims[0] && ncols==matvar->dims[1]);
but
matvar->data==NULL
I'm assuming something is going wrong reading in the .mat file, but I'm not sure how to diagnose it.
You should check the MATLAB file version.
Maybe the file is written in MATLAB version 7.3 or with compression.
If you build your lib (matio) without zlib you can not read compressed data.
If you build without zlib and HDF5 you can not read file version 7.3 files.
To access the data of a variable you have to use Mat_VarRead() instead of Mat_VarReadInfo(). Otherwise matvar->data is NULL.
I am trying to calculate the gradient of an image. I tried this code on the sample image given (Gourds6.png).
I used cmake . to create the CMakeFiles and then make. Everything works fine and the executable file is created. Now when I run the code using command ./computeGradient Gourds6.png out.png 1.5, it complains that:
Error:
itk::ImageFileWriterException (0x1446b40)
Location: "void itk::ImageFileWriter<TInputImage>::Write() [with TInputImage = itk::Image<float, 2u>]"
File: /usr/local/include/ITK-4.3/itkImageFileWriter.hxx
Line: 152
Description: Could not create IO object for file out.png
Tried to create one of the following:
You probably failed to set a file suffix, or
set the suffix to an unsupported type.
I haven't done any change to this code. It should work. I don't know what is wrong with it :( Do you have any idea?
Also, why don't we need to update the reader to read the image? Why do we only update the writer?
I appreciate for any help!
The pixel type of the output file in this example of ITK is float. And writing an image of float as a PNG image is not possible.
A list of supported files formats and corresponding data types is given on the wiki of ITK.
To save this image of float, here are formats that are expected to work :
Analyze (.img)
DICOM (.dic : failed on my PC)
GIPL (.gipl)
MetaImage (mhd) (out.mhd+out.raw)
Nrrd (.nhdr, .nrrd)
Stimulate (.spr)
VTK (.vtk)
The VTK file format works well and may be opened by the paraview software.
To use a PNG format, the image should be casted to unsigned char type. It may be performed by the CastImageFilter(). See this example. Another solution is to use the RescaleIntensityImageFilter(). See this example.
This question and its answer (which happens to be mine) explains how to convert a float image type to a ùnsigned char` image type and save it as PNG.
typedef itk::RescaleIntensityImageFilter< FloatImageType, UCharImageType > RescaleFilterType;
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter ->SetInput(importFilter->GetOutput());
rescaleFilter->SetOutputMinimum(0);
rescaleFilter->SetOutputMaximum(255);
typedef itk::ImageFileWriter< UCharImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( "output.png" );
writer->SetInput(rescaleFilter->GetOutput() );
writer->Update();
Finally, your last question : why do we only update the writer ? As the writer is updated, it will first check if its entries are up to date. If it is not the case, it will call filter->Update(), and so on.