当前位置:网站首页>Opencv_ 100 questions_ Chapter II (6-10)
Opencv_ 100 questions_ Chapter II (6-10)
2022-06-10 22:43:00 【Fioman_ Hammer】
List of articles
6. Subtractive treatment
Subtraction is the process of subtracting a picture 256 ^ 3 A hue becomes 4 ^ 3 A hue , That is to say, every original channel 256 The color levels are divided into four sections and compressed to 4 Color levels , These four gray levels are given . Namely 32, 96, 160,224.
pix = 32 (0 <= pix < 64)
pix = 96 (64 <= pix < 128)
pix = 160 (128 <= pix < 192)
pix = 224 (192 <= pix < 256)
You can get a formula that is
pix = (int)(pix/64)*64 + 32
Code section :
# @Time : 2022/6/9 13:46
# @Author : Fioman
# @Phone : 13149920693
# @Tips : Talk is Cheap,Show me the code! ^_^^_^
from settings import *
# Dicrease color
def dicrease_color(image):
""" The image is subtracted :param image: :return: """
out = image.copy()
out = out // 64 * 64 + 32
return out
if __name__ == '__main__':
imagePath = os.path.join(OPENCV_100_Q_PATH,"gray_01.bmp")
imageOriginal = cv.imread(imagePath,cv.IMREAD_COLOR)
imageDicrease = dicrease_color(imageOriginal)
cv.imshow("ImageOriginal",imageOriginal)
cv.imshow("ImageDicrease",imageDicrease)
cv.waitKey(0)
7. The average pooling (Average Pool)
Divide the image into fixed size grids , The pixel value in the grid is the average value of all pixels in the grid .
We will use the same size grid to divide the image , The operation of finding the representative values in the grid is called pooling (Pooling).
Pool operation isConvolutional neural networks (Convolutional Neural Network)The important image processing method in . Average pooling is defined as follows :

Please put the size 128 * 128 Use the picture of 8 * 8 The grid is pooled evenly , Average pooling is to change all pixel values in the region to draw values
# @Time : 2022/6/9 14:10
# @Author : Fioman
# @Phone : 13149920693
# @Tips : Talk is Cheap,Show me the code! ^_^^_^
from settings import *
def average_pooling(image, G=8):
out = image.copy()
if len(image.shape) == 3:
H, W, C = image.shape
else:
H, W = image.shape[:2]
C = []
newW = int(W / G)
newH = int(H / G)
for y in range(newH):
for x in range(newW):
if C != []:
for c in range(C):
out[G * y:G * (y + 1), G * x:G * (x + 1), c] =
np.mean(out[G * y, G * (y + 1), G * x:G * (x + 1), c]).astype(np.uint8)
else:
out[G * y:G * (y + 1), G * x:G * (x + 1)] =
np.mean(out[G * y:G * (y + 1), G * x:G * (x + 1)]).astype(np.uint8)
return out
if __name__ == '__main__':
imagePath = os.path.join(OPENCV_100_Q_PATH, "gray_01.bmp")
imageOriginal = cv.imread(imagePath, cv.IMREAD_GRAYSCALE)
averPoolImage = average_pooling(imageOriginal, 8)
cv.imshow("Original", imageOriginal)
cv.imshow("AverPoolImage", averPoolImage)
cv.waitKey(0)
8. Maximum pooling (Max Pooling)
The values in the grid are not averaged , Instead, the maximum value in the grid is taken for pooling
# @Time : 2022/6/9 15:56
# @Author : Fioman
# @Phone : 13149920693
# @Tips : Talk is Cheap,Show me the code! ^_^^_^
from settings import *
def max_pooling(image, G=8):
# Max Pooling
out = image.copy()
if len(image.shape) == 3:
H, W, C = image.shape
else:
H, W = image.shape[:2]
C = None
hNums = int(H / G)
wNums = int(W / G)
for y in range(hNums):
for x in range(wNums):
if C is not None:
for c in range(C):
out[G * y:G * (y + 1), G * x:G * (x + 1), c] =
np.max(out[G * y:G * (y + 1), G * x:G * (x + 1)], c)
else:
out[G * y:G * (y + 1), G * x:G * (x + 1)] =
np.max(out[G * y:G * (y + 1), G * x:G * (x + 1)])
return out
if __name__ == '__main__':
imagePath = os.path.join(OPENCV_100_Q_PATH, "gray_01.bmp")
imageOrginal = cv.imread(imagePath, cv.IMREAD_GRAYSCALE)
maxPool = max_pooling(imageOrginal, 8)
cv.imshow("Orginal", imageOrginal)
cv.imshow("MaxPool", maxPool)
cv.waitKey(0)
9. Gauss filtering (Gaussian Filter)
Gauss filtering (Gaussian Filter) It is a kind of linear filtering . Gaussian filtering is mainly used for smoothing ( Fuzzy ) Images , Gaussian filter is also a low-pass filter .
The idea of Gaussian filter :
The value of each pixel on the image , It is obtained by weighted averaging the values of itself and other pixels in the neighborhood . Just this weighted kernel , It's based on the Gaussian distribution . The center point is the pixel itself , The whole kernel obeys Gaussian distribution .
Gauss function :
One dimensional Gaussian function :
G(x)FollowsigmaThe value of has a great relationship ,sigmaThe larger the value , The smoother the image ,sigmaThe smaller the value , The sharper the image .


To understand Gaussian Blur , First of all, understand a little , Gauss formula is used to calculate the value of kernel weight , And the convex part of the center point is the pixel to be calculated . Now assume a set of pixels , another sigma=1.5:

Bring the pixel coordinates into the Gaussian formula , The weight of the occupation is :
The result calculated here is the weight of the relative position , This weight has nothing to do with the value of the pixel value , It is calculated by applying Gauss formula according to relative position . After calculating this value , According to normalization , Is to make the sum of weights 1, The method is to divide the above values by their sum , Finally, their sum is 1.

Here we get the Gaussian convolution kernel , Then convolute with the pixel value :
Expand to 2D

Generation of Gaussian filter template :
It is calculated by two-dimensional Gaussian function , Let's say that the length and width of a Gaussian template are 5, The variance of 0.5, So first of all , We are building a coordinate system on the convolution kernel template , Its origin is the center point of Gaussian template , Here's the picture :


Gaussian filter template : Two forms , One is in decimal form , One is in integer form
Template in decimal form :Is the value calculated directly , Then divide the values by their sum .Template in integer form :It needs to be normalized , Normalize the value of the upper left corner of the template to 1. The integer template needs to add a coefficient , The coefficient is the reciprocal of the sum of the template coefficients .
sigma The significance and selection of :
In Gaussian distribution sigma Represents the standard deviation . The standard deviation represents the degree of dispersion , It also represents the width of the pendulum image with Gaussian distribution . sigma The smaller it is , Indicates that the greater the degree of dispersion , The distribution is relatively concentrated , The proportion of the middle part is much higher than that of the other parts . and sigma The bigger it is , Indicates the smaller the degree of dispersion , The distribution is quite scattered , The proportion of the middle part is about the same as that of the other parts .
Gaussian filter code implementation :
# @Time : 2022/6/10 10:35
# @Author : Fioman
# @Phone : 13149920693
# @Tips : Talk is Cheap,Show me the code! ^_^^_^
from settings import *
def gaussian_filter(image, kSize=3, sigma=1.3):
""" Gaussian filter implementation :param image: :param kSize: :param sigma: :return: """
if len(image.shape) == 3:
H, W, C = image.shape
else:
image = np.expand_dims(image, axis=-1)
H, W, C = image.shape
## Zero Padding
pad = kSize // 2
out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float32)
out[pad:pad + H, pad:pad + W] = image.copy().astype(np.float32)
## preprare Kernel
K = np.zeros((kSize, kSize), dtype=np.float32)
for x in range(-pad, -pad + kSize):
for y in range(-pad, -pad + kSize):
K[y + pad, x + pad] = np.exp(-(x ** 2 + y ** 2) / (2 * (sigma ** 2)))
K /= (2 * np.pi * sigma * sigma)
K /= K.sum()
temp = out.copy()
# filter
for y in range(H):
for x in range(W):
for c in range(C):
out[pad + y, pad + x, c] = np.sum(K * temp[y:y + kSize, x:x + kSize, c])
out = np.clip(out, 0, 255)
out = out[pad:pad + H, pad:pad + W].astype(np.uint8)
return out
if __name__ == '__main__':
imagePath = os.path.join(OPENCV_100_Q_PATH, "gray_01.bmp")
imageOriginal = cv.imread(imagePath, cv.IMREAD_GRAYSCALE)
gaussianFilter = gaussian_filter(imageOriginal, 3, 1.2)
cv.imshow("Original", imageOriginal)
cv.imshow("GaussianFilter",gaussianFilter)
cv.waitKey(0)
10. median filtering
Median filter is also a kind of filter that can make the image smooth . The median value of pixels within the filtering core range is used for filtering . Edge fill uses Zero Padding
Code implementation :
# @Time : 2022/6/10 10:54
# @Author : Fioman
# @Phone : 13149920693
# @Tips : Talk is Cheap,Show me the code! ^_^^_^
from settings import *
def median_filter(image, kSize=3):
""" median filtering :param image: Filtered picture :param kSize: Filter core size :return: """
if len(image.shape) == 3:
H, W, C = image.shape
else:
image = np.expand_dims(image, axis=-1)
H, W, C = image.shape
pad = kSize // 2
imagePadding = np.zeros((H + 2 * pad, W + 2 * pad, C), dtype=np.float32)
imagePadding[pad:pad + H, pad:pad + W] = image.copy().astype(np.float32)
temp = imagePadding.copy()
for y in range(H):
for x in range(W):
for c in range(C):
imagePadding[pad + y, pad + x, c] = np.median(temp[y:y + kSize, x:x + kSize, c])
imagePadding = imagePadding[pad:pad + H, pad:pad + W].astype(np.uint8)
return imagePadding
if __name__ == '__main__':
imagePath = os.path.join(OPENCV_100_Q_PATH,"gray_01.bmp")
imageOriginal = cv.imread(imagePath,cv.IMREAD_GRAYSCALE)
medianBlured = median_filter(imageOriginal,3)
cv.imshow("Original",imageOriginal)
cv.imshow("MedianBlured",medianBlured)
cv.waitKey(0)
边栏推荐
- QT custom delegation
- 【TcaplusDB知识库】TcaplusDB TcapProxy扩缩容介绍
- 小微企业如何低成本搭建微官网
- SQL server queries are case sensitive
- String search in C
- (十一)TableView
- leetcode 130. Surrounded regions (medium)
- LeetCode - 5. Longest Palindromic Substring
- Notes (IV) - multithreading
- What about the popular state management library mobx?
猜你喜欢

leetcode 130. Surrounded regions (medium)
![[tcapulusdb knowledge base] tcapulusdb viewing process status introduction](/img/8d/fc454b23489edcd8a7528a0acd3a02.png)
[tcapulusdb knowledge base] tcapulusdb viewing process status introduction

Latex error: file ‘xxx. sty‘ not found
![[tcapulusdb knowledge base] tcapulusdb viewing online operation](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb viewing online operation

鲸会务智慧景区管理解决方案

《暗黑破坏神不朽》数据库资料站地址 暗黑不朽资料库网址

【TcaplusDB知识库】TcaplusDB查看进程状态介绍

【TcaplusDB知识库】TcaplusDB日常巡检介绍

Tcapulusdb Jun · industry news collection (I)

Web3生态去中心化金融平台——Sealem Finance
随机推荐
SQLSERVER database application and development: Chapter 9 computer operation
笔记(二)
How can small and medium-sized conferences be upgraded digitally?
TcaplusDB君 · 行业新闻汇编(一)
自己做了个相亲交友App,有兴趣的朋友可以看看
【TcaplusDB知识库】TcaplusDB推送配置介绍
[tcapulusdb knowledge base] tcapulusdb daily inspection introduction
【TcaplusDB知识库】TcaplusDB事务管理介绍
[MySQL] summary of common data types
TcaplusDB君 · 行业新闻汇编(六)
CCF CSP 202109-4 收集卡牌
【TcaplusDB知识库】TcaplusDB引擎参数调整介绍
torch_geometric
MySQL主从复制解决读写分离
[tcapulusdb knowledge base] tcapulusdb refresh tbus channel introduction
SQL server queries are case sensitive
Whale conference sharing: what should we do if the conference is difficult?
【TcaplusDB知识库】TcaplusDB TcapDB扩缩容介绍
Capacity expansion mechanism of ArrayList
记录(三)