Target edge detection in image processing

Target edge detection in image processing

experiment analysis

Insert picture description here

The digital part of the image is detected by the edge of the sobel operator and converted into an integral image to find the target area.
My idea is to perform simple binarization after edge detection to highlight the digital part, then highlight the target area in the integral map, find the coordinates of the upper left corner of the digital area, and draw a rectangle from the upper left to the lower right.

Sobel operator for edge detection

The Soble operator edge detection uses the SobelGrayImage function;
because I found that there are lighter white points next to the numbers, I will perform a simple binarization afterwards:

	void bin(BYTE *pSrc, int width, int height, BYTE *pDst)
	{
    
    
		for(int i=0;i<height;i++)
			for (int j = 0; j < width; j++)
			{
    
    
				pDst[i*width + j] = (pSrc[i*width + j]> 200 ? pSrc[i*width + j] : 0);
			}
		return;
}

Then calculate the integral map:

	void integrogram(BYTE* inputMatrix, int width, int height, BYTE* outputMatrix) 
	{
    
    
		BYTE *columnSum = new BYTE[width];
		for (int i = 0; i < width; i++) 
		{
    
    
			columnSum[i] = inputMatrix[i];
			outputMatrix[i] = inputMatrix[i];
			if (i > 0) 
			{
    
    
				outputMatrix[i] += outputMatrix[i - 1];
			}
		}
		for (int i = 1; i < height; i++) 
		{
    
    
			int offset = i * width;
			columnSum[0] += inputMatrix[offset];
			outputMatrix[offset] = columnSum[0];
			for (int j = 1; j < width; j++) 
			{
    
    
				columnSum[j] +=(255, inputMatrix[offset + j]);
				outputMatrix[offset + j] = min(255,outputMatrix[offset + j - 1] + columnSum[j]);
			//全部变成255;
			}
		}
	return;
}

At this time, there will be an obvious target area:

Insert picture description here

Get the coordinates of the first white pixel in the upper left corner:

void findROIxy(BYTE *pSrc,int width,int height,int *x,int *y)
	{
    
    
		for (int i = 0; i < height; i++)
			for(int j=0;j<width;j++)
			{
    
    
				if (pSrc[i*width + j] != 0)
				{
    
    
					*x = j;
					*y = i;
					return;
				}
			}
}

Draw a rectangle from top left to bottom right;

void drawRectangle(BYTE *pSrc, int x, int y, int width,int height,int draw_width, 	int draw_height,BYTE *pDst)
	{
    
    
		for (int i = x - 10; i < x + draw_width; i++)
		{
    
    
			pSrc[y*width + i] = 255;
			pSrc[(y + draw_height)*width + i] = 255;
		}
		for (int i = y; i < y + draw_height; i++)
		{
    
    
			pSrc[i*width+x-10] = 255;
			pSrc[i*width + x + draw_width] = 255;
		}
		
}

result

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

It takes about 10ms for each run in Debug mode, and about 4ms in Release mode;

This process is not very stable, because it is just a simple binarization. If you encounter a point that is similar to the pixel value of the target area, then the rectangular frame will deviate very much, and this approach has certain limitations, because I found it absolutely The size of the digital area of ​​most pictures is the same, so I defaulted that the size of this area is fixed, so it won’t work if you put it in other questions, you need to modify the parameters, there should be a more formal approach.

Guess you like

Origin blog.csdn.net/qq_36587495/article/details/108164957