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

📄 imgproc.cpp

📁 1394 接口视觉工具箱 (英文工具箱
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			
			/* get list of colours */
			if(!mxIsEmpty(prhs[2])) {
				
				if(!mxIsNumeric(prhs[2]) && !mxIsComplex(prhs[2]))
					mexErrMsgTxt("The specified colour list has to be a vector with up to 16 colour IDs\n");
				
				maxCOL = (unsigned int)mxGetNumberOfElements(prhs[2]);
				if(maxCOL > CMV_MAX_COLORS) {
					mexWarnMsgTxt("Too many colours specified. Reducing to maximum permitted (32)\n");
					maxCOL = CMV_MAX_COLORS;
				}
				
				// read colour vector
				pfout = mxGetPr(prhs[2]);
				for(fi=0; fi<maxCOL; fi++) {
					
					COLIDs[fi] = (unsigned int)(*(pfout+fi))-1;
					
					if((COLIDs[fi] < 0) || (COLIDs[fi] >= CMV_MAX_COLORS))
						mexErrMsgTxt("Colour(s) outside the valid range (1 ... 32).\n");
				}
				
			}
			
		}
		
	}
	
	
	/* determine image size */
	inDims      = mxGetDimensions(prhs[0]);
	frameHeight = (unsigned long)inDims[0];		// rows
	frameWidth  = (unsigned long)inDims[1];		// columns
	
	/* ensure minimum width and height (to avoid crashes) */
	if(frameWidth < 2 || frameHeight < 1) {

		// input image size is too small
		mexErrMsgTxt("Size of provided RGB image is too small (minimum: 2 x 1 pixels).\n");

	}


	/* vision algorithms are yuyv based  --  won't work if 'frameWidth' is an odd number 
	   (need 2 RGB pixels to form one yuyv pixel)  ->  adjust width, if required  */
	if((frameWidth%2) == 1) {

		mexPrintf("Reducing width by one pixel to make frameWidth become an even number\n");
		frameWidth--;

	}
	
	//mexPrintf("frameWidth  = %d\n", frameWidth);
	//mexPrintf("frameHeight = %d\n", frameHeight);
	
	/* allocate necessary memory... */
	mySetupVision(frameWidth, frameHeight);
	
	// convert RGB image to YUYV equivalent
	convRGB_2_YUYV(yuyvBuf, MATbuf);
	//mexPrintf("RGB image converted to yuyv.\n");
	
	// process image data
	//mexPrintf("before process.\n");
	vision.processFrame(yuyvBuf);
	//mexPrintf("after process.\n");
	
	{
		unsigned int	row, col;
		mxArray			*FGretIMG;					/* optional display of image data -> need to create workspace variable */
		
		//mexPrintf("Before 'create FGretIMG'...\n");
		/* create numeric array FGretIMG (used to display the image) */
		dims[0] = frameHeight;
		dims[1] = frameWidth;
		dims[2] = 3;
		
		/* create a MATLAB array with 'iWidth' columns, 'iHeight' rows, 'real numbers'	*/
		FGretIMG = mxCreateNumericArray(NDIMS, (int const *)&dims[0], mxUINT8_CLASS, mxREAL);
		//mexPrintf("After 'create FGretIMG'.\n");
		
		/* ensure that MATLAB doesn't free data associated with FGretIMG (between subsequent calls to mdl-functions) */
		//mexMakeArrayPersistent(FGretIMG);		/* IMPORTANT!! (for Simulink only, FW-03-03) */
		
		/* get pointer to data area, can reuse pointer variable MATbuf here (input data has already been copied to yuyv buffer...) */
		MATbuf = (unsigned char *)mxGetData(FGretIMG);
		
		{
			
			//char	myEvalStr[30];
			
			// convert raw yuv422 image to RGB
			convYUYV_2_RGB(img, (yuv422 *)yuyvBuf);
			//mexPrintf("image converted to RGB\n");
			
			// copy RGB to the MATLAB buffer...
			for(row=0; row<frameHeight; row++) {
				for(col=0; col<frameWidth; col++) {
					
					MATbuf[row+col*frameHeight+0*frameWidth*frameHeight] = img[row*frameWidth+col].red;
					MATbuf[row+col*frameHeight+1*frameWidth*frameHeight] = img[row*frameWidth+col].green;
					MATbuf[row+col*frameHeight+2*frameWidth*frameHeight] = img[row*frameWidth+col].blue;
					
				}
			}
			
			/* clear current axis (they remain held...) */
			// IMPORTANT!!! (otherwise the figure keeps filling... everythg slows down with every frame!  (FW-03-03)
			mexEvalString("cla");
			
			/* call MATLAB to display the contents of (persistent) variable FGretIMG */
			mexCallMATLAB(0, NULL, 1, &FGretIMG, "image");
			
			// use fixed 'image' axes
			mexEvalString("axis image");
			//mexEvalString("axis image ij;");					/* use this one if the picture is upside down... */

			// set image dimensions to (frameWidth x frameHeight)
			//sprintf(myEvalStr, "axis([0 %d 0 %d]);", frameWidth, frameHeight);
			//mexEvalString(myEvalStr);
			
			// put axis on hold...
			//mexEvalString("hold on;");
			//mexEvalString("set(gca, 'NextPlot', 'add');");	/* equivalent */

		}

		
	}
	
	
	// reset 'detected regions' list
	numCOL = 0;
	for(i=0; i<maxCOL; i++)  COLdetected[i] = -1;
	
	// scan regions with specified colour IDs
	for(i=0; i<maxCOL; i++) {
		
		reg = vision.getRegions(COLIDs[i]);
		
		// allocate memory to store data
		retREG[i] = (struct myREG *)mxCalloc(1, sizeof(struct myREG));
		
		
		// initialize structure
		retREG[i]->colourID = COLIDs[i];
		retREG[i]->colour = vision.getColorVisual(COLIDs[i]);
		mexPrintf("-------------------------------\n");
		mexPrintf("R|G|B: %3d|%3d|%3d\n", retREG[i]->colour.red,
			retREG[i]->colour.green,
			retREG[i]->colour.blue);
		
		numREG = 0;
		while(reg && reg->area>minAREA && numREG<maxREG) {
			
			retREG[i]->cen_x[numREG] = (unsigned int)reg->cen_x;
			retREG[i]->cen_y[numREG] = (unsigned int)reg->cen_y;
			retREG[i]->x1[numREG]    = (unsigned int)reg->x1;
			retREG[i]->y1[numREG]    = (unsigned int)reg->y1;
			retREG[i]->x2[numREG]    = (unsigned int)reg->x2;
			retREG[i]->y2[numREG]    = (unsigned int)reg->y2;
			
			mexPrintf("Area of region %d: %d\n", numREG, reg->area);
			mexPrintf("X1|Y1|X2|Y2: %3d|%3d|%3d|%3d\n", retREG[i]->x1[numREG], retREG[i]->y1[numREG],
				retREG[i]->x2[numREG], retREG[i]->y2[numREG]);
			
			retREG[i]->nREG = ++numREG;
			reg = reg->next;
			
		}
		
		// check if any regions were detected for this colour
		if(numREG) {
			// valid colour... (found at least one valid region)
			COLdetected[numCOL++] = i;      // corresponding colour ID (loop variable)
		}		
		
	}
	
	mexPrintf("-------------------------------\n");
	
	
	/* destroy vision object & free memory */
	mySetupVision(0, 0);
	
	
	
	
	/*************************************************************************/
	/* return regional information in form of a structure array		 */
	/*************************************************************************/
	if((pArray = mxCreateStructMatrix(maxCOL, 1, 4, fnames)) == NULL)
		mexErrMsgTxt("Failure assigning stucture memory\n");
	
	
	// update 'ColourID' field...
	for (i=0; i<numCOL; i++)		// ... in all elements of the structure
	{
		// get index of the detected colour
		j = COLdetected[i];
		
		// create mxArray to store the colour information
		fout = mxCreateDoubleMatrix(1,1,mxREAL);		// colourID
		pfout = mxGetPr(fout);				// beginning of data area
		
		//printf("Updating colourID (%d)\n", j);
		// copy data
		*(pfout  ) =  (double)(retREG[j]->colourID);
		
		// update structure entry (field index: 0)
		mxSetFieldByNumber(pArray,i,0,fout);
	}
	//printf("Updated colourID\n");
	
	
	// update 'Colour' field...
	for (i=0; i<numCOL; i++)		// ... in all elements of the structure
	{
		// get index of the detected colour
		j = COLdetected[i];
		
		// create mxArray to store the colour information
		fout = mxCreateDoubleMatrix(1,3,mxREAL);		// R, G, B
		pfout = mxGetPr(fout);				// beginning of data area
		
		// copy data
		*(pfout  ) =  (double)(retREG[j]->colour.red)/255.0;
		*(pfout+1) =  (double)(retREG[j]->colour.green)/255.0;
		*(pfout+2) =  (double)(retREG[j]->colour.blue)/255.0;
		
		// update structure entry (field index: 0)
		mxSetFieldByNumber(pArray,i,1,fout);
	}
	//printf("Updated colour\n");
	
	// update 'nRegions' field...
	for (i=0; i<numCOL; i++)		// ... in all elements of the structure
	{
		// get index of the detected colour
		j = COLdetected[i];
		
		// create mxArray to store the number of valid regions found
		fout = mxCreateDoubleMatrix(1,1,mxREAL);
		pfout = mxGetPr(fout);				// beginning of data area
		
		// copy data
		*(pfout  ) =  (double)(retREG[j]->nREG);
		
		// update structure entry (field index: 1)
		mxSetFieldByNumber(pArray,i,2,fout);
	}
	//printf("Updated nRegions\n");
	
	
	// update 'Regions' field...
	for (i=0; i<numCOL; i++)		// ... in all elements of the structure
	{
		// get index of the detected colour
		j = COLdetected[i];
		
		// create mxArray to store the colour information
		numREG = (unsigned int)(retREG[j]->nREG);
		
		if(numREG)
		{
			//printf(">> In numREG %d\n", i);
			//printf(">> numREG = %d\n", numREG);
			*((int*)foutdims) = 6;						// cen_x, cen_y, x, y, w, h
            foutdims[1] = (int)numREG;					// valid regions
			fout = mxCreateNumericArray(2, foutdims, mxDOUBLE_CLASS, mxREAL);
			pfout = mxGetPr(fout);						// beginning of data area
			
			// copy data
			for(fi=0; fi<numREG; fi++)
			{
				*(pfout+fi*6  ) =  (double)(retREG[j]->cen_x[fi]);
				*(pfout+fi*6+1) =  (double)(retREG[j]->cen_y[fi]);
				*(pfout+fi*6+2) =  (double)(retREG[j]->x1[fi]);
				*(pfout+fi*6+3) =  (double)(retREG[j]->y1[fi]);
				*(pfout+fi*6+4) =  (double)(retREG[j]->x2[fi]);
				*(pfout+fi*6+5) =  (double)(retREG[j]->y2[fi]);
				
				// update structure entry (field index: 2)
				mxSetFieldByNumber(pArray,i,3,fout);
			}
		}
	}
	//printf("Updated Regions\n");
	
	// free dynamically allocated memory
	for(i=0; i<maxCOL; i++)	mxFree(retREG[i]);
	
	// return pointer
	plhs[0] = pArray;
	
}

⌨️ 快捷键说明

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