# -*- coding: utf-8 -*-
"""
Created on Thu Jul 27 09:11:24 2017
@author: user
"""
import numpy as np
import cv2
def test_imread():
img=cv2.imread('C:\Users\user\Desktop\CVTE_lk\cat.jpg')
cv2.imshow('my_first_cat',img)
height,width,channels=img.shape
print img.shape
cv2.waitKey(0)
def test_np_zeros():
screen=np.zeros((400,800),np.uint8)
cv2.imshow('screen_my',screen)
cv2.imwrite('screen_my.jpg',screen)
cv2.waitKey(0)
def test_resize():
image=cv2.imread('C:\Users\user\Desktop\CVTE_lk\cat.jpg')
#r = 100.0 / image.shape[1]
dim = (800, int(image.shape[0] * 2)) #800指的是width
# perform the actual resizing of the image and show it
resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("resized", resized)
cv2.waitKey(0)
def test_crop():
img=cv2.imread('C:\Users\user\Desktop\CVTE_lk\cat.jpg',cv2.IMREAD_GRAYSCALE)#灰度显示
#print img.shape
cv2.imshow('img',img)
crop=img[70:270,50:300]
cv2.imshow('crop',crop)
cv2.waitKey(0)
def test_image_feature():
img2=cv2.imread('C:\Users\user\Desktop\CVTE_lk\charizard.png')
cv2.imshow('img2',img2)
raw=img2.flatten()
#means=cv2.mean(img2)
means,dev=cv2.meanStdDev(img2)
stats=np.concatenate([means,dev]).flatten()
print raw.shape
print raw
print means
print 'stats=',stats
cv2.waitKey(0)
def test_img_convert_clo():
img3=cv2.imread('C:\Users\user\Desktop\CVTE_lk\charizard.png')
cv2.imshow('INPUT',img3)
img4=cv2.cvtColor(img3,cv2.COLOR_BGR2RGB)
cv2.imshow('img4',img4)
print img4.size
#print img4[:,:,0]
cv2.imshow('img3_B',img4[:,:,0])
cv2.imshow('img3_G',img4[:,:,1])
cv2.imshow('img3_R',img4[:,:,2])
cv2.waitKey(0)
#==============================================================================
# 2017/7/28
#==============================================================================
def test_img_convert_clo2():
img3=cv2.imread('C:\Users\user\Desktop\CVTE_lk\charizard.png')
cv2.imshow('INPUT',img3)
img4=cv2.cvtColor(img3,cv2.COLOR_BGR2HSV)
cv2.imshow('img4',img4)
print img4.size
#print img4[:,:,0]
cv2.imshow('img3_H',img4[:,:,0])
cv2.imshow('img3_S',img4[:,:,1])
cv2.imshow('img3_V',img4[:,:,2])
cv2.waitKey(0)
def test_img_affine():
img4=cv2.imread('C:\Users\user\Desktop\CVTE_lk\charizard.png')
img_rows,img_clos=img4.shape[:2]
cv2.imshow('INPUT_orginal',img4)
affine=np.float32([[1,0,30],[0,1,30]])
img5=cv2.warpAffine(img4,affine,(img_clos,img_rows))
cv2.imshow('output',img5)
cv2.waitKey(0)
def test_img_rotation():
img5=cv2.imread('C:\Users\user\Desktop\CVTE_lk\charizard.png')
img_rows,img_clos=img5.shape[:2]
cv2.imshow('INPUT_orginal',img5)
rotation_matrix=cv2.getRotationMatrix2D((img_clos,img_rows),30,1)
img5=cv2.warpAffine(img5,rotation_matrix,(img_clos*3,img_rows*3))
affine=np.float32([[1,0,0.5*img_clos],[0,1,0.5*img_rows]])
img51=cv2.warpAffine(img5,affine,(img_clos*2,img_rows*2))
cv2.imshow('output',img51)
cv2.waitKey(0)
#==============================================================================
# 镜像变换,属于仿射变换的一种
#==============================================================================
def test_img_affine1():
img = cv2.imread('C:\Users\user\Desktop\CVTE_lk\charizard.png')
rows, cols = img.shape[:2]
src_points = np.float32([[0,0],[cols-1,0], [0,rows-1]])
dst_points = np.float32([[cols-1,0],[0,0], [cols-1,rows-1]])
affine_matrix = cv2.getAffineTransform(src_points, dst_points)
img_output = cv2.warpAffine(img, affine_matrix,(cols,rows))
cv2.imshow('Input', img)
cv2.imshow('Output', img_output)
cv2.waitKey(0)
def test_img_filter():
img=cv2.imread('C:\Users\user\Desktop\CVTE_lk\charizard.png')
cv2.imshow('Input',img)
rows,cols=img.shape[:2]
output=cv2.medianBlur(img,7)
cv2.imshow('output',output)
cv2.waitKey(0)
def test_img_SIFT():
img=cv2.imread('C:\Users\user\Desktop\CVTE_lk\charizard.png')
cv2.imshow('Input',img)
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT()
keypoints = sift.detect(gray_img, None)
input_image = cv2.drawKeypoints(img,keypoints,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('SIFT features', input_image)
cv2.waitKey(0)
#pass
def test_img_he():
img=cv2.imread('C:\Users\user\Desktop\CVTE_lk\charizard.png')
cv2.imshow('img_input',img)
#cv2.waitKey(0)
key=cv2.waitKey(0)
if key == 27:
cv2.destroyAllWindows()
return
#==============================================================================
#
#==============================================================================
def test_img_perspective():
img = cv2.imread(r'C:\Users\user\Desktop\CVTE_lk\beauty.jpg')
rows, cols = img.shape[:2]
src_points = np.float32([[0,0], [cols-1,0], [0,rows-1], [cols-1,rows-1]])
dst_points = np.float32([[0,0], [cols-1,0], [int(0.33*cols),rows-1], [int(0.66*cols),rows-1]])
#dst_points = np.float32([[int(0.33*cols),0], [cols-1,0], [int(0.33*cols),rows-1], [int(0.66*cols),rows-1]])
projective_matrix = cv2.getPerspectiveTransform(src_points, dst_points)
img_output = cv2.warpPerspective(img,projective_matrix, (cols,rows))
cv2.imshow('Input',img)
cv2.imshow('Output',img_output)
cv2.waitKey(0)
if __name__=='__main__':
#test_imread()
#test_np_zeros()
#test_resize()
#test_crop()
#test_img_convert_clo2()
#test_image_feature()
#test_img_affine()
#test_img_rotation()
#test_img_affine1()
#test_img_filter()
#test_img_SIFT()
#test_img_he()
test_img_perspective()
print 'return ok?'
OpenCv基本操作
最新推荐文章于 2022-12-08 21:14:01 发布