Write in front
The following codes are all online excerpts, if I need the complete code, private!
hw1
hw1:
1. Image resize
Change the image memory size first, for example:
from X: 4 * 4 to Y: 7 * 7
Calculate the coordinate transformation relationship between two points:
aX + b = Y, find a = 4/7, b = -3/14
Interpolation method:
1) The nearest neighbor element method
is to assign the gray level of the neighboring pixel closest to the pixel to be determined to the pixel to be determined.
2) Triangular interpolation
Q = V1 A1 + V2 A2 + V3 * A3
3) Two-line interpolation method
Loop through pixels and map back to old coordinates
Use nearest neighbor interpolation to fill the image
2.
Find a moving picture explanation on CSDN:
the code on the Internet (c ++)
//******************高斯卷积核生成函数*************************
void GetGaussianKernel(double **gaus, const int size,const double sigma)
{
const double PI=4.0*atan(1.0); //圆周率π赋值
int center=size/2;
double sum=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
gaus[i][j]=(1/(2*PI*sigma*sigma))*exp(-((i-center)*(i-center)+(j-center)*(j-center))/(2*sigma*sigma));
sum+=gaus[i][j];
}
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
gaus[i][j]/=sum;
cout<<gaus[i][j]<<" ";
}
cout<<endl<<endl;
}
return ;
}
3. The mixed image (Gaussian convolution)
Gaussian filter is a low-pass filter.
You can get the high-pass filtered image by subtracting the Gaussian filtered image from the original image.
4. Sobel filter
Principle: Make the filter first, then normalize it
Netizen's code:
#竖直方向[1 2 1 水平方向[1 0 -1
# 0 0 0 2 0 -2
# -1 -2 -1 1 0 -1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,1),np.uint8)
for i in range(0,height-2):
for j in range(0,width-2):
gy = gray[i,j]+2*gray[i,j+1]+gray[i,j+2]-(gray[i+2,j]+gray[i+2,j+1]+gray[i+2,j+2])
gx = gray[i,j]+2*gray[i+1,j]+gray[i+2,j]-(gray[i,j+2]+gray[i+1,j+2]+gray[i+2,j+2])
grad = math.sqrt(gx*gx+gy*gy) #求梯度大小
if grad>=100:
dst[i,j] = 255
else:
dst[i,j] = 0
Mat sobel(Mat src,Mat dst)
{
int y, x;
int w = src.cols;
int h = src.rows;
int gx = 0, gy = 0;
for (y = 1; y < h - 1; y++)
{
for (x = 1; x < w - 1; x++)
{
gx=src.at<uchar>(y-1,x+1)+src.at<uchar>(y,x+1)*2+src.at<uchar>(y+1,x+1)-src.at<uchar>(y-1,x-1)-src.at<uchar>(y,x-1)*2-src.at<uchar>(y+1,x-1);
gy=src.at<uchar>(y-1,x-1)+src.at<uchar>(y-1,x)*2+src.at<uchar>(y-1,x+1)-src.at<uchar>(y+1,x-1)-src.at<uchar>(y+1,x)*2-src.at<uchar>(y+1,x+1);
dst.at<uchar>(y,x)= abs(gx)+abs(gy) ;
}
}
}