如何在Python中使用OpenCV对图像进行像素化? [英] How to pixelate image using OpenCV in Python?

查看:283
本文介绍了如何在Python中使用OpenCV对图像进行像素化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此链接中的最佳答案 如何使用python将正方形图像像素化为256个大像素?使用PIL像素化图像.可以将图像从PIL转换为cv2.Mat,但是不允许使用其他库,并且使用opencv找不到任何好的方法.

Top answer in this link How to pixelate a square image to 256 big pixels with python? uses PIL to pixelate image. Converting image from PIL to cv2.Mat is possible but I'm not allowed to use other library, and I couldn't find any good method using opencv.

有什么方法可以仅在Python中使用OpenCV库对图像进行像素化吗?任何样本图像都可以.我可以控制像素大小参数以进行后续调整的解决方案将不胜感激.

Is there any way to pixelate image using OpenCV library only in Python? Any sample image is fine. Solution with pixel size parameter that I can control for later adjustment would be very appreciated.

推荐答案

EDIT ^ 2

在自己的帮助下,我将 Mark Setchell的答案(上面提到的最重要的答案)移到了简单的OpenCV Python代码. (请查看我的答案的修订历史,以循环查看旧版本.)

EDIT^2

With the help of himself, I moved Mark Setchell's answer, which is the above mentioned top answer, to plain OpenCV Python code. (Have a look at the revision history of my answer to see the old version using a loop.)

import cv2

# Input image
input = cv2.imread('images/paddington.png')

# Get input size
height, width = input.shape[:2]

# Desired "pixelated" size
w, h = (16, 16)

# Resize input to "pixelated" size
temp = cv2.resize(input, (w, h), interpolation=cv2.INTER_LINEAR)

# Initialize output image
output = cv2.resize(temp, (width, height), interpolation=cv2.INTER_NEAREST)

cv2.imshow('Input', input)
cv2.imshow('Output', output)

cv2.waitKey(0)

输入(来自链接的问题):

Input (from linked question):

输出:

免责声明:我是Python的新手,尤其是OpenCV(胜利的C ++)的Python API.非常欢迎评论,改进,强调Python的执行!!

Disclaimer: I'm new to Python in general, and specially to the Python API of OpenCV (C++ for the win). Comments, improvements, highlighting Python no-gos are highly welcome!

这篇关于如何在Python中使用OpenCV对图像进行像素化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆