当前位置: 移动技术网 > IT编程>脚本编程>Python > 可操作的python——opencv边缘提取的实战

可操作的python——opencv边缘提取的实战

2020年07月30日  | 移动技术网IT编程  | 我要评论

课前笔记

由于用的工具有pygame和opencv,没有的要在官网上先下载才行哦,所生成的图片有保存在py同一文件夹。

所用程序汇总如下:
#载入图片
img=pygame.image.load(“girl.png”)
#显示图片
screen.blit(imgtext,(x,y))
#大小提取
w2,h2=planet.get_size()
#opencv操作
Pic=cv.imread(“girl.png")
cv.imshow(“input image”,Pic)
cv.imwrite(“result.png”,Pic)

程序部分

简单的动画效果程序,鼠标移动到图片执行,左右键更换效果。

import pygame,random,time
from pygame.locals import *
import cv2 as cv
import numpy as np

filename="woman.png"#此处填写路径名,若图片位置在py一个地方,就只填图片

def print_t(screen,font,x,y,text,color=(255,255,255)):
    #   输入文字
    imgtext=font.render(text,True,color)
    screen.blit(imgtext,(x,y))
#初始化
def game_init1():
    global w,h,img
    pygame.init()
    img=pygame.image.load(filename)
    size=w,h=img.get_size()
    screen = pygame.display.set_mode(size,0,32)
    pygame.display.set_caption("精灵类测试")
    return screen

#主程序
def game_group(screen):
    font = pygame.font.SysFont("SimHei",24)
    g_pic=Change_pic()
    g_pic.input_image()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            g_pic.press_key(event)
            keys= pygame.key.get_pressed()
            if keys[pygame.K_ESCAPE]:
                exit()
                
        screen.fill((100,255,255))
        g_pic.react()
        g_pic.display(font)
        pygame.display.update()

#opencv块程序,可以捞出来单独玩
def result_image():
    image=cv.imread(filename)
    blurred=cv.GaussianBlur(image,(3,3),0)
    cv.imwrite("result1.png",blurred)
    gray=cv.cvtColor(blurred,cv.COLOR_BGR2GRAY)
    cv.imwrite("result2.png",gray)
    #图像梯度,边缘提取
    xgrad=cv.Sobel(gray,cv.CV_16SC1,1,0)
    ygrad=cv.Sobel(gray,cv.CV_16SC1,0,1)
    edge_output=cv.Canny(xgrad,ygrad,50,150)
    cv.imwrite("result3.png",edge_output)
    #边缘改为白字黑字,非逻辑
    output=cv.bitwise_not(edge_output)
    cv.imwrite("result4.png",output)
    #合并源图像,与逻辑
    dst=cv.bitwise_and(image,image,mask=output)
    cv.imwrite("result5.png",dst)

 
class Change_pic(object):
    def __init__(self):
        self.pic_group=[0 for col in range(10)]
        self.num=4
        self.mouse_x=0
        self.mouse_y=0
        self.key_num=0
        self.press=False
    #pygame导入图片  
    def input_image(self):
        self.pic_group[0]=img
        for n in range(1,6):
            string="result"+str(n)+".png"
            self.pic_group[n]=pygame.image.load(string)
	#按键响应
    def press_key(self,event):
        if event.type==pygame.MOUSEMOTION:
            self.mouse_x,self.mouse_y=event.pos
        elif event.type==pygame.MOUSEBUTTONDOWN:
            self.press=True
            self.key_num=event.button
	#鼠标左右键换图程序
    def react(self):
        if w/4<self.mouse_x<w*3/4 and h/4<self.mouse_y<h*3/4:
            screen.blit(self.pic_group[self.num],(0,0))
            self.keys()
        else:
            screen.blit(self.pic_group[0],(0,0))
	#按键响应
    def keys(self):
        if self.press:
            if self.key_num==1:
                self.num-=1
                self.press=False
            if self.key_num==3:
                self.num+=1
                self.press=False
        if self.num>=6:
            self.num=1
        if self.num<=0:
            self.num=5
	#制定文字,不显示在保存图片中
    def display(self,font):
        black=0,0,0
        white=255,255,255
        test=["原图",
              "去噪",
              "灰度",
              "提取",
              "反色",
              "成图",]
        if self.num==3:
            color=white
        else :
            color=black
        print_t(screen,font,20,20,test[self.num],color)
        
if __name__=='__main__':
    result_image()
    screen=game_init1()
    game_group(screen)

由于发现手机端最后一部分代码无法显示,所以稍微做了点更改,谢谢大家!

本文地址:https://blog.csdn.net/weixin_46921175/article/details/107625396

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网