当前位置:网站首页>Image processing tool design
Image processing tool design
2022-07-31 00:48:00 【The wind blows the fallen leaves and flowers】
图像处理工具设计
import cv2 as cv
import numpy as np
#
'''
函数功能:
Gramm 提高图像对比度
img:输入图像
power1:gramm值,The greater the greater the contrast
k:亮度,Multiple output image brightness
'''
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
'''
函数功能:
提取边缘,
kFor the limiting magnitude
'''
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
'''
函数功能:
The video into frames
VideoPath:输入视频的地址
dstPath:Of the output image after address
'''
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()
'''
函数功能:
输入图片,And then we through the photosGrammWith the brightness contrast ascension best proportion of rolling test,便于调参
'''
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) # According to the binary map to find outline
cv.drawContours(img1, contours, -1, (0, 0, 255), 3) # The outline of the picture on the artwork(0,0,255) 表示 RGB 三通道,红色
htich=np.hstack((img1,img2,th3))
cv.imshow('image',htich)
if cv.waitKey(1)&0xFF==27:
break
cv.destrayAllWindow()
'''
函数功能:
遍历图像,And connect the recent pixel
img:输入图像
i,j:当前像素的位置
k:基于当前位置,To retrieve the Manhattan distance less than or equal tok的像素,并连接
thre:Pixel value is greater thanthresholdWhen the connection
'''
dicPix=dict()#Save the connection number of pixels are
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:
# Find the nearest point after the attachment,And tag here
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
'''
函数功能:
Obtained from an image of a pixel recently pixel position
Based on breadth search to find out the recent pixel position
'''
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)#To press the current pixel into
dicBook=dict()#创建一个字典,避免重复添加
dicBook[i*ColMax+j]=1
dicBook = dict() # 创建一个字典,避免重复添加
while not q.empty():
nowPix=q.get()
nowrow,nowcol=nowPix//ColMax,nowPix%ColMax
# If the current pixel values greater than or equal to the value,And is connected the number of less than2则返回
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]
#Determine whether traversal before that point,And that point without crossing the line
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) #The current position of pixels into
# If there is no find the nearest point,则返回-1,-1
return -1,-1
'''
函数功能:
For the picture each pixel location assignment through all previous number is0
'''
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)
边栏推荐
- Niuke.com question brushing training (4)
- Method for deduplication of object collection
- MySQL triggers
- 人工智能与云安全
- MySQL笔记下
- Why use high-defense CDN when financial, government and enterprises are attacked?
- Mini Program - Global Data Sharing
- MySQL的触发器
- 解决:Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
- Adding, deleting, modifying and checking the foundation of MySQL
猜你喜欢
Summary of MySQL database interview questions (2022 latest version)
WEB Security Basics - - - Vulnerability Scanner
【Demo】ABAP Base64加解密测试
unity2D横版游戏教程4-物品收集以及物理材质
Mini Program - Global Data Sharing
MySQL数据库面试题总结(2022最新版)
MySQL数据库进阶篇
MySQL master-slave replication and read-write separation script - pro test available
xss bypass: prompt(1)
程序员工作三年攒多少钱合适?
随机推荐
redis学习
Error in go mode tidy go warning “all” matched no packages
DNS解析过程【访问网站】
typescript11-数据类型
ELK部署脚本---亲测可用
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
这个项目太有极客范儿了
ShardingSphere之公共表实战(七)
MySQL database advanced articles
unity2D横版游戏教程4-物品收集以及物理材质
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
【深度学习】Transformer模型详解
ShardingSphere's unsharded table configuration combat (6)
加密传输过程
BOM系列之Navigator对象
DOM系列之 offset 系列
Kotlin协程:协程上下文与上下文元素
pytorch bilinear interpolation
Gabor filter study notes
Error occurred while trying to proxy request The project suddenly can't get up