⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 visionapi.cpp

📁 good luck to everyone!
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/********************************************************************************
*  Project   		: FIRST Motor Controller
*  File Name  		: VisionAPI.cpp        
*  Contributors   	: ELF, EMF
*  Creation Date 	: June 26, 2008
*  Revision History	: Source code & revision history maintained at sourceforge.WPI.edu   
*  File Description	: C Routines for FIRST Vision API. Open source API developed 
*    by BAE Systems to interface with the National Instruments vision C library
*    in the nivision.out module. The published interface to nivision.out is in
*    the header file nivision.h and documented in the NIVisionCVI.chm help file.
*/ 
/*----------------------------------------------------------------------------*/
/*        Copyright (c) FIRST 2008.  All Rights Reserved.                     */
/*  Open Source Software - may be modified and shared by FRC teams. The code  */
/*  must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/

#include "stdioLib.h" 
#include "vxWorks.h" 

#include "BaeUtilities.h"
#include "FrcError.h"
#include "VisionAPI.h" 

int VisionAPI_debugFlag = 1;
#define DPRINTF if(VisionAPI_debugFlag)dprintf

/*   Image Management functions    */

/**
* @brief Create an image object
* Supports IMAQ_IMAGE_U8, IMAQ_IMAGE_I16, IMAQ_IMAGE_SGL, IMAQ_IMAGE_COMPLEX, IMAQ_IMAGE_RGB, IMAQ_IMAGE_HSL, IMAQ_IMAGE_RGB_U64          
* The border size is defaulted to 3 so that convolutional algorithms work at the edges. 
* When you are finished with the created image, dispose of it by calling frcDispose(). 
* To get extended error information, call GetLastError().   
*           
* @param type Type of image to create
* @return Image* On success, this function returns the created image. On failure, it returns NULL.
*/
Image* frcCreateImage(ImageType type) { return imaqCreateImage(type, DEFAULT_BORDER_SIZE); }

/**
* @brief Dispose of one object. Supports any object created on the heap.
* 
* @param object object to dispose of
* @return On success: 1. On failure: 0. To get extended error information, call GetLastError().
*/
int frcDispose(void* object)  { return imaqDispose(object);	}
/**
* @brief Dispose of a list of objects. Supports any object created on the heap.
* 
* @param functionName The name of the function
* @param ... A list of pointers to structures that need to be disposed of. 
* The last pointer in the list should always be set to NULL.
* 
* @return On success: 1. On failure: 0. To get extended error information, call GetLastError().
*/
int frcDispose( const char* functionName, ... ) /* Variable argument list */
{
    va_list disposalPtrList;   /* Input argument list */
    void* disposalPtr;         /* For iteration */
    int success, returnValue = 1;
    
    va_start( disposalPtrList, functionName );  /* start of variable list */
    disposalPtr = va_arg( disposalPtrList, void* );
    while( disposalPtr != NULL )     {
    	success = imaqDispose(disposalPtr);
        if (!success) {returnValue = 0;}
        disposalPtr = va_arg( disposalPtrList, void* );
    }
    return returnValue;
}

/**
* @brief Copy an image object. 
* Supports IMAQ_IMAGE_U8, IMAQ_IMAGE_I16, IMAQ_IMAGE_SGL, IMAQ_IMAGE_RGB, IMAQ_IMAGE_HSL.          
* 
* @param dest Copy of image. On failure, dest is NULL. Must have already been created using frcCreateImage().
* When you are finished with the created image, dispose of it by calling frcDispose().
* @param source Image to copy      
* 
* @return On success: 1. On failure: 0. To get extended error information, call GetLastError().
*/
int frcCopyImage(Image* dest, const Image* source) { return imaqDuplicate(dest, source);  }

/**
* @brief Crop image without changing the scale. 
* Supports IMAQ_IMAGE_U8, IMAQ_IMAGE_I16, IMAQ_IMAGE_SGL, IMAQ_IMAGE_RGB, IMAQ_IMAGE_HSL.
* 
* @param dest Modified image
* @param source Image to crop
* @param rect region to process, or IMAQ_NO_RECT
* 
* @return On success: 1. On failure: 0. To get extended error information, call GetLastError().
*/
int frcCrop(Image* dest, const Image* source, Rect rect)
{	
	return imaqScale(dest, source, 1, 1, IMAQ_SCALE_LARGER, rect);
}


/**
* @brief Scales the entire image larger or smaller. 
* Supports IMAQ_IMAGE_U8, IMAQ_IMAGE_I16, IMAQ_IMAGE_SGL, IMAQ_IMAGE_RGB, IMAQ_IMAGE_HSL.
*     
* @param dest Modified image
* @param source Image to scale
* @param xScale the horizontal reduction ratio
* @param yScale the vertical reduction ratio 
* @param scaleMode IMAQ_SCALE_LARGER or IMAQ_SCALE_SMALLER  
* 
* @return On success: 1. On failure: 0. To get extended error information, call GetLastError().
*/
int frcScale(Image* dest, const Image* source, int xScale, int yScale, ScalingMode scaleMode)
{	
	Rect rect = IMAQ_NO_RECT;
	return imaqScale(dest, source, xScale, yScale, scaleMode, rect);	
}
  
/**
 * @brief Creates image object from the information in a file. The file can be in one of the following formats: 
 * PNG, JPEG, JPEG2000, TIFF, AIPD, or BMP. 
 * Supports IMAQ_IMAGE_U8, IMAQ_IMAGE_I16, IMAQ_IMAGE_SGL, IMAQ_IMAGE_COMPLEX, IMAQ_IMAGE_RGB, IMAQ_IMAGE_HSL, IMAQ_IMAGE_RGB_U64.
 * 
 * @param image Image read in
 * @param fileName File to read. Cannot be NULL
 * 
 * @return On success: 1. On failure: 0. To get extended error information, call GetLastError().
 */
 int frcReadImage(Image* image, const char* fileName)
 {
	 return imaqReadFile(image, fileName, NULL, NULL);
 }


 /**
 * @brief Write image to a file. 
 * Supports IMAQ_IMAGE_U8, IMAQ_IMAGE_I16, IMAQ_IMAGE_SGL, IMAQ_IMAGE_COMPLEX, IMAQ_IMAGE_RGB, IMAQ_IMAGE_HSL, IMAQ_IMAGE_RGB_U64.
 * 
 * The file type is determined by the extension, as follows:
 *
 *		Extension 					File Type 
 *		aipd or .apd 				AIPD 
 *		.bmp 						BMP 
 *		.jpg or .jpeg 				JPEG 
 *		.jp2 						JPEG2000 
 *		.png 						PNG 
 *		.tif or .tiff 				TIFF 
 * 
 * 
 *The following are the supported image types for each file type:
 *
 *		File Types 					Image Types 
 *		AIPD 						all image types 
 *		BMP, JPEG 					8-bit, RGB 
 *		PNG, TIFF, JPEG2000 		8-bit, 16-bit, RGB, RGBU64 		
 * 			    
 * @param image Image to write
 * @param fileName File to read. Cannot be NULL. The extension determines the file format that is written.
 * 
 * @return On success: 1. On failure: 0. To get extended error information, call GetLastError().
 */
int frcWriteImage(const Image* image, const char* fileName)
{	
	RGBValue* colorTable = NULL;
	return imaqWriteFile(image, fileName, colorTable);
}


/*  Measure Intensity functions */

/**
* @brief Measures the pixel intensities in a rectangle of an image. 
* Outputs intensity based statistics about an image such as Max, Min, Mean and Std Dev of pixel value. 
* Supports IMAQ_IMAGE_U8, IMAQ_IMAGE_I16, IMAQ_IMAGE_SGL.
*
*  Parameter Discussion	: 
*		Relevant parameters of the HistogramReport include:
* 			min, max, mean and stdDev      
*		min/max 桽etting both min and max to 0 causes the function to set min to 0 
* 			and the max to 255 for 8-bit images and to the actual minimum value and
* 			maximum value of the image for all other image types.
*		max桽etting both min and max to 0 causes the function to set max to 255 
* 			for 8-bit images and to the actual maximum value of the image for 
* 			all other image types.
*
* @param image Image whose histogram the function calculates.
* @param numClasses The number of classes into which the function separates the pixels. 
* Determines the number of elements in the histogram array returned
* @param min The minimum pixel value to consider for the histogram. 
* The function does not count pixels with values less than min.
* @param max The maximum pixel value to consider for the histogram. 
* The function does not count pixels with values greater than max.
* @param rect Region of interest in the image. If not included, the entire image is used. 
* @return On success, this function returns a report describing the pixel value classification.    
* When you are finished with the report, dispose of it by calling frcDispose(). 
* On failure, this function returns NULL. To get extended error information, call GetLastError().            
* 
*/
HistogramReport* frcHistogram(const Image* image, int numClasses, float min, float max)
{
	Rect rect = IMAQ_NO_RECT;
	return frcHistogram(image, numClasses, min, max, rect);		
}
HistogramReport* frcHistogram(const Image* image, int numClasses, float min, float max, Rect rect)
{
	int success; 
	int fillValue = 1;
	
	/* create the region of interest */
	ROI* pRoi = imaqCreateROI();
	success = imaqAddRectContour(pRoi, rect); 
	if ( !success )	{ GetLastVisionError(); return NULL; }	

	/* make a mask from the ROI */
	Image* pMask = frcCreateImage(IMAQ_IMAGE_U8);
	success = imaqROIToMask(pMask, pRoi, fillValue, NULL, NULL);
	if ( !success )	{ 
		GetLastVisionError(); 
		frcDispose(__FUNCTION__, pRoi, NULL); 
		return NULL; 
	}	
	
	/* get a histogram report */
	HistogramReport* pHr = NULL;
	pHr = imaqHistogram(image, numClasses, min, max, pMask); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -