Home Image-Pro Plus Automation with Macros

Macro for cutting 300 frames 13*13 pixels

(Originally posted by Dongfang on 7/26/2006)

I need to cut 300 frames time-series images using rectangle tool. Is there a macro to do this? It is time consuming to do it manually. I did it manually as following:

Step 1: open the time series images

Step 2: use the rectangle tool(13*13 pixels) to select AOI in time series image, "Ctrl+C" to copy the AOI,

Step 3: Ctrl+N to open new image,---OK---- Ctrl+V to save the AOI,

Step 4: Save as tiff in a folder.

Is there a macro to do that?



  • edited June 2013

    (Originally posted by KevinR on 7/26/2006)

    This is a good question, and the answer illustrates a couple of important issues when dealing with sequences. Two things that can help you here:

    • Saving from an image with an AOI will save only that area.
    • Turning off "Apply to Sequence" means that only that frame will be used as a source for saving.


    Sub YourMacro()
    	Dim bApply As Long, nFrames As Long, i As Long
    	Dim fileStr As String*255
    	ret = IpSeqGet(SEQGET_NUMFRAMES, nFrames)
    	ret = IpSeqGet(SEQ_APPLY, bApply)
    	' Set to apply to individual frames, _not_ the sequence
    	ret = IpSeqSet(SEQ_APPLY, 0)
    	For i=0 To nFrames-1
    		ret = IpSeqPlay(i)
    		' Set your AOI here...
    		' [...]
    		' Set a file location and save
    		' You will have to decide where to put them...
    		' See the help file for IpStAutoName( )
    		ret = IpStAutoName("C:\IPWIN60\IMAGES\EXP###.TIF",X,fileStr)
    		ret = IpWsSaveAs(fileStr, "TIF")
    	Next i
    	' Restore the state of sequence apply
    	ret = IpSeqSet(SEQ_APPLY, bApply)
    End Sub


  • (Originally posted by dewrosy on 8/2/2006)

    Dear KevinR

    thank you for your reply.
    The problem is the object that i track is moving


  • edited June 2013

    (Originally posted by KevinR on 8/2/2006)

    Here's some expanded code for this. It will copy an AOI of whatever size you have open on the sequence.

    ' Copy an AOI around tracking coordinates to new sequences.
    ' If you wish to resize the AOI, do so after tracking but before
    ' running the macro.
    Sub TrackAOI()
    	Dim aoiRect As RECT, newRect As RECT
    	Dim offX As Integer, offY As Integer
    	Dim shiftX As Integer, shiftY As Integer
    	Dim dPoints As Double, dTraces As Double
    	Dim dInfo As IPDOCINFO
    	Dim i As Long, j As Long
    	Dim trackPt() As Double
    	Dim outIm As Integer, source As Integer
    	Dim bApply As Long
    	' Get document information
    	ret = IpDocGet(DOCGET_GETACTDOC, 0, source)
    	ret = IpDocGet(DOCGET_GETDOCINFO, source, dInfo)
    	' Get tracking information
    	ret = IpTrackMeas(TMEAS_NUM_TRACKS_GET, 0, dTraces)
    	' Set up sequence apply
    	ret = IpSeqGet(SEQGET_APPLY, bApply)
    	ret = IpSeqSet(SEQSET_APPLY, 0)
    	For i=0 To dTraces-1
    		' Obtain the original AOI
    		ret = IpAoiGet(GETBOUNDS, 0, aoiRect)
    		offX = (aoiRect.Right-aoiRect.Left + 1) / 2
    		offY = (aoiRect.bottom-aoiRect.top + 1) / 2
    		' Create an array to hold the points
    		ret = IpTrackMeas(TMEAS_NUM_POINTS_GET, i, dPoints)
    		ReDim trackPt(0 To dPoints*2-1)
    		' Create an output image of appropriate size, type, frames, etc.
    		outIm = IpWsCreateEx(aoiRect.Right-aoiRect.Left+1, _
    			aoiRect.bottom-aoiRect.top+1, 300, dInfo.iClass, dPoints)
    		ret = IpAppSelectDoc(source)
    		' Get points
    		ret = IpTrackMeas(TMEAS_POINTS_GET, i, trackPt(0))
    		For j=0 To dPoints-1
    			' Find AOI offset
    			If j=0 Then
    				' Move original AOI to center on the track
    				shiftX = trackPt(0) - (aoiRect.Left + offX)
    				shiftY = trackPt(1) - (aoiRect.top + offY)
    				' Move using track delta
    				shiftX = trackPt(j*2) - trackPt(j*2-2)
    				shiftY = trackPt(j*2+1) - trackPt(j*2-1)
    			End If
    			' Move the AOI
    			ret = IpSeqPlay(j)
    			ret = IpAoiMove(shiftX, shiftY)
    			' Paste the AOI into the output sequence as desired
    			ret = IpSeqSet(SEQSET_APPLY, 0)
    			ret = IpWsCopy()
    			ret = IpAppSelectDoc(outIm)
    			ret = IpSeqSet(SEQSET_APPLY, 0)
    			ret = IpSeqPlay(j)
    			ret = IpWsPaste(0,0)
    			' Go back to the original
    			ret = IpAppSelectDoc(source)
    			ret = IpSeqSet(SEQSET_APPLY, 0)
    		Next j
    	Next i
    	' Reset the sequence play
    	ret = IpSeqSet(SEQSET_APPLY, bApply)
    End Sub

    IMPORTANT NOTE: I created this macro by recording the following steps:

    - Create an output sequence
    - Select the source (with an AOI already applied)
    - Turn off SEQ_APPLY
    - Copy
    - Select output
    - Turn off SEQ_APPLY
    - Select target frame
    - Paste
    - Go back to source, move AOI, repeat

    I then wrapped that sequence of operations with a couple of For loops, and used queries to the Tracking module to get the points.

Sign In or Register to comment.