图像处理工具设计

图像处理工具设计

import cv2 as cv
import numpy as np
#
'''
函数功能:
Gramm 提高图像对比度
img:输入图像
power1:gramm值,越大对比度越大
k:亮度,输出图像亮度倍数
'''
def gama_transfer(img,power1,k):
    k/=2.0
    if len(img.shape) == 3:
         img= cv.cvtColor(img,cv.COLOR_BGR2RGB)
    img = 255*np.power(img/255,power1)
    img = np.around(img)
    img[img>255] = 255
    out_img = img.astype(np.uint8)
    out_img = cv.addWeighted(out_img, k, out_img, k, 0)
    return out_img

'''
函数功能:
提取边缘,
k为边缘亮度
'''
def Soble_Transfer(grayImg,k):
    k/=2
    if len(grayImg.shape) == 3:
         grayImg= cv.cvtColor(grayImg,cv.COLOR_BGR2RGB)

    x = cv.Sobel(grayImg, cv.CV_16S, 1, 0)  # 对x求一阶导
    y = cv.Sobel(grayImg, cv.CV_16S, 0, 1)  # 对y求一阶导
    absX = cv.convertScaleAbs(x)
    absY = cv.convertScaleAbs(y)
    # edge_output=cv.Canny(absX,absY,50,150)
    Sobel = cv.addWeighted(absX, k, absY, k, 0)
    return Sobel


'''
函数功能:
将视频拆分成帧
VideoPath:输入视频的地址
dstPath:拆分后输出图片的地址
'''
import os
def Video2Imgs(VideoPath,dstPath):
    if not os.path.isdir(dstPath):
        os.mkdir(dstPath)
    capture=cv.VideoCapture(VideoPath)#
    if not capture.isOpened():
        print('not Open this Video')
        exit(0)
    ret,frame=capture.read()
    index=0
    while ret:
        cv.imwrite(os.path.join(dstPath,str(index)+'.jpg'),frame)
        print(os.path.join(dstPath,str(index)+'.jpg'))
        ret, frame = capture.read()
        index+=1
        cv.waitKey(1)
    capture.release()

'''
函数功能:
输入图片,然后我们进行图片的Gramm与对比度亮度提升的最佳比例滚动测试,便于调参

'''
def task():
    pass
def gra2conTrackbar(img):
    cv.namedWindow('image',256)
    cv.createTrackbar('Contrast','image',100,1000,task)
    cv.createTrackbar('Gramm','image',100,1000,task)
    cv.createTrackbar('Thres','image',100,255,task)
    cv.createTrackbar('Sobel', 'image', 100, 1000, task)
    kernel=cv.getStructuringElement(0,(3,3))

    while True:
        con=cv.getTrackbarPos('Contrast','image')
        gra=cv.getTrackbarPos('Gramm','image')
        th=cv.getTrackbarPos('Thres','image')
        so=cv.getTrackbarPos('Sobel','image')

        img1=cv.cvtColor(img,cv.COLOR_RGB2GRAY)
        img1=gama_transfer(img1,gra/100,con/100)
        #img2 = Soble_Transfer(img1, so / 100)
        # print(con/100)
        img2 = cv.erode(img1, kernel)
        img2 = cv.dilate(img2, kernel)
       # ConnectGrayImg(img2,5,30)



        ret3, th3 = cv.threshold(img1, th, 255, cv.THRESH_OTSU)
        contours,_ = cv.findContours(th3, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)  # 根据二值图找轮廓
        cv.drawContours(img1, contours, -1, (0, 0, 255), 3)  # 把轮廓画在原图上(0,0,255) 表示 RGB 三通道,红色

        htich=np.hstack((img1,img2,th3))
        cv.imshow('image',htich)
        if cv.waitKey(1)&0xFF==27:
            break
    cv.destrayAllWindow()

'''
函数功能:
遍历图像,并连接最近的像素
img:输入图像
i,j:当前像素的位置
k:基于当前位置,向外检索曼哈顿距离小于等于k的像素,并连接
thre:像素值当大于threshold时才连接
'''
dicPix=dict()#保存该像素被连接次数
def ConnectGrayImg(img,k,thre):
    initPix(img)
    Row=img.shape[0]#高
    Col=img.shape[1]#宽
    tempImg2=np.zeros_like(np.ones_like(img))
    for i in range(Row):
        for j in range(Col):
            if dicPix[i*Row+j]<2:
                x,y=getNearPix(img,i,j,k,thre)

                if x!=-1 and y!=-1 and dicPix[x * Row + y]<2:
                    # 找到最近的点之后就连线,并标记来过
                    print(x * Row + y)
                    dicPix[i * Row + j] += 1
                    dicPix[x * Row + y] += 1
                    cv.line(tempImg2,(x,y),(i,j),(250,250,250),1)

    cv.imshow('11',tempImg2)
    cv.waitKey(0)
from queue import Queue
'''
函数功能:
获取距离某个图像的一个像素的最近像素的位置
基于广度搜索可求出最近像素点位置
'''
def getNearPix(img,i,j,k,th1):
    RowMax=img.shape[0]#高
    ColMax=img.shape[1]#宽
    #Channel=img.shape[2]#通道数


    lrow,rrow=max(0,i-k),min(RowMax-1,i+k)
    lcol,rcol=max(0,j-k),min(ColMax-1,j+k)

    vis=[[0,1],[0,-1],[1,-1],[1,0],[1,1],[-1,-1],[-1,0],[-1,1]]
    q=Queue()
    q.put(i*ColMax+j)#将当前像素压入
    dicBook=dict()#创建一个字典,避免重复添加
    dicBook[i*ColMax+j]=1
    dicBook = dict()  # 创建一个字典,避免重复添加
    while not q.empty():
        nowPix=q.get()
        nowrow,nowcol=nowPix//ColMax,nowPix%ColMax
        # 如果当前像素值大于等于阀值,且被连接的次数小于2则返回
        if img[int(nowrow)][int(nowcol)]>=th1 and dicPix[int(nowrow)*ColMax+int(nowcol)]<2:
            return nowrow,nowcol
        for i in range(8):
            trow=nowrow+vis[i][0]
            tcol=nowcol+vis[i][1]
            #判断是否之前遍历过该点,并且该点没有越界
            if trow>=lrow and trow<=rrow and tcol>=lcol and tcol<=rcol and trow*ColMax+tcol not in dicBook:
                dicBook[trow * ColMax + tcol] = 1
                q.put(trow * ColMax + tcol)        #将当前位置像素压入
    # 如果没有找到最近的点,则返回-1,-1
    return -1,-1
'''
函数功能:
为图片的每一个像素位置赋值遍历次数为0
'''
def initPix(img):
    for i in range(img.shape[0]+1):
        for j in range(img.shape[1]+1):
            dicPix[i*img.shape[1]+j]=0


if __name__=='__main__':
    img=cv.imread('tempImg/3.jpg')
    try:
        img.shape
    except:
        print('not find this image')
        exit(0)
    gra2conTrackbar(img)

猜你喜欢

转载自blog.csdn.net/qq_51116518/article/details/126078185
今日推荐