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')

整体代码

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

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()

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()
        
display_img()
#display_gray_img()
#display_video()
#get_frame_img()
#extrats_rgb_img()
#border_fill_img()
#img_compose()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值