当前位置:网站首页>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)
边栏推荐
- typescript12-联合类型
- Why use high-defense CDN when financial, government and enterprises are attacked?
- WMware Tools安装失败segmentation fault解决方法
- Error in go mode tidy go warning “all” matched no packages
- Meeting OA project pending meeting, all meeting functions
- typescript14-(单独指定参数和返回值的类型)
- MySQL database advanced articles
- The difference between truncate and delete in MySQL database
- h264和h265解码上的区别
- 人工智能与云安全
猜你喜欢

typescript10-常用基础类型

Shell编程之条件语句

从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术

Gabor滤波器学习笔记

Error occurred while trying to proxy request The project suddenly can't get up

程序员工作三年攒多少钱合适?

go mode tidy出现报错go warning “all“ matched no packages

Regular expression password policy and regular backtracking mechanism bypass

DOM系列之 client 系列

ShardingSphere之读写分离(八)
随机推荐
ES6中 async 函数、await表达式 的基本用法
go mode tidy出现报错go warning “all“ matched no packages
【唐宇迪 深度学习-3D点云实战系列】学习笔记
【深入浅出玩转FPGA学习15----------时序分析基础】
响应式布局与px/em/rem的比对
unity2D横版游戏教程4-物品收集以及物理材质
ShardingSphere之公共表实战(七)
MySQL grant statements
ShardingSphere's unsharded table configuration combat (6)
网络常用的状态码
Basic usage of async functions and await expressions in ES6
不用Swagger,那我用啥?
Redis learning
MySQL database advanced articles
【Yugong Series】July 2022 Go Teaching Course 013-Constants, Pointers
Mysql systemized JOIN operation example analysis
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
MySQL Series 1: Account Management and Engine
Error occurred while trying to proxy request The project suddenly can't get up
Method for deduplication of object collection