Save VtkVolume as stl file (3d data)? - vtk

I'm making a function using vtk now.
The process is as follows:
Load dicom series
we can see only some parts give to value in vtk Piecewise function.
write stl file
I need to some function about write vtkVolume to stl data.
How can I save vtkvolume data to stl file?
MY CODE AS BELOW:-
vtkDICOMImageReader *reader = vtkDICOMImageReader::New();
reader->SetDirectoryName("Dicom_Series");
reader->Update();
vtkPiecewiseFunction* opacitytransfer = vtkPiecewiseFunction::New();
opacitytransfer->AddPoint(-700, 0.0);
opacitytransfer->AddPoint(-101, 0.0);
opacitytransfer->ClampingOff();
vtkColorTransferFunction* colortranster = vtkColorTransferFunction::New();
colortranster->AddRGBPoint(-700, 0.0, 0.0, 0.0);
colortranster->AddRGBPoint(-101, 54.0 / 255.0, 154.0 / 255.0, 254.0 / 255.0);
colortranster->AddRGBPoint(-100, 237.0 / 255.0, 204.0 / 255.0, 159.0 / 255.0);
colortranster->ClampingOff();
vtkVolumeProperty* volumeproperty = vtkVolumeProperty::New();
volumeproperty->SetColor(colortranster);
volumeproperty->SetScalarOpacity(opacitytransfer);
volumeproperty->ShadeOn();
vtkFixedPointVolumeRayCastMapper* volumeMapper = vtkFixedPointVolumeRayCastMapper::New();
volumeMapper->SetInputConnection(reader->GetOutputPort());
vtkVolume* volume1 = vtkVolume::New();
volume1->SetMapper(volumeMapper);
volume1->SetProperty(volumeproperty);
vtkRenderer* aRenderer = vtkRenderer::New();
aRenderer->AddVolume(volume1);
aRenderer->SetBackground(0, 0, 0);
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(aRenderer);
renWin->SetSize(600, 600);
renWin->Render();
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
iren->Initialize();
iren->Start();

You cannot write out a volume image as an STL file. STL is a surface mesh format, which is completely different data type than an image. You need to extract an iso-surface from your volume. To do so, you can use VTK's MarchingCubes filter.
Here's an example:
https://lorensen.github.io/VTKExamples/site/Cxx/Modelling/MarchingCubes/
And here's the documentation for the vtkMarchingCubes class:
https://vtk.org/doc/nightly/html/classvtkMarchingCubes.html

Related

Undesired lines in FilledCurve (Wolfram Mathematica)

I am trying to make a custom arrowhead with Wolfram Mathematica (v. 10.0) using function FilledCurve. The result is looking fine on the Wolfram output. When I save the picture as pdf, some undesirable vertical line appears on the left border of my arrowhead. It is visible also in the latex document where I insert my pictures.
The code is
px = 0.7; py = 0.14; mpx = -0.2;
pts = {{-px, py}, {mpx, 0}, {-px, -py}};
ah = Graphics[{FilledCurve[{BSplineCurve[pts], Line[{{-px, -py}, {0, 0}, {-px, py}}]}]}]
To see the problem you need to save the output as pdf and open it in Adobe Acrobat Reader (or insert it in latex document).
Any suggestions?
Thank you!
It seems there is some bag in WM.
I solves the problem just by creating the required curve "manually" from lines.
The final code (with some minor improvements) is as follows:
px = 0.7; py = 0.14; mpx = -0.3;
pts = {{-px, py}, {mpx, 0}, {-px, -py}};
curpts = Table[f[t], {t, 0, 1, 0.02}];
f = BSplineFunction[pts];
linpts = {{-px, -py}, {0, 0}, {-px, py}};
allpts = Join[curpts, {linpts[[-2]], linpts[[-1]]}];
ah = Graphics[{FilledCurve[Line[allpts]], Line[linpts]}]
Result:

Aspect ration of a image for instagram

i want to change the aspect ratio of an image for the instagram with python .here is my code for change the aspact ration :
width,height=imageFile.size
aspectRatio = width/height
if(aspectRatio>=0.80 and aspectRatio<=1.90):
print("yeah")
else:
if(height>width):
futureHeight = width/.85
print(str(width)+" ,"+str(futureHeight))
print(width/futureHeight)
left = 0
int(futureHeight)
teetet = height-futureHeight/2
top = teetet / 4
right = width
bottom = height -150
im1 = imageFile.crop((left, top, right, bottom))
print(im1.size)
im1.show()
im1.save(image)
but still it show
ValueError: Incompatible aspect ratio.
whenever i try to upload this image
i resolved that using basic dimensions. using the inspector i saw that instagram convert images to 598.02x598.02 in the home page, so i typed:
im=PIL.Image.open(path)
im=im.resize((598,598), Image.ANTIALIAS)
im.save(path) # overwrite the image
instead of
im=PIL.Image.open(path)
baseheight = 560
hpercent = (baseheight / float(im.size[1]))
wsize = int((float(im.size[0]) * float(hpercent)))
im=im.resize((wsize, baseheight), PIL.Image.ANTIALIAS)
im.save(path)
in wich aspect ratio is out of range anyway.
now you can post it using something like instabot without troubles 'cause the aspect ratio is 1.0 .

Overwrite GPS coordinates in Image Exif using Python 3.6

I am trying to transform image geotags so that images and ground control points lie in the same coordinate system inside my software (Pix4D mapper).
The answer here says:
Exif data is standardized, and GPS data must be encoded using
geographical coordinates (minutes, seconds, etc) described above
instead of a fraction. Unless it's encoded in that format in the exif
tag, it won't stick.
Here is my code:
import os, piexif, pyproj
from PIL import Image
img = Image.open(os.path.join(dirPath,fn))
exif_dict = piexif.load(img.info['exif'])
breite = exif_dict['GPS'][piexif.GPSIFD.GPSLatitude]
lange = exif_dict['GPS'][piexif.GPSIFD.GPSLongitude]
breite = breite[0][0] / breite[0][1] + breite[1][0] / (breite[1][1] * 60) + breite[2][0] / (breite[2][1] * 3600)
lange = lange[0][0] / lange[0][1] + lange[1][0] / (lange[1][1] * 60) + lange[2][0] / (lange[2][1] * 3600)
print(breite) #48.81368778730952
print(lange) #9.954511162420633
x, y = pyproj.transform(wgs84, gk3, lange, breite) #from WGS84 to GaussKrüger zone 3
print(x) #3570178.732528623
print(y) #5408908.20172699
exif_dict['GPS'][piexif.GPSIFD.GPSLatitude] = [ ( (int)(round(y,6) * 1000000), 1000000 ), (0, 1), (0, 1) ]
exif_bytes = piexif.dump(exif_dict) #error here
img.save(os.path.join(outPath,fn), "jpeg", exif=exif_bytes)
I am getting struct.error: argument out of range in the dump method. The original GPSInfo tag looks like: {0: b'\x02\x03\x00\x00', 1: 'N', 2: ((48, 1), (48, 1), (3449322402, 70000000)), 3: 'E', 4: ((9, 1), (57, 1), (1136812930, 70000000)), 5: b'\x00', 6: (3659, 10)}
I am guessing I have to offset the values and encode them properly before writing, but have no idea what is to be done.
It looks like you are already using PIL and Python 3.x, not sure if you want to continue using piexif but either way, you may find it easier to convert the degrees, minutes, and seconds into decimal first. It looks like you are trying to do that already but putting it in a separate function may be clearer and account for direction reference.
Here's an example:
def get_decimal_from_dms(dms, ref):
degrees = dms[0][0] / dms[0][1]
minutes = dms[1][0] / dms[1][1] / 60.0
seconds = dms[2][0] / dms[2][1] / 3600.0
if ref in ['S', 'W']:
degrees = -degrees
minutes = -minutes
seconds = -seconds
return round(degrees + minutes + seconds, 5)
def get_coordinates(geotags):
lat = get_decimal_from_dms(geotags['GPSLatitude'], geotags['GPSLatitudeRef'])
lon = get_decimal_from_dms(geotags['GPSLongitude'], geotags['GPSLongitudeRef'])
return (lat,lon)
The geotags in this example is a dictionary with the GPSTAGS as keys instead of the numeric codes for readability. You can find more detail and the complete example from this blog post: Getting Started with Geocoding Exif Image Metadata in Python 3
After much hemming & hawing I reached the pages of py3exiv2 image metadata manipulation library. One will find exhaustive lists of the metadata tags as one reads through but here is the list of EXIF tags just to save few clicks.
It runs smoothly on Linux and provides many opportunities to edit image-headers. The documentation is also quite clear. I recommend this as a solution and am interested to know if it solves everyone else's problems as well.

freeimage convert png 24bit to 1bit black/white = grey/white?

I am using Freeimage.sf.net to load an png24bit and want to save it as black/white png.
using
FIBITMAP bib = Freeimage.Load("<24bit_png>");
FIBITMAP conv = FreeImage.ConvertColorDepth(bib, FREE_IMAGE_COLOR_DEPTH. FICD_01_BPP_THRESHOLD, (byte) 128);
FreeImage.Unload(bin); bib.SetNull();
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_PNG, conv, "<out_png>", FREE_IMAGE_SAVE_FLAGS.PNG_Z_BEST_COMPRESSION);
FreeImage.Unload(conv); conv.SetNull();
but the is not black/white, as it is #0b120c / white?
why does the threshold not create a bw-palette?
The line
FIBITMAP conv = FreeImage.ConvertColorDepth( bib,
FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP_THRESHOLD, (byte) 128 );
builds a palette for the most used colors.
Replacing it with
FIBITMAP bw = FreeImage.Threshold( biRef, threshold_BW );
will create a truly black-and-white image.

How to increase resolution of gif image?

How to increase resolution of gif image generated by rgl package of R (plot3d and movie3d functions) - either externally or through R ?
R Code :
MyX<-rnorm(10,5,1)
MyY<-rnorm(10,5,1)
MyZ<-rnorm(10,5,1)
plot3d(MyX, MyY, MyZ, xlab="X", ylab="Y", zlab="Z", type="s", box=T, axes=F)
text3d(MyX, MyY, MyZ, text=c(1:10), cex=5, adj=1)
movie3d(spin3d(axis = c(0, 0, 1), rpm = 4), duration=15, movie="TestMovie",
type="gif", dir=("~/Desktop"))
Output :
Update
Adding this line at the beginning of code solved the problem
r3dDefaults$windowRect <- c(0, 100, 1400, 1400)
I don't think you can do much about the resolution of the gif itself. I think you have to make the image much larger as an alternative, and then when you display it smaller it looks better. This is untested as a recent upgrade broke a thing or two for me, but this did work under 2.15:
par3d(windowRect = c(0, 0, 500, 500)) # make the window large
par3d(zoom = 1.1) # larger values make the image smaller
# you can test your settings interactively at this point
M <- par3d("userMatrix") # save your settings to pass to the movie
movie3d(par3dinterp(userMatrix=list(M,
rotate3d(M, pi, 1, 0, 0),
rotate3d(M, pi, 0, 1, 0) ) ),
duration = 5, fps = 50,
movie = "MyMovie")
HTH. If it doesn't quite work for you, check out the functions used and tune up the settings.

Resources