您的位置:首页 > 健康 > 美食 > 闵行18路_天津网站制作报价_清理优化大师_惠州疫情最新情况

闵行18路_天津网站制作报价_清理优化大师_惠州疫情最新情况

2024/12/23 2:18:08 来源:https://blog.csdn.net/2301_79810514/article/details/143286618  浏览:    关键词:闵行18路_天津网站制作报价_清理优化大师_惠州疫情最新情况
闵行18路_天津网站制作报价_清理优化大师_惠州疫情最新情况

实验图片如下:
命名为img1.jpg, 放在项目下新建文件夹images下
在这里插入图片描述
项目构造如下:
在这里插入图片描述

app.py源码如下

import cv2
import os
from matplotlib import pyplot as plt
import numpy as npclass ImageProcessor:def __init__(self, image_path):self.image = cv2.imread(image_path)  # Load the imageif self.image is None:raise ValueError("Image not found or unable to load.")self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)  # Convert to RGB formatself.gray_image = cv2.cvtColor(self.image, cv2.COLOR_RGB2GRAY)  # Convert to grayscaleself.blurred_image = Noneself.edges = Noneself.inverted_edges = Noneself.cartoon_image = Noneself.save_counter = 0def apply_gaussian_blur(self, ksize=(5, 5), sigmaX=0):self.blurred_image = cv2.GaussianBlur(self.gray_image, ksize, sigmaX)self.cartoon_image = self.blurred_image  # Assuming cartoon_image should be blurred initiallyself.save_counter += 1self.save_image(f'new{self.save_counter}.jpg')def detect_edges(self, threshold1=50, threshold2=150):self.apply_gaussian_blur()self.edges = cv2.Canny(self.blurred_image, threshold1, threshold2)self.cartoon_image = self.edges  # Assuming cartoon_image should show edges initiallyself.save_counter += 1self.save_image(f'new{self.save_counter}.jpg')def invert_edges(self):self.detect_edges()self.inverted_edges = cv2.bitwise_not(self.edges)self.cartoon_image = self.inverted_edges  # Assuming cartoon_image should show inverted edges initiallyself.save_counter += 1self.save_image(f'new{self.save_counter}.jpg')def create_cartoon_effect(self):self.invert_edges()# Create a cartoon effect by combining edges and original imagecolor_image = cv2.bilateralFilter(self.image, d=9, sigmaColor=75, sigmaSpace=75)self.cartoon_image = cv2.bitwise_and(color_image, color_image, mask=self.inverted_edges)self.save_counter += 1self.save_image(f'new{self.save_counter}.jpg')def save_image(self, filename):if self.cartoon_image is None or self.cartoon_image.size == 0:print("Cartoon image is empty, skipping save.")returnif not os.path.exists('../newImg'):os.makedirs('../newImg')cv2.imwrite(f'newImg/{filename}', cv2.cvtColor(self.cartoon_image, cv2.COLOR_RGB2BGR))print(f"图片已保存为 newImg/{filename}")def show_images(self):plt.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文plt.rcParams['axes.unicode_minus'] = False  # 解决坐标轴负号显示问题plt.figure(figsize=(10, 10))plt.subplot(1, 2, 1)plt.title("原图片")plt.imshow(self.image)plt.axis('off')plt.subplot(1, 2, 2)plt.title("处理后的图片")plt.imshow(self.cartoon_image)plt.axis('off')plt.show()def main():image_path = '../images/img1.jpg'  # 请确保路径正确processor = ImageProcessor(image_path)while True:print("\n请选择图片处理功能编号:")print("1 - 高斯模糊")print("2 - Canny边缘检测")print("3 - 反转边缘图像")print("4 - 生成手绘效果")print("5 - 显示图片")print("0 - 退出")choice = input("输入你的选择:")if choice == '1':processor.apply_gaussian_blur()print("高斯模糊已应用并保存。")elif choice == '2':processor.detect_edges()print("Canny边缘检测已应用并保存。")elif choice == '3':processor.invert_edges()print("边缘图像已反转并保存。")elif choice == '4':processor.create_cartoon_effect()print("手绘效果已生成并保存。")elif choice == '5':processor.show_images()elif choice == '0':print("退出程序。")breakelse:print("无效的输入,请重新输入。")if __name__ == "__main__":main()

终端运行效果:
在这里插入图片描述
生成手绘照片处理效果如下:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com