Simple image processing III based on easyx (C language)

  This article mainly talks about the edge processing of the image. I have already explained the graying and binarization of the image. In the image processing, there is also the edge processing of the image. Here I use various operators to process the image , The reader can see the effect of these operators.

Before writing the code, I referred to this information. In fact, there are many online and detailed descriptions of the principle of image marginalization, so I wo n’t go into details here.

https://blog.csdn.net/guanyuqiu/article/details/52993412

First give the program using the sobel operator:

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int Gray[1500][1000];
int result[1500][1000];
IMAGE img1;
int imagHight,imagWidth;
int main()
{
	
	int i,j,Gx,Gy,nSum;
	// 读取图片至绘图窗口
	loadimage(&img1, _T("D:\\testh.bmp"));
	imagHight = img1.getheight();
	imagWidth = img1.getwidth();
	initgraph(imagWidth,imagHight, SHOWCONSOLE|NOCLOSE|NOMINIMIZE);
    putimage(0, 0, &img1);
	_getch();
	DWORD* pMem = GetImageBuffer();

	//1.图像灰度化
	for(i = 0; i <imagHight; i++)
	{
		for(j=0;j<imagWidth;j++)
	  {
	   *pMem = BGR(*pMem);
	   Gray[i][j]  = (GetRValue(*pMem)*299+GetGValue(*pMem)*587+GetBValue(*pMem)*114+500)/1000;  
	  *pMem = RGB(Gray[i][j],Gray[i][j],Gray[i][j]);
	   pMem++;
		}
	}
	   pMem-=imagHight*imagWidth;;

    //2.对图像利用算子处理
	for(i =1; i <imagHight-1; i++)
	{
		for(j=1;j<imagWidth-1;j++)
	  {
		  //sobel算子
	     Gx = (-1)*Gray[i-1][j-1]+Gray[i+1][j-1]-2*Gray[i-1][j]+2*Gray[i+1][j]-Gray[i-1][j+1]+Gray[i+1][j+1];
		 Gy = Gray[i-1][j-1] +2*Gray[i][j-1]+Gray[i+1][j-1]-Gray[i-1][j+1]-2*Gray[i][j+1]-Gray[i+1][j+1]; 
		 nSum = abs(Gx)+abs(Gy);
		 if( nSum>175)
		   result[i][j] = 255;
		 else
           result[i][j] = 0;
	  }
	}

	//3.将处理后的图像显示出来
	for(i = 0; i <imagHight; i++)
	{
		for(j=0;j<imagWidth;j++)
	  {
	      *pMem = RGB(result[i][j],result[i][j],result[i][j]);
	      pMem++;
		}
	}
	FlushBatchDraw();
	// 按任意键退出
	_getch();
	closegraph();
}

Here is the rendering of the sobel operator:

To use different operators here, just change the following code:

 

 //sobel算子
	     Gx = (-1)*Gray[i-1][j-1]+Gray[i+1][j-1]-2*Gray[i-1][j]+2*Gray[i+1][j]-Gray[i-1][j+1]+Gray[i+1][j+1];
		 Gy = Gray[i-1][j-1] +2*Gray[i][j-1]+Gray[i+1][j-1]-Gray[i-1][j+1]-2*Gray[i][j+1]-Gray[i+1][j+1]; 
		 nSum = abs(Gx)+abs(Gy);
		 if( nSum>175)
		   result[i][j] = 255;
		 else
           result[i][j] = 0;

To:

        //Robert算子
	nSum = abs(Gray[i+1][j+1]-Gray[i][j])+abs(Gray[i+1][j]-Gray[i][j+1]);
	if( nSum>175)
	 result[i][j] = 255;
	else
         result[i][j] = 0;

Effect picture of Robert operator:

The effect of marginalization here is not very good, because of the problem of threshold. Different operators process different thresholds. Here, the threshold is changed from 175 to 50. Effect picture:

The Laplace algorithm is used here:

//拉普拉斯算子
 nSum = 4*Gray[i][j] - Gray[i+1][j]-Gray[i][j-1]-Gray[i][j+1]-Gray[i-1][j];
 if( nSum>35)
   result[i][j] = 255;
 else
   result[i][j] = 0;

Effect picture:

 

If you are interested, you can try other operators, and you can adjust the threshold.

Published 7 original articles · won 13 · views 2095

Guess you like

Origin blog.csdn.net/qq_39036834/article/details/97898219