当前位置:网站首页>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)
边栏推荐
- Bypass of xss
- 金融政企被攻击为什么要用高防CDN?
- Jmeter parameter transfer method (token transfer, interface association, etc.)
- 不用Swagger,那我用啥?
- [Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
- BOM系列之history对象
- Homework: iptables prevent nmap scan and binlog
- SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
- 【多线程】
- Go 学习笔记(84)— Go 项目目录结构
猜你喜欢

typescript9-常用基础类型

【多线程】

Jmeter parameter transfer method (token transfer, interface association, etc.)

A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES

【深入浅出玩转FPGA学习13-----------测试用例设计1】

【深入浅出玩转FPGA学习14----------测试用例设计2】

Mysql systemized JOIN operation example analysis

Asser uses ant sword to log in

【Multithreading】

IOT跨平台组件设计方案
随机推荐
DOM系列之scroll系列
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
【c语言课程设计】C语言校园卡管理系统
MySQL数据库(基础)
typescript16-void
Detailed explanation of 9 common reasons for MySQL index failure
WEB Security Basics - - - Vulnerability Scanner
Redis learning
【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
24. 请你谈谈单例模式的优缺点,注意事项,使用场景
IOT跨平台组件设计方案
DOM系列之动画函数封装
MySQL database (basic)
【Multithreading】
【深入浅出玩转FPGA学习14----------测试用例设计2】
Common network status codes
【愚公系列】2022年07月 Go教学课程 016-运算符之逻辑运算符和其他运算符
MySql数据恢复方法个人总结
Preparations for web vulnerabilities
【唐宇迪 深度学习-3D点云实战系列】学习笔记