import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np
block_size = 3
ksize = 3
k = 0.05
image = cv. imread( './lighttower.jpg' )
print ( image. shape)
height = image. shape[ 0 ]
width = image. shape[ 1 ]
channels = image. shape[ 2 ]
print ( 'image\'s height:%s, width:%s, number of channel:%s' % ( height, width, channels) )
(768, 1024, 3)
image's height:768, width:1024, number of channel:3
RGB图像转灰度图
gray_img = cv. imread( './lighttower.jpg' , cv. IMREAD_GRAYSCALE)
plt. imshow( gray_img)
<matplotlib.image.AxesImage at 0x7fda708fd890>
gray_img = cv. cvtColor( image, cv. COLOR_BGR2GRAY)
gray_img = np. float32( gray_img)
plt. imshow( gray_img)
<matplotlib.image.AxesImage at 0x7fda7063d990>
corner_image = cv. cornerHarris( gray_img, block_size, ksize, k)
kernel = cv. getStructuringElement( cv. MORPH_RECT, ( 2 , 2 ) )
dst = cv. dilate( corner_image, kernel)
for r in range ( height) :
for c in range ( width) :
pix = dst[ r, c]
if pix> 0.05 * dst. max ( ) :
cv. circle( image, ( c, r) , 5 , ( 0 , 0 , 255 ) , 0 )
img = cv. cvtColor( image, cv. COLOR_BGR2RGB)
plt. imshow( img)
<matplotlib.image.AxesImage at 0x7fda71c028d0>