1、亮度增强
import cv2
import numpy as np
img = cv2. imread( 'image0.jpg' , 1 )
height = int ( img. shape[ 0 ] )
width = int ( img. shape[ 1 ] )
dst = np. zeros( ( height, width, 3 ) , np. uint8)
for i in range ( 0 , height) :
for j in range ( 0 , width) :
( b, g, r) = img[ i, j]
b = int ( b) + 40
g = int ( g) + 40
r = int ( r) + 40
if b> 255 :
b = 255
if g> 255 :
g = 255
if r> 255 :
r = 255
dst[ i, j] = ( b, g, r)
cv2. imshow( 'src' , img)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
2、磨皮美白
import cv2
img = cv2. imread( '1.png' , 1 )
cv2. imshow( 'src' , img)
dst = cv2. bilateralFilter( img, 15 , 35 , 35 )
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
3、灰度图像直方图(源码)
import cv2
import numpy as np
import matplotlib. pyplot as plt
img = cv2. imread( 'image0.jpg' , 1 )
height = int ( img. shape[ 0 ] )
width = int ( img. shape[ 1 ] )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
count = np. zeros( 256 , np. float )
for i in range ( 0 , height) :
for j in range ( 0 , width) :
pixel = gray[ i, j]
index = int ( pixel)
count[ index] = count[ index] + 1
for i in range ( 0 , 256 ) :
count[ i] = count[ i] / height* width
x = np. linspace( 0 , 255 , 256 )
y = count
plt. bar( x, y, 0.9 , alpha= 1 , color= 'b' )
plt. show( )
plt. bar( x, y, 1.9 , alpha= 1 , color= 'r' )
plt. show( )
4、彩色图像直方图
import cv2
import numpy as np
img = cv2. imread( 'C:\\Users\\18792\\Desktop\\DL\\image0.jpg' , 1 )
b, g, r = cv2. split( img)
def calcHist ( image, color) :
hist = cv2. calcHist( [ image] , [ 0 ] , None , [ 256 ] , [ 0.0 , 256.0 ] )
minV, maxV, minL, maxL = cv2. minMaxLoc( hist)
histImg = np. zeros( [ 256 , 256 , 3 ] , np. uint8)
for h in range ( 256 ) :
intensity = int ( hist[ h] * int ( 0.9 * 256 ) / maxV)
cv2. line( histImg, ( h, 256 ) , ( h, 256 - intensity) , color)
return histImg
histImgB = calcHist( b, [ 255 , 0 , 0 ] )
histImgG = calcHist( g, [ 0 , 255 , 0 ] )
histImgR = calcHist( r, [ 0 , 0 , 255 ] )
cv2. imshow( 'histImgB' , histImgB)
cv2. imshow( 'histImgG' , histImgG)
cv2. imshow( 'histImgR' , histImgR)
cv2. waitKey( 0 )
import cv2
import numpy as np
import matplotlib. pyplot as plt
img = cv2. imread( 'image0.jpg' , 1 )
height = int ( img. shape[ 0 ] )
width = int ( img. shape[ 1 ] )
count_b = np. zeros( 256 , np. float )
count_g = np. zeros( 256 , np. float )
count_r = np. zeros( 256 , np. float )
for i in range ( 0 , height) :
for j in range ( 0 , width) :
b, g, r = img[ i, j]
index_b = int ( b)
index_g = int ( g)
index_r = int ( r)
count_b[ index_b] = count_b[ index_b] + 1
count_g[ index_g] = count_g[ index_g] + 1
count_r[ index_r] = count_r[ index_r] + 1
for i in range ( 0 , 256 ) :
count_b[ i] = count_b[ i] / height* width
count_g[ i] = count_g[ i] / height* width
count_r[ i] = count_r[ i] / height* width
x = np. linspace( 0 , 255 , 256 )
y_b = count_b
plt. figure( )
plt. bar( x, y_b, 0.9 , alpha= 1 , color= 'b' )
y_g = count_g
plt. figure( )
plt. bar( x, y_g, 0.9 , alpha= 1 , color= 'g' )
y_r = count_r
plt. figure( )
plt. bar( x, y_r, 0.9 , alpha= 1 , color= 'r' )
plt. show( )
cv2. waitKey( 0 )
5、直方图均衡化
import cv2
img = cv2. imread( 'image0.jpg' , 1 )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
dst = cv2. equalizeHist( gray)
cv2. imshow( 'src' , gray)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
import cv2
img = cv2. imread( 'image0.jpg' , 1 )
b, g, r = cv2. split( img)
b1 = cv2. equalizeHist( b)
g1 = cv2. equalizeHist( g)
r1 = cv2. equalizeHist( r)
dst = cv2. merge( ( b1, g1, r1) )
cv2. imshow( 'src' , img)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
6、灰度图像直方图均衡化
'''
直方图纵坐标:每个像素值出现的概率
概率 累加概率
0.5 0.5
0.3 0.8
0.1 0.9
直方图均衡化:每个像素的new概率=累加概率*255
'''
import cv2
import numpy as np
import matplotlib. pyplot as plt
img = cv2. imread( 'image0.jpg' , 1 )
height = int ( img. shape[ 0 ] )
width = int ( img. shape[ 1 ] )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
cv2. imshow( 'src' , gray)
count = np. zeros( 256 , np. float )
for i in range ( 0 , height) :
for j in range ( 0 , width) :
pixel = gray[ i, j]
index = int ( pixel)
count[ index] = count[ index] + 1
for i in range ( 0 , 256 ) :
count[ i] = count[ i] / ( height* width)
sum1 = float ( 0 )
for i in range ( 0 , 256 ) :
sum1 = sum1+ count[ i]
count[ i] = sum1
map1 = np. zeros( 256 , np. uint16)
for i in range ( 0 , 256 ) :
map1[ i] = np. uint16( count[ i] * 255 )
for i in range ( 0 , height) :
for j in range ( 0 , width) :
pixel = gray[ i, j]
gray[ i, j] = map1[ pixel]
cv2. imshow( 'dst' , gray)
cv2. waitKey( 0 )
7、彩色图像直方图均衡化
import cv2
import numpy as np
img = cv2. imread( 'image0.jpg' , 1 )
height = int ( img. shape[ 0 ] )
width = int ( img. shape[ 1 ] )
countB = np. zeros( 256 , np. float )
countG = np. zeros( 256 , np. float )
countR = np. zeros( 256 , np. float )
for i in range ( 0 , height) :
for j in range ( 0 , width) :
b, g, r = img[ i, j]
indexB = int ( b)
indexG = int ( g)
indexR = int ( r)
countB[ indexB] = countB[ indexB] + 1
countG[ indexG] = countG[ indexG] + 1
countR[ indexR] = countR[ indexR] + 1
for i in range ( 0 , 256 ) :
countB[ i] = countB[ i] / ( height* width)
countG[ i] = countG[ i] / ( height* width)
countR[ i] = countR[ i] / ( height* width)
sum1 = float ( 0 )
sum2 = float ( 0 )
sum3 = float ( 0 )
for i in range ( 0 , 256 ) :
sum1 = sum1+ countB[ i]
countB[ i] = sum1
sum2 = sum2+ countG[ i]
countG[ i] = sum2
sum3 = sum3+ countR[ i]
countR[ i] = sum3
map1 = np. zeros( 256 , np. uint16)
map2 = np. zeros( 256 , np. uint16)
map3 = np. zeros( 256 , np. uint16)
for i in range ( 0 , 256 ) :
map1[ i] = np. uint16( countB[ i] * 255 )
map2[ i] = np. uint16( countG[ i] * 255 )
map3[ i] = np. uint16( countR[ i] * 255 )
dst = np. zeros( ( height, width, 3 ) , np. uint8)
for i in range ( 0 , height) :
for j in range ( 0 , width) :
b, g, r = img[ i, j]
indexB = int ( b)
indexG = int ( g)
indexR = int ( r)
dst[ i, j] = ( map1[ indexB] , map2[ indexG] , map3[ indexR] )
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
8、线段绘制
import cv2
import numpy as np
newImageInfo = ( 500 , 500 , 3 )
dst = np. zeros( newImageInfo, np. uint8)
cv2. line( dst, ( 100 , 200 ) , ( 400 , 200 ) , ( 0 , 0 , 255 ) , 20 )
cv2. line( dst, ( 100 , 300 ) , ( 400 , 300 ) , ( 255 , 0 , 0 ) , 20 , cv2. LINE_AA)
cv2. line( dst, ( 200 , 150 ) , ( 50 , 250 ) , ( 0 , 255 , 0 ) , 20 )
cv2. line( dst, ( 50 , 250 ) , ( 400 , 380 ) , ( 0 , 255 , 0 ) , 20 )
cv2. line( dst, ( 400 , 380 ) , ( 200 , 150 ) , ( 0 , 255 , 0 ) , 20 )
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
9、绘制图形
import cv2
import numpy as np
newImageInfo = ( 500 , 500 , 3 )
dst = np. zeros( newImageInfo, np. uint8)
cv2. line( dst, ( 100 , 200 ) , ( 400 , 200 ) , ( 0 , 0 , 255 ) , 20 )
cv2. line( dst, ( 100 , 300 ) , ( 400 , 300 ) , ( 255 , 0 , 0 ) , 20 , cv2. LINE_AA)
cv2. line( dst, ( 200 , 150 ) , ( 50 , 250 ) , ( 0 , 255 , 0 ) , 20 )
cv2. line( dst, ( 50 , 250 ) , ( 400 , 380 ) , ( 0 , 255 , 0 ) , 20 )
cv2. line( dst, ( 400 , 380 ) , ( 200 , 150 ) , ( 0 , 255 , 0 ) , 20 )
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
10、文字图片绘制
import cv2
import numpy as np
img = cv2. imread( 'image0.jpg' , 1 )
font = cv2. FONT_HERSHEY_SIMPLEX
cv2. rectangle( img, ( 50 , 200 ) , ( 100 , 300 ) , ( 223 , 123 , 44 ) , 5 )
cv2. putText( img, 'this is wangboyun' , ( 100 , 200 ) , font, 1 , ( 212 , 1 , 34 ) , 4 )
cv2. imshow( 'src' , img)
cv2. waitKey( 0 )
import cv2
import numpy as np
img = cv2. imread( 'image0.jpg' , 1 )
font = cv2. FONT_HERSHEY_SIMPLEX
cv2. rectangle( img, ( 50 , 200 ) , ( 100 , 300 ) , ( 223 , 123 , 44 ) , 5 )
cv2. putText( img, 'this is wangboyun' , ( 100 , 200 ) , font, 1 , ( 212 , 1 , 34 ) , 4 )
cv2. imshow( 'src' , img)
cv2. waitKey( 0 )
11、高斯、均值、中值滤波
import cv2
img = cv2. imread( 'image0.jpg' , 1 )
cv2. imshow( 'src' , img)
dst = cv2. GaussianBlur( img, ( 3 , 3 ) , 1.5 )
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
import cv2
import cv2
img = cv2. imread( 'image0.jpg' , 1 )
cv2. imshow( 'src' , img)
dst = cv2. blur( img, ( 3 , 3 ) )
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
import cv2
import cv2
img = cv2. imread( 'image0.jpg' , 1 )
cv2. imshow( 'src' , img)
dst = cv2. medianBlur( img, 3 )
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )