您好,登錄后才能下訂單哦!
這篇文章主要介紹Python中opencv操作的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
直接讀取圖片
def display_img(file="p.jpeg"): img = cv.imread(file) print (img.shape) cv.imshow('image',img) cv.waitKey(0) cv.destroyAllWindows()
讀取灰度圖片
def display_gray_img(file="p.jpeg"): img = cv.imread(file,cv.IMREAD_GRAYSCALE) print (img.shape) cv.imshow('image',img) cv.waitKey(0) cv.destroyAllWindows() cv.imwrite("gray_img.png",img)
讀取視頻
def display_video(file="sj.mp4"): v = cv.VideoCapture(file) if v.isOpened(): open,frame = v.read() else: open=False while open: ret,frame = v.read() if frame is None: break if ret == True: gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY) cv.imshow("result",gray) if cv.waitKey(10) & 0xFF == 27: break v.release() v.waitKey(0) v.destroyAllWindows()
截取圖片
def get_frame_img(file="p.jpeg"): img = cv.imread(file) print (img.shape) cat = img[0:200,0:200] cv.imshow('get_frame_img',cat) cv.waitKey(0) cv.destroyAllWindows()
提取rgb通道
def extrats_rgb_img(file="p.jpeg"): img = cv.imread(file) b,g,r = cv.split(img) print (b.shape,g.shape,r.shape) new_img = cv.merge((b,g,r)) print (new_img.shape) copy_img_r = img.copy() copy_img_r[:,:,0]=0 copy_img_r[:,:,1]=0 cv.imshow("r_img",copy_img_r) copy_img_g = img.copy() copy_img_g[:,:,0]=0 copy_img_g[:,:,2]=0 cv.imshow("g_img",copy_img_g) copy_img_b = img.copy() copy_img_b[:,:,1]=0 copy_img_b[:,:,2]=0 cv.imshow("b_img",copy_img_b)
邊界填充
def border_fill_img(file="p.jpeg"): border_type = [ cv.BORDER_REPLICATE,#復制法,復制邊緣 cv.BORDER_REFLECT, #反射法,對感興趣的圖像中的像素在兩邊進行復制 cv.BORDER_REFLECT_101,#反射法,以邊緣像素為軸,對稱 cv.BORDER_WRAP,#外包裝法 cv.BORDER_CONSTANT#常量法,常量填充 ] border_title = [ "REPLICATE", "REFLECT", "REFLECT_101", "WRAP", "CONSTANT" ] img = cv.imread(file) top_size,bottom_size,left_size,right_size = (50,50,50,50) plt.subplot(231) plt.imshow(img,"gray")#原始圖像 plt.title("ORIGNAL") for i in range(len(border_type)): result = cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,border_type[i]) plt.subplot(232+i) plt.imshow(result,"gray") plt.title(border_title[i]) plt.show()
圖像融合,變換
def img_compose(file1="tu.jpeg",file2="gui.jpeg"): img_1 = cv.imread(file1) img_2 = cv.imread(file2) print (img_1.shape) print (img_2.shape) img_1= cv.resize(img_1,(500,500)) img_2= cv.resize(img_2,(500,500)) print (img_1.shape) print (img_2.shape) res = cv.addWeighted(img_1,0.4,img_2,0.6,0) plt.imshow(res) plt.show() res = cv.resize(img_1,(0,0),fx=3,fy=1) plt.imshow(res) plt.show() res = cv.resize(img_2,(0,0),fx=1,fy=3) plt.imshow(res) plt.show()
二值化處理
def Binarization(filepath): img = cv2.imread(filepath,0) limit = 120 ret,thresh=cv2.threshold(img,limit,255,cv2.THRESH_BINARY_INV) plt.imshow(thresh,'gray') plt.show() return thresh Binarization('t1.jpg')
以上是“Python中opencv操作的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。