How to use Image Matrix.toImage()

Ken89148

Active member
Hello
I have been trying to convert a matrix to an image without success, as a simple example please see below;

var view = ImageWindow.activeWindow; // All white image
var matrixView = view.mainView.image.toMatrix(); // Convert image to matrix

console.writeln(matrixView.maxElement()) // returns 1 as expected for all white view
matrixView.div(10); // just to see that matrix changed
console.writeln(matrixView.maxElement()) // returns 0.1 as expected

// Now to convert matrix back to image, some examples I have tried;
matrixView.toImage(); // no error but no image
view = matrixView.toImage(); // no error but no image
view.mainView = matrixView.toImage(); // error
view.mainView.image = matrixView.toImage(); // error

I've tried many other mostly random variations without success. I'm sure its simple if you know how. Unfortunately there are no helpful comments or examples in Object Editor, just a list of objects.
Thanks Ken
 
Hi Ken,

matrixView.toImage(); // no error but no image
view = matrixView.toImage(); // no error but no image
view.mainView = matrixView.toImage(); // error

None of these can work because either you are not using the returned value or you are trying to assign an Image object to a View object, which does not make any sense (and is not implemented). In addition, you must create a view modification context to change a view's image. This is achieved by calling the View.beginProcess() and View.endProcess() methods.

The correct way to do what you want is as follows:

JavaScript:
var window = ImageWindow.activeWindow;
var view = window.mainView;

var matrixView = view.image.toMatrix();
console.writeln( matrixView.maxElement() ) // returns 1 as expected for all white view
matrixView.div( 10 ); // just to see that matrix changed
console.writeln( matrixView.maxElement() ) // returns 0.1 as expected

view.beginProcess();
view.image.assign( matrixView.toImage() );
view.endProcess();


Also see this little script, which generates a new image with a Gaussian profile:

JavaScript:
#include <pjsr/UndoFlag.jsh>

var window = new ImageWindow( 1, 1, 1 );
var view = window.mainView;
view.beginProcess( UndoFlag_NoSwapFile );
view.image.assign( Matrix.gaussianFilter( 40 ).toImage() );
view.endProcess();
window.zoomToFit();
window.show();

Let me know if this clarifies these concepts.
 
Hello Juan. I had not considered using the .image.assign with the .toImage, so yes I now understand. I'm working on a line detector using the hough transform and wanted to visualize the matrix to better understand the transform. Thank you so much for your help.
Ken
 
Back
Top