Home  >  Article  >  Backend Development  >  How to binarize images in python language

How to binarize images in python language

尚
Original
2019-07-08 14:07:594600browse

How to binarize images in python language

Python语言是一门比较简单好用的语言,其图像处理也是比较重要的一种应用,那么该怎么实现对对一个图片的二值化处理呢,下面小编给出具体的方法。

1、打开Python的shell界面,如图所示。

How to binarize images in python language

载入相关的库包,代码如下:

from skimage import io,data,color

How to binarize images in python language

读取图片,代码如下:

img=data.

注,这里读取的是工具包中的图片,大家也可以读取自己的图片,可参考实例“如何采用Python读取一个图像”,具体代码如下:

    img_name="D:\\WinPython-64bit-3.5.3.0Qt5\\notebooks\\hashiqi.jpg"
    img=io.imread(img_name,as_grey=False)

How to binarize images in python language

将图片进行灰度化处理,具体指令如下:

img_gray=color.rgb2gray(img)

How to binarize images in python language

对图片进行二值化处理,代码如下;

rows,cols=img_gray.shape
for i in range(rows):    
    for j in range(cols):
        if (img_gray[i,j]<=0.5):
            img_gray[i,j]=0
        else:
            img_gray[i,j]=1

How to binarize images in python language

查看结果的代码:

io.imshow(img_gray)
io.show

How to binarize images in python language

查看我们的效果如下图。前图是二值化之前,后图是二值化之后。

How to binarize images in python language

How to binarize images in python language

更多Python相关技术文章,请访问Python教程栏目进行学习!

The above is the detailed content of How to binarize images in python language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn