Last active
December 15, 2015 06:59
-
-
Save hyounggyu/5219748 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Runtime.InteropServices; | |
| using NationalInstruments.Vision; | |
| using NationalInstruments.Vision.Acquisition.Imaq; | |
| namespace StroboR3 | |
| { | |
| class Acquisition | |
| { | |
| private ImaqSession _session = null; | |
| private ImaqBufferCollection bufList; | |
| private PixelValue2D subPixels; | |
| private int[] dims; | |
| private string interfaceName; | |
| private uint bufferNumber; | |
| private int numberOfImages; // buffer size | |
| private int imageNumPerSet; // z axis of imageData | |
| public Acquisition() | |
| { | |
| // Initialize | |
| dims = new int[] { 640, 480, 40 }; | |
| interfaceName = "img0"; | |
| numberOfImages = 5; | |
| bufferNumber = 1; // start from 1, see ImaqAcquisition.Extract help page | |
| imageNumPerSet = 0; | |
| // Create a session. | |
| _session = new ImaqSession(interfaceName); | |
| subPixels = new PixelValue2D(new byte[dims[1], dims[0]]); | |
| } | |
| public void Setup() | |
| { | |
| // Create a buffer collection for the acquisition with the requested | |
| // number of images, and configure the buffers to loop continuously. | |
| bufList = _session.CreateBufferCollection(numberOfImages, ImaqBufferCollectionType.PixelValue2D); | |
| for (int i = 0; i < bufList.Count; ++i) | |
| { | |
| bufList[i].Command = (i == bufList.Count - 1) ? ImaqBufferCommand.Loop : ImaqBufferCommand.Next; | |
| } | |
| // Configure and start the acquisition. | |
| _session.Acquisition.Configure(bufList); | |
| _session.Acquisition.AcquireAsync(); | |
| } | |
| public void Run(VisionImage curImage, VisionImage subImage) | |
| { | |
| uint curBufferNumber, preBufferNumber; | |
| PixelValue2D curPixels = _session.Acquisition.Extract(bufferNumber, out curBufferNumber).ToPixelArray(); | |
| PixelValue2D prePixels = _session.Acquisition.Extract(bufferNumber-1, out preBufferNumber).ToPixelArray(); | |
| // Check curBufferNumber and preBufferNumber? | |
| // Subtraction and Save | |
| float[] pixel = new float[] { 0.0f }; | |
| IntPtr pPixel; | |
| int tmp; | |
| for (int y = 0; y < dims[1]; y++) | |
| { | |
| for (int x = 0; x < dims[0]; x++) | |
| { | |
| tmp = (int)curPixels.U8[y, x] - (int)prePixels.U8[y, x]; | |
| tmp = (tmp < 0) ? -tmp : tmp; | |
| // Set subPixels to display subtraction image | |
| subPixels.U8[y, x] = (byte)tmp; | |
| } | |
| } | |
| imageNumPerSet++; | |
| // Display | |
| curImage.ArrayToImage(curPixels); | |
| subImage.ArrayToImage(subPixels); | |
| } | |
| public void Stop() | |
| { | |
| _session.Stop(); | |
| } | |
| public void Close() | |
| { | |
| _session.Close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment