Adobe Photoshop CS3 Extended + MATLAB 7.0+
This will be the first of several posts about the new relationship between PS CS3 Extended and MATLAB 7.0 (or greater). I understand that it is a little difficult to get detailed information on this new and exciting relationship, so for this post, I will focus on the basic two-way communication between PS and MATLAB.
Overview
Because of this new ability between these two apps – the ultimate photo manipulation and compositing tool and the math juggernaut – so many new ways for expressing ourselves intelligently and creatively now exist. From scientific visualizations to creating your own visual effects… it’s like being able to take the aesthetic quality of your visualizations dramatically further… and also to be able to lift up the hood of a plug-in and tweak it to make a look that’s completely yours. If you are doing serious work in MATLAB and you are hungry to push the visuals or if you are working in a visual effects house, you should seriously consider working with PS CS3 Extended + MATLAB.
PS CS3 Extended: Animation Palette
In case you missed it, you might be asking yourself doesn’t visual effects refer to motion graphics – and in PS? The new PS CS3 Extended has a new Animation palette that allows you either import movie files or create your own video layer for frame-by-frame editing. I will get into the relationship between the Animation palette and MATLAB, but keep it in mind because it is possible – with a few tricks and modifications – to control the Animation palette content from MATLAB.
What is MATLAB?
Let us begin with what MATLAB is, as defined by Mathworks:
‘MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++, and Fortran.
You can use MATLAB in a wide range of applications, including signal and image processing, communications, control design, test and measurement, financial modeling and analysis, and computational biology [I would also add to that motion graphics and visual effects]. Add-on toolboxes (collections of special-purpose MATLAB functions, available separately) extend the MATLAB environment to solve particular classes of problems in these application areas.
MATLAB provides a number of features for documenting and sharing your work. You can integrate your MATLAB code with other languages and applications, and distribute your MATLAB algorithms and applications.’ - Mathworks website.
I like to think of MATLAB as – with all due respect to the Flash team – as what ActionScript wants to be as the language matures. Meaning specifically that MATLAB has been around long enough and has been used by enough brilliant people who have developed a solid codebase and set of libraries, MATLAB calls them Toolboxes, that are available commercially and as open source. Here is a thorough list of Toolboxes available from MATLAB’s site:
Math and Optimization
* Optimization Toolbox
* Symbolic Math Toolbox
* Extended Symbolic Math Toolbox
* Partial Differential Equation Toolbox
* Genetic Algorithm and Direct Search Toolbox
Statistics and Data Analysis
* Statistics Toolbox
* Neural Network Toolbox
* Curve Fitting Toolbox
* Spline Toolbox
* Model-Based Calibration Toolbox
Control System Design and Analysis
* Control System Toolbox
* System Identification Toolbox
* Fuzzy Logic Toolbox
* Robust Control Toolbox
* Model Predictive Control Toolbox
* Aerospace Toolbox
Signal Processing and Communications
* Signal Processing Toolbox
* Communications Toolbox
* Filter Design Toolbox
* Filter Design HDL Coder
* Wavelet Toolbox
* Fixed-Point Toolbox
* RF Toolbox
* Link for Code Composer Studio™
* Link for ModelSim®
* Link for Cadence Incisive
* Link for Analog Devices VisualDSP++®
Image Processing
* Image Processing Toolbox
* Image Acquisition Toolbox
* Mapping Toolbox
Test & Measurement
* Data Acquisition Toolbox
* Instrument Control Toolbox
* Image Acquisition Toolbox
* SystemTest
* OPC Toolbox
Financial Modeling and Analysis
* Financial Toolbox
* Financial Derivatives Toolbox
* GARCH Toolbox
* Datafeed Toolbox
* Fixed-Income Toolbox
Application Deployment
* MATLAB® Compiler
* Excel Link
* MATLAB® Builder for Excel®
* MATLAB® Builder for .NET
* MATLAB® Builder for Java™
Database Connectivity and Reporting
* Database Toolbox
* MATLAB® Report Generator
Distributed Computing
* Distributed Computing Toolbox
* MATLAB Distributed Computing Engine
That pretty much says it all, doesn’t it? AS3 is simply amazing, but because of the significant changes from AS2, as developers we have to build up our libraries of code again. As a disclaimer, MATLAB and Flash are not competitors… I only use the analogy simply to suggest that it is easier to stand on the shoulders of giants with MATLAB at this point in time. I love Flash CS3, but scientific visualizations and creating your own visual effects, MATLAB and PS CS3 Extended are perfect.
On a side note for Mac users, the last time I checked MATLAB released a beta to run native on the Intel processors. Woohoo!
Photoshop Toolbox for MATLAB
As I mentioned above, a Toolbox is an add-on extension of MATLAB. The Photoshop Toolbox includes functions that are geared towards giving MATLAB control over the Photoshop environment and allow pixels to be exchanged between both apps. It does this magic by essentially wrapping Javascript written specifically for Photoshop in MATLAB’s syntax and saving each method as a separate function that can be called from either the command line or a .m file within MATLAB. Essentially, anything you can do with Javascript to control Photoshop, you should be able to replicate with MATLAB in that loop… but that is another story. Out of the box, the Photoshop Toolbox comes with a full set of Functions that will allow you to handle the majority of communication between the two environments, which I will outline below after a few examples of what you can do with MATLAB <-> PS. If you are interested in learning more about developing Javascript for Photoshop, the main pdf documentation can be downloaded here.
Examples
As far as I know, and I could be a little off, the examples included with the new PS enable you to do the following from within MATLAB, with PS opened in the background:
– Open and modify a PS document: Opens a psd doc, gets the pixels into MATLAB, averages each channel and then sets the new pixels back into PS.
– Layered Document: Similar to above, however, it also gets an array of the layered names, sets the active layer to the last layer in the array and then gets, defines the mean and sets the pixels back into PS.
– Transparency: Similar to above, however, we can also grab the transparency pixel values of a layer using an attribute when getting the pixels. So if a layer contains an image that only occupies a portion of the image area, we would import the rgb values into one array and the transparency values into another. Then, with the exception of where the transparent pixels are, the mean of each channel is calculated and placed in a multi-dimensional array and then passed back into PS to set only the pixels which were not transparent.
– Animation Palette: Although it is not included as an example with PS or documented, I was eager to see if the same methodology could be applied to a video layer in the Animation palette, across multiple frames. Although, and please correct me if I am wrong, out-of-the-box you are unable to control the playback head in the Animation palette, with a few modifications of the psgetpixels() and pssetpixels() and a custom psgotoframe() script that I wrote, it is possible to do so. Several examples of an experiment I did exploring Algebraic Combinatorics – the slicing-n-dicing of a posterization algorithm’s expressions/variables/values and operators) can be seen at on Vimeo. This was simply an exercise in systematically stepping through all permutations of the Posterization Algorithm in search of interesting new effects. Which reminds me, if anyone knows of some great sources for visual effects/plug-in algorithms, please share it with us. At some point in the future, I will post more information if you are interested on how to tweak the 2 methods listed before.
Categorical List of Functions
Please note that it is possible these Functions might be inaccurate since the last time I checked.
General.
psconfig - Get and set the current preferences settings in Photoshop.
psjavascript - Execute the given text as JavaScript and return the result.
pslaunch - Launch Photoshop or attach to an already running Photoshop.
psquit - Quit the Photoshop application.
Document functions.
psclosedoc - Close the active document in Photoshop.
psdocinfo - Return information for the current document in Photoshop.
psdocnames - Return the names of all the open documents in Photoshop.
pshistogram - Return the histogram for the current document.
psnewdoc - Create a new document in Photoshop.
psnumdocs - Return the number of documents currently open in Photoshop.
psopendoc - Open the file in Photoshop.
pssetactivedoc - Bring the document with the given name to the front.
Color management functions.
pscolorprofile - Get or set the color profile of the active document.
pscolorsettings - Get or set the color settings.
psconvertprofile - Convert the active document to the new profile.
Layer functions.
pslayernames - Return the layer names.
psnewlayer - Create a new layer on the current document in Photoshop.
psnumlayers - Return the number of layers in the current document.
pssetactivelayer - Activate the current layer in the current document.
Channel functions.
pschannelnames - Return the channel names of the current document.
psnumchannels - Return the number of channels in the current document.
pssetactivechannels - Activate the current channels in the current document.
Path functions.
psgetpath - Return the path structure given the name.
psnewpath - Create a new path in Photoshop.
psnumpaths - Return the number of paths in the current document.
pspathnames - Return the path names in the current document.
Pixel functions.
psgetpixels - Get pixel data from current layer in Photoshop.
psimread - Open a document in Photoshop and return the pixel values.
psnewdocmatrix - Create a new document in Photoshop from a matrix.
psnewlayermatrix - Create a new layer in Photoshop from a matrix.
pssetpixels - Set pixel data to the current layer in Photoshop.
Filter functions.
psaddnoise - Run the Add Noise filter.
psaverage - Run the Average filter.
psblur - Run the Blur filter.
psblurmore - Run the Blur More filter.
psboxblur - Run the Box Blur filter.
pscustom - Run the Custom filter.
psdustandscratches - Run the Dust and Scratches filter.
psgaussianblur - Run the Gaussian Blur filter.
pshighpass - Run the High Pass filter
pslensblur - Run the Lens Blur filter.
psmaximum - Run the Maximum filter.
psmedian - Run the Median filter.
psminimum - Run the Minimum filter.
psmotionblur - Run the Motion Blur filter.
psoffset - Run the Offset filter.
psradialblur - Run the Radial Blur filter.
psshapeblur - Run the Shape Blur filter.
pssharpen - Run the Sharpen filter.
pssharpenedges - Run the Sharpen Edges filter.
pssharpenmore - Run the Sharpen More filter.
pssmartblur - Run the Smart Blur filter.
pssurfaceblur - Run the Surface Blur filter.
psunsharpmask - Run the Unsharp Mask filter.
Additional posts about PS CS3 Extended + MATLAB
– Color Combinatorics: In search of beautiful color palettes
June 16th, 2007 at 2:44 pm
John Nack was kind enough to provide me with this link for verifying MATLAB integration, which originally helped out considerably when I first began trying to get the two apps to talk to each other. It’s not that it’s difficult, it’s actually really easy and takes less then 5 minutes, as long as you have MATLAB 7.0+ and PS CS3 Extended. In addition, keep your eyes posted to John’s blog, John Nack on Adobe, because he told me he should be posting something about this subject soon.
June 19th, 2007 at 2:23 pm
Any thoughts on memory requirements to be running both Matlab and PS at the same time? Both are memory hogs in their own right — I’m guessing >2Gb just to open the door and peak inside!
June 19th, 2007 at 3:09 pm
dave, i was pleasantly surprised. on my test box, which is a g4 powerpc that’s several years old with only 1 gb of ram, i had no problem pushing the pixels back and forth. mind you, it didn’t scream in performance, but i attribute that more to the cpu then the two-way communication + having both apps running at the same time. naturally, the performance is also relative to:
- the screen size of both the source and target image(s);
- whether your source/targets are single layer/frame vs. video layer with multiple frames
- whether you’re using psgetpixels and then pssetpixels vs. psgetpixels and then ‘drawing’ in PS based on your results using custom .m scripts to control the drawing tools in PS, eg., using the ellipse tool to draw circles for visualizations.
the only time i ran into a memory issue was when, after getting and setting pixels from PS->ML->PS, i used a custom .m script that i wrote to Save To Web as a PNG sequence. there were two solutions to resolve the issue: 1) there is a javascript Purge method that can be used; and i created a simple 2) the pssaveasdoc method which allows me to define the filename and file format.
September 19th, 2007 at 10:53 am
Haven’t played with MATLAB. Kinda rolled my own artistic image processing, building on top of MATLAB-like numeric libraries in C++. Nice to know this possibility exits.
October 10th, 2007 at 9:04 am
just a heads up… i believe the MATLAB directory is installed with the standard version of PS CS3. if that is the case, you’ll still need PS CS3 Extended in order for MATLAB and PS to communicate.
October 29th, 2007 at 8:13 am
thanks for this information.
Im a designer and this articles is very good.
Best regards Boris
http://www.ooYes.net
April 24th, 2008 at 6:24 pm
hi, i am working on my project, i plot some figures in matlab and edit them in photoshop, and then save them into eps file. i am wondering if i can save the figure into eps using matlab command? that is to say, i need to get the figure’s name in photoshop and then i use matlab’s print to print it out. Thanks.
April 24th, 2008 at 8:05 pm
dongyan;
/ *
ok… i’m a little bit confused by what your objective is and what you would features you would like to integrate from PS and ML. i’ll try my best to answer your question, but feel free to clarify it if i misunderstand.
‘i am wondering if i can save the figure into eps using matlab command?’ - if the objective is to use ML to analyze the data, plot the results in PS – because of the better drawing/rendering capabilities – and then save that as an EPS file then it is possible to do so. even though tom ruark created a wonderful library of methods for handling the most popular functions, if there are additional features you need, like exporting to EPS, you can extend that library. there is documentation included with the install of PS that defines PS’s API (aka DOM - Document Object Model) that will show define the object and it’s properties. the document is called Photoshop CS3 JavaScript Ref.pdf and the object you’re interested in is called EPSSaveOptions.
if this is your objective, my recommendation is to write a test script in ExtendScript ToolKit – also included with your installation of PS CS3 – that will simply allow you to export a file as an EPS document. if you need sample code, refer to the example for JPEGSaveOptions for reference… keeping in mind that it will be a similar approach with the EPSSaveOptions, however, the name of the object will obviously be different and the properties will probably vary a bit.
once that test script works, you can then extend the MATLAB -> PS library by following the examples that are included with PS. a good, simple example that tom wrote is PSNEWLAYER.m. note that he’s basically wrapping up all of the ExtendScript code up in a string (pstext) that includes multiple lines, using try / catch for debugging and executing that code via psjavascriptu(pstext).
hopefully that takes you in the right direction… if not, feel free to give me a little more info.
* /
woohoo!
drew
June 12th, 2008 at 10:37 pm
Thanks for the long list of information. I heard from my friend that Adobe CS3 is brilliant but its requires lot of memory and high speed computer processor. I am using Adobe CS and with 512 MB or RAM and P4 and it still took lot of memory. So if we use CS3 then u can imagine what we need.
June 24th, 2008 at 6:17 am
dubai;
/ *
‘Thanks for the long list of information.’
any time.
as far as system requirements, i’m sure when the time is right for you, you will make the transition.
* /
take care & woohoo!
d.
June 24th, 2008 at 9:50 am
Is there, by any chance, a way to use photoshop via Matlab to select a region in an image?
I know you said this was a *categorical* list of functions, but I was really hoping for something like ‘psgetselectioncoords’ or something.
June 24th, 2008 at 11:05 am
darik;
/ *
it depends.
rectangle
for a rectangle, this will work:
% P = PSGETPIXELS(’undefined’, 0, 0, 10, 10) is a 10-by-10-C pixel matrix
% containing the top left 10-by-10-C square pixel. C is the number of
% active channels.
where p = psgetpixels(channels, top, left, bottom, right)
other shapes
if you’re interested in selecting different shape and/or using different selection tools like the magic wand, etc., and the method does not exist within the provided .m library, it’s easy enough to extend the library.
to extend the library, you can first test out your ExtendScript in ExtendScript Toolkit (estk). estk is installed with all CS3 apps. to check the DOM (api) documentation to verify the objects/classes/properties are available, the pdf for photoshop is called Photoshop CS3 JavaScript Ref and is included in the app directory of ps. once you’ve tested the script successfully within estk, you would then need to port that to a .m file, which really means you’ll need to basically wrap up your code in a string. the formatting is already provided for you… just take a look at a couple of the examples included in the .m library from ps for reference.
if, however, you can not find the object/class/property you’re looking for in the ps DOM, there is a one more option. there’s a plug-in that is included that will record the majority of actions – including functions that may not exist within the DOM – in a format that you can copy and paste into your own code. that plug-in is called ScriptingListener.plugin and is located in the Scripting Guide/Utilities directory.
hope that helps!
* /
woohoo!
d.
November 12th, 2009 at 11:14 am
Matlab reports that PS CS4 Extended seems unable to take commands. The Path to the PS Toolbox is correct and it Verifies OK, but testall fails immediately due to lack of response.
Matlab 2007a, CS4 ver 11 Universal , Minimac Intel 2GIG.
Please give some pointers on how to enable communication . Thanks
Luis
November 12th, 2009 at 6:41 pm
What version of the mac os are you on?
November 13th, 2009 at 6:30 am
MAC OS 10.5.8.
testall fails , however I tried running some of the tests individually and at least one verified OK. Both Matlab and P.S. are Universal versions. I also tried running them both in Rosseta but it failed just the same . If I try any of the PS Toolbox commands they report no response from P.S.
I am using XQuartz 2.1.6 (xorg-server 1.4.2-apple33)
For example :
>> p = psgetpixels();
Warning: Message from C preprocessor:
/bin/bash: cc: command not found
> In loadlibrary at 268
In psjavascriptu at 22
In psgetpixels at 137
??? Error using ==> psjavascriptu at 34
Photoshop appears to be unable to take commands.
Error in ==> psgetpixels at 137
psresult = psjavascriptu(pstext);
>>
November 19th, 2009 at 11:57 am
Well I seem to have fixed my MATLAB /PHOTOSHOP communications problem , although I do not know for sure which of my actions did it.
First I tried installing the Developers XTools from Apple, to explore the possibility that I did not have a c preprocessor, or a compiler for that matter. That did not change the situation . Matlab run as usual but P.S. CS4 did not respond.
Since this connection to P.S. was started on version CS3, and apparently abandoned after that , I decided to reinstall my old version of P.S. CS3. That did not work. Neither CS3 nor CS4 will respond . On a hunch I transfered the MATLAB folder from CS3 to CS4 and bingo CS4 started responding.
I removed CS3 and communication persists . So I am OK now but I submit all this info for whatever good it might provide to others.
December 9th, 2009 at 10:56 am
so is there a way to program your own plugin for photoshop using matlab? if yes, how can you develop your code in matlab to be a complete plugin? thanks
December 9th, 2009 at 7:12 pm
m-s. that’s a great question… i haven’t attempted it but theoretically you can compile your code as C out of matlab and integrate that with the PS SDK. i haven’t tried it… and i know only a subset of matlab’s code can be exported. here’s mathworks breakdown of what can / can’t be compiled: http://www.mathworks.com/products/compiler/compiler_support.html