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

📄 image.cpp

📁 机器人处理中使用USB摄像机的例子程序.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					wTmp +=  *pW * mat[0][1];

					pW =  (WORD *) &Pixel[lig-1][t2];
					wTmp +=  *pW * mat[0][2];

					pW =  (WORD *) &Pixel[lig  ][t1];
					wTmp +=  *pW * mat[1][0];

					pW =  (WORD *) &Pixel[lig  ][index];
					wTmp +=  *pW * mat[1][1];

					pW =  (WORD *) &Pixel[lig  ][t2];
					wTmp +=  *pW * mat[1][2];

					pW =  (WORD *) &Pixel[lig+1][t1];
					wTmp +=  *pW * mat[2][0];
	
					pW =  (WORD *) &Pixel[lig+1][index];
					wTmp +=  *pW * mat[2][1];

					pW =  (WORD *) &Pixel[lig+1][t2];
					wTmp +=  *pW * mat[2][2];

					// Take the absolute value of the gradient
					wTmp = abs (wTmp); 
		
					// Update image for display
					dest->Pixel[lig][index++] = LOBYTE (wTmp);
					dest->Pixel[lig][index++] = LOBYTE (wTmp);
					dest->Pixel[lig][ index ] = LOBYTE (wTmp);

				}
			}
			break;
		}

		default :
		{
			if (fast)
			{
				for (lig = 1; lig < height; lig++)
				for (col = 1; col < width; col++)
				{
					t1 = (col-1)*bPerPixel+chan;
					t2 = (col+1)*bPerPixel+chan;
					wTmp  = -Pixel[lig-1][t1] 
						  +  Pixel[lig-1][t2] 
				  		  -2 * Pixel[lig  ][t1] 
						  +2 * Pixel[lig  ][t2] 
				 		  -  Pixel[lig+1][t1] 
						  +  Pixel[lig+1][t2];

					// Take the absolute value of the gradient
					wTmp = abs (wTmp); 
				}
			}
			else
			{	
				for (lig = 1; lig < height; lig++)
				for (col = 1; col < width; col++)
				{
					index = col * bPerPixel;
					t1 = (col-1)*bPerPixel+chan;
					t2 = (col+1)*bPerPixel+chan;
					t3 = index+chan;
					wTmp  = Pixel[lig-1][t1] * mat[0][0]
						  + Pixel[lig-1][t3] * mat[0][1]
						  + Pixel[lig-1][t2] * mat[0][2]
				  		  + Pixel[lig  ][t1] * mat[1][0]
						  + Pixel[lig  ][t3] * mat[1][1]
						  + Pixel[lig  ][t2] * mat[1][2]
				 		  + Pixel[lig+1][t1] * mat[2][0]
						  + Pixel[lig+1][t3] * mat[2][1]
						  + Pixel[lig+1][t2] * mat[2][2];

					// Take the absolute value of the gradient
					wTmp = abs (wTmp); 
		
					dest->Pixel[lig][index++] = LOBYTE (wTmp);
					dest->Pixel[lig][index++] = LOBYTE (wTmp);
					dest->Pixel[lig][ index ] = LOBYTE (wTmp);
				}
			}
			break;
		}
	}
}

void CImage::ExtractChannel(CImage *dest, short channel)
{
	int i,val;
	int index, PixSize;
	BYTE  b;
	WORD  *pw;

	PixSize = pHead->biWidth * pHead->biHeight;

	switch (channel)
	{
		case RED_CHANNEL :	
			for (i = 0; i < PixSize; i++)
			{
				index = i*bPerPixel;
				b = pBits[index+2];
				dest->pBits[index]   = b;
				dest->pBits[index+1] = b;
				dest->pBits[index+2] = b;
			}
			break;

		case GREEN_CHANNEL :
			for (i = 0; i < PixSize; i++)
			{
				index = i*bPerPixel;
				b = pBits[index+1];
				dest->pBits[index]   = b;
				dest->pBits[index+1] = b;
				dest->pBits[index+2] = b;
			}
			break;

		case BLUE_CHANNEL :
			for (i = 0; i < PixSize; i++)
			{
				index = i*bPerPixel;
				b = pBits[index];
				dest->pBits[index]   = b;
				dest->pBits[index+1] = b;
				dest->pBits[index+2] = b;
			}
			break;

		case GRAY_CHANNEL :
			for (i = 0; i < PixSize; i++)
			{
				index = i*bPerPixel;
				val = pBits[index]
					+ pBits[index+1] 
				    + pBits[index+2];
				b = (BYTE) (val/3);
				dest->pBits[index]   = b;
				dest->pBits[index+1] = b;
				dest->pBits[index+2] = b;
			}
			break;

		case (RG_CHANNEL):
			for (i = 0; i < PixSize; i++)
			{
				// Store the sum in the two low bytes
				index = i*bPerPixel;
				pw = (WORD*) (dest->pBits + index);
				*pw = pBits[index+1] + pBits[index+2];
			}
			break;

		case (RGB_CHANNEL) :
			for (i = 0; i < PixSize; i++)
			{
				// Store the sum in the two low bytes
				index = i*bPerPixel;
				pw = (WORD*) (dest->pBits + index);
				*pw = pBits[index] + pBits[index+1] + pBits[index+2];
			}
			break;

		default : AfxMessageBox ("This channel doesn't exists!"); break;
	}
}

void CImage::HSI_Values (CImage *dest)
{
        int i;
        int r, g, b,max, min, hue, saturation, temp;  // long !!!
		register int max_min, intensity; 

        for (i=0; i < (int) pHead->biSizeImage; i += bPerPixel)
        {
                b = pBits[i];
                g = pBits[i+1];
                r = pBits[i+2];

                max = MAX3(r,g,b);           /* compute Intensity */
                min = MIN3(r,g,b);
				max_min = max - min;
                intensity = (min + max) / 2;
                dest->pBits[i+2] = (unsigned char) intensity; // intensity in 3th byte 

                if (max == min)                         /* achromatic case */
                {
                        dest->pBits[i] = 0;      // hue is undefined
                        dest->pBits[i+1] = 0;    // saturation = 0
						continue;
                }
                else if (intensity < 20)        /* too dark (ADDED) */
                {
                        dest->pBits[i] = 0;      // hue is undefined
                        dest->pBits[i+1] = 0;    // saturation = 0
						continue;
                }
                else if (intensity > 235)       /* too bright (ADDED) */
                {
                        dest->pBits[i] = 0;      // hue is undefined
                        dest->pBits[i+1] = 0;    // saturation = 0
						continue;
                }
                else            /* chromatic case */
                {
                        if (intensity <= 127)   /* bottom cone (dark) */
                                saturation = (255 * (max_min)) / (max + min);
                        else    /* top cone (bright) */
                                saturation = (255 * (max_min)) / (512 - max - min);

                        dest->pBits[i+1] = (unsigned char) saturation; // saturation in 2th byte 

                        if (r == max)
                                hue = (60 *(g -b))/(max_min);
        /* color is between yellow and magenta */
                        else if (g == max)
                                hue = 60*2 + (60*(b-r))/(max_min);
        /* color is between cyan and yellow */
                        else
                                hue = 60*4 + (60*(r-g))/(max_min);      
        /* color is between magenta and cyan */
                        if (hue < 0) hue = hue + 360;
                        
                        temp = (255 * hue) / 360;
                        dest->pBits[i] = (unsigned char) temp; // hue in 1th byte 
                }
        }
}

void CImage::AfxHSI(int nCol, int nLine)
{
	CString str;
	BYTE H,S,I;
	GetPixelHSI (nCol, nLine, &H, &S, &I);
	str.Format ("Pixel x = %i y = %i\n\nH : %u\nS : %u\nI  : %u",nCol, nLine, H, S, I);
	AfxMessageBox (str);
}
	
void CImage::AfxRGB(int nCol, int nLine)
{
	CString str;
	BYTE R,G,B;
	GetPixelRGB (nCol, nLine, &R, &G, &B);
	str.Format ("Pixel x = %i y = %i\n\nR : %u\nG : %u\nB : %u",nCol, nLine, R, G, B);
	AfxMessageBox (str);
}

void CImage::TresholdImage(CImage *dest, short chan, short treshlow, short treshhigh, int *HueHist, short mode)
{	
	register BYTE val;
	register short thigh = treshhigh,tlow = treshlow;
	register int idx, idxchan, PixelSize;
	
	register int i;
	
	// Because all bytes have the same value
	if (chan == GRAY_CHANNEL) chan = GREEN_CHANNEL;

	PixelSize = pHead->biWidth * pHead->biHeight;

	if (mode == MODE_NORMAL)
	{
		for (i = 0; i < PixelSize; i++)
		{
			idx		= i * bPerPixel;
			idxchan = idx + chan;
		
			if ( (pBits[idxchan] >= tlow) && (pBits[idxchan] <= thigh) )
			{
				val = pBits[idxchan];
				dest->pBits[idx++]   = val;
				dest->pBits[idx++]   = val;
				dest->pBits[idx]     = val;
				continue;
			}
			else
			{
				dest->pBits[idx++]   = 0;
				dest->pBits[idx++]   = 0;
				dest->pBits[idx]     = 0;
			}
		}
	}
	else
	if (mode == MODE_LET_PIXEL_INFO_IN)
	{
		for (i = 0; i < PixelSize; i++)
		{
			idx		= i * bPerPixel;
			idxchan = idx + chan;
		
			if ( (pBits[idxchan] >= tlow) && (pBits[idxchan] <= thigh) )
			{
				dest->pBits[idx]   = pBits[idx];
				dest->pBits[idx+1] = pBits[idx+1];
				dest->pBits[idx+2] = pBits[idx+2];
				continue;
			}
			else
			{
				dest->pBits[idx++]   = 0;
				dest->pBits[idx++]   = 0;
				dest->pBits[idx]     = 0;
			}
		}
	}
	else
	if (mode == MODE_LET_PIXEL_INFO_IN_AND_HISTOGRAM)
	{
		// Purge histogram
		for (i = 0; i < MAX_HUE+1; i++) HueHist[i] = 0;

		for (i = 0; i < PixelSize; i++)
		{
			idx		= i * bPerPixel;
			idxchan = idx + chan;
		
			if ( (pBits[idxchan] >= tlow) && (pBits[idxchan] <= thigh) )
			{
				dest->pBits[idx]   = pBits[idx];
				dest->pBits[idx+1] = pBits[idx+1];
				dest->pBits[idx+2] = pBits[idx+2];
				HueHist[pBits[idx+HUE_CHANNEL]] ++;
				continue;
			}
			else
			{
				dest->pBits[idx++]   = 0;
				dest->pBits[idx++]   = 0;
				dest->pBits[idx]     = 0;
			}
		}
	}
	else
	if (mode == MODE_PASS_ZERO)
	{
		for (i = 0; i < PixelSize; i++)
		{
			idx		= i * bPerPixel;
			idxchan = idx + chan;
		
			if ( (pBits[idxchan] >= tlow) || (pBits[idxchan] <= thigh) )
			{
				val = pBits[idxchan];
				dest->pBits[idx++]  = val;
				dest->pBits[idx++]  = val;
				dest->pBits[idx]    = val;
				continue;
			}
			else
			{	
				dest->pBits[idx++]  = 0; 
				dest->pBits[idx++]  = 0;
				dest->pBits[idx]	= 0;
			}
		}
	}	
}

void CImage::ComputeColHistogram(int *hist, short middle, short range, short addmax)
{
	int		 maxi = -1;
	register int lig, col, idx;
	register short bPerPix = bPerPixel; 	
	register short dif;
	register float frange = float (range);
	register float slope = float (- addmax / range);

	for (int i = 0; i < pHead->biWidth; i++) hist[i] = 0;
	
	for (lig = 0; lig < pHead->biHeight; lig++)
		for (col = 0; col < pHead->biWidth; col++)
		{		
			idx = col*bPerPix;
			// Add only if saturation != 0
			if (Pixel[lig][idx+SAT_CHANNEL] != 0) 
			{
				dif = abs (middle - Pixel[lig][idx+HUE_CHANNEL]);
				if (dif > range) dif = 256 - dif;
				hist[col] += int (slope * dif + addmax);
			}	
		}
}

⌨️ 快捷键说明

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